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