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