Easing Example 1

Easing Example

Source

easing_example.py
  1"""
  2Example showing how to use the easing functions for position.
  3
  4See:
  5https://easings.net/
  6...for a great guide on the theory behind how easings can work.
  7
  8See example 2 for how to use easings for angles.
  9
 10If Python and Arcade are installed, this example can be run from the command line with:
 11python -m arcade.examples.easing_example_1
 12"""
 13import arcade
 14from arcade import easing
 15from arcade.types import Color
 16
 17SPRITE_SCALING = 0.5
 18
 19SCREEN_WIDTH = 800
 20SCREEN_HEIGHT = 600
 21SCREEN_TITLE = "Easing Example"
 22
 23BACKGROUND_COLOR = "#F5D167"
 24TEXT_COLOR = "#4B1DF2"
 25BALL_COLOR = "#42B5EB"
 26LINE_COLOR = "#45E6D0"
 27LINE_WIDTH = 3
 28
 29X_START = 40
 30X_END = 760
 31Y_INTERVAL = 50
 32BALL_RADIUS = 13
 33TIME = 3.0
 34
 35
 36class EasingCircle(arcade.SpriteCircle):
 37    """ Player class """
 38
 39    def __init__(self, radius, color):
 40        """ Set up the player """
 41
 42        # Call the parent init
 43        super().__init__(radius, color)
 44
 45        self.easing_x_data = None
 46        self.easing_y_data = None
 47
 48    def on_update(self, delta_time: float = 1 / 60):
 49        if self.easing_x_data is not None:
 50            done, self.center_x = easing.ease_update(self.easing_x_data, delta_time)
 51            if done:
 52                x = X_START
 53                if self.center_x < SCREEN_WIDTH / 2:
 54                    x = X_END
 55                ex, ey = easing.ease_position(self.position,
 56                                              (x, self.center_y),
 57                                              rate=180,
 58                                              ease_function=self.easing_x_data.ease_function)
 59                self.easing_x_data = ex
 60
 61        if self.easing_y_data is not None:
 62            done, self.center_y = easing.ease_update(self.easing_y_data, delta_time)
 63            if done:
 64                self.easing_y_data = None
 65
 66
 67class MyGame(arcade.Window):
 68    """ Main application class. """
 69
 70    def __init__(self, width, height, title):
 71        """ Initializer """
 72
 73        # Call the parent class initializer
 74        super().__init__(width, height, title)
 75
 76        # Set the background color
 77        self.background_color = Color.from_hex_string(BACKGROUND_COLOR)
 78
 79        self.ball_list = None
 80        self.text_list = []
 81        self.lines = None
 82
 83    def setup(self):
 84        """ Set up the game and initialize the variables. """
 85
 86        # Sprite lists
 87        self.ball_list = arcade.SpriteList()
 88        self.lines = arcade.shape_list.ShapeElementList()
 89
 90        def create_ball(ball_y, ease_function):
 91            ball = EasingCircle(BALL_RADIUS, Color.from_hex_string(BALL_COLOR))
 92            ball.position = X_START, ball_y
 93            p1 = ball.position
 94            p2 = (X_END, ball_y)
 95            ex, ey = easing.ease_position(p1, p2, time=TIME, ease_function=ease_function)
 96            ball.ease_function = ease_function
 97            ball.easing_x_data = ex
 98            ball.easing_y_data = ey
 99            return ball
100
101        def create_line(line_y):
102            line = arcade.shape_list.create_line(
103                X_START, line_y - BALL_RADIUS - LINE_WIDTH,
104                X_END, line_y - BALL_RADIUS,
105                line_color, line_width=LINE_WIDTH,
106            )
107            return line
108
109        def create_text(text_string):
110            text = arcade.Text(text_string, X_START, y - BALL_RADIUS, color=text_color, font_size=14)
111            return text
112
113        def add_item(item_y, ease_function, text):
114            ball = create_ball(item_y, ease_function)
115            self.ball_list.append(ball)
116            text = create_text(text)
117            self.text_list.append(text)
118            line = create_line(item_y)
119            self.lines.append(line)
120
121        text_color = Color.from_hex_string(TEXT_COLOR)
122        line_color = Color.from_hex_string(LINE_COLOR)
123
124        y = Y_INTERVAL
125        add_item(y, easing.linear, "Linear")
126
127        y += Y_INTERVAL
128        add_item(y, easing.ease_out, "Ease out")
129
130        y += Y_INTERVAL
131        add_item(y, easing.ease_in, "Ease in")
132
133        y += Y_INTERVAL
134        add_item(y, easing.smoothstep, "Smoothstep")
135
136        y += Y_INTERVAL
137        add_item(y, easing.ease_in_out, "Ease in/out")
138
139        y += Y_INTERVAL
140        add_item(y, easing.ease_out_elastic, "Ease out elastic")
141
142        y += Y_INTERVAL
143        add_item(y, easing.ease_in_back, "Ease in back")
144
145        y += Y_INTERVAL
146        add_item(y, easing.ease_out_back, "Ease out back")
147
148        y += Y_INTERVAL
149        add_item(y, easing.ease_in_sin, "Ease in sin")
150
151        y += Y_INTERVAL
152        add_item(y, easing.ease_out_sin, "Ease out sin")
153
154        y += Y_INTERVAL
155        add_item(y, easing.ease_in_out_sin, "Ease in out sin")
156
157    def on_draw(self):
158        """ Render the screen. """
159
160        # This command has to happen before we start drawing
161        self.clear()
162
163        self.lines.draw()
164
165        # Draw all the sprites.
166        self.ball_list.draw()
167
168        for text in self.text_list:
169            text.draw()
170
171    def on_update(self, delta_time):
172        """ Movement and game logic """
173
174        # Call update on all sprites (The sprites don't do much in this
175        # example though.)
176        self.ball_list.on_update(delta_time)
177
178
179def main():
180    """ Main function """
181    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
182    window.setup()
183    arcade.run()
184
185
186if __name__ == "__main__":
187    main()