Easing Example 1

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
19WINDOW_WIDTH = 1280
20WINDOW_HEIGHT = 720
21WINDOW_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 = 1200
31Y_INTERVAL = 60
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 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 < WINDOW_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 GameView(arcade.View):
68 """ Main application class. """
69
70 def __init__(self):
71 """ Initializer """
72
73 # Call the parent class initializer
74 super().__init__()
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(
111 text_string,
112 x=X_START,
113 y=y - BALL_RADIUS,
114 color=text_color,
115 font_size=24,
116 )
117 return text
118
119 def add_item(item_y, ease_function, text):
120 ball = create_ball(item_y, ease_function)
121 self.ball_list.append(ball)
122 text = create_text(text)
123 self.text_list.append(text)
124 line = create_line(item_y)
125 self.lines.append(line)
126
127 text_color = Color.from_hex_string(TEXT_COLOR)
128 line_color = Color.from_hex_string(LINE_COLOR)
129
130 y = Y_INTERVAL
131 add_item(y, easing.linear, "Linear")
132
133 y += Y_INTERVAL
134 add_item(y, easing.ease_out, "Ease out")
135
136 y += Y_INTERVAL
137 add_item(y, easing.ease_in, "Ease in")
138
139 y += Y_INTERVAL
140 add_item(y, easing.smoothstep, "Smoothstep")
141
142 y += Y_INTERVAL
143 add_item(y, easing.ease_in_out, "Ease in/out")
144
145 y += Y_INTERVAL
146 add_item(y, easing.ease_out_elastic, "Ease out elastic")
147
148 y += Y_INTERVAL
149 add_item(y, easing.ease_in_back, "Ease in back")
150
151 y += Y_INTERVAL
152 add_item(y, easing.ease_out_back, "Ease out back")
153
154 y += Y_INTERVAL
155 add_item(y, easing.ease_in_sin, "Ease in sin")
156
157 y += Y_INTERVAL
158 add_item(y, easing.ease_out_sin, "Ease out sin")
159
160 y += Y_INTERVAL
161 add_item(y, easing.ease_in_out_sin, "Ease in out sin")
162
163 def on_draw(self):
164 """ Render the screen. """
165
166 # This command has to happen before we start drawing
167 self.clear()
168
169 self.lines.draw()
170
171 # Draw all the sprites.
172 self.ball_list.draw()
173
174 for text in self.text_list:
175 text.draw()
176
177 def on_update(self, delta_time):
178 """ Movement and game logic """
179
180 # Call update on all sprites (The sprites don't do much in this
181 # example though.)
182 self.ball_list.update(delta_time)
183
184
185def main():
186 """ Main function """
187 # Create a window class. This is what actually shows up on screen
188 window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
189
190 # Create and setup the GameView
191 game = GameView()
192 game.setup()
193
194 # Show GameView on screen
195 window.show_view(game)
196
197 # Start the arcade game loop
198 arcade.run()
199
200
201if __name__ == "__main__":
202 main()