Easing Example 2#

Source#
easing_example.py#
1"""
2Example showing how to use the easing functions for position.
3Example showing how to use easing for angles.
4
5See:
6https://easings.net/
7...for a great guide on the theory behind how easings can work.
8
9If Python and Arcade are installed, this example can be run from the command line with:
10python -m arcade.examples.easing_example_2
11"""
12import arcade
13from arcade import easing
14
15SPRITE_SCALING = 0.5
16
17SCREEN_WIDTH = 800
18SCREEN_HEIGHT = 600
19SCREEN_TITLE = "Easing Example"
20
21
22class Player(arcade.Sprite):
23 """ Player class """
24
25 def __init__(self, image, scale):
26 """ Set up the player """
27
28 # Call the parent init
29 super().__init__(image, scale=scale)
30
31 self.easing_angle_data = None
32 self.easing_x_data = None
33 self.easing_y_data = None
34
35 def on_update(self, delta_time: float = 1 / 60):
36 if self.easing_angle_data is not None:
37 done, self.angle = easing.ease_angle_update(self.easing_angle_data, delta_time)
38 if done:
39 self.easing_angle_data = None
40
41 if self.easing_x_data is not None:
42 done, self.center_x = easing.ease_update(self.easing_x_data, delta_time)
43 if done:
44 self.easing_x_data = None
45
46 if self.easing_y_data is not None:
47 done, self.center_y = easing.ease_update(self.easing_y_data, delta_time)
48 if done:
49 self.easing_y_data = None
50
51
52class MyGame(arcade.Window):
53 """ Main application class. """
54
55 def __init__(self, width, height, title):
56 """ Initializer """
57
58 # Call the parent class initializer
59 super().__init__(width, height, title)
60
61 # Variables that will hold sprite lists
62 self.player_list = None
63
64 # Set up the player info
65 self.player_sprite = None
66
67 # Set the background color
68 self.background_color = arcade.color.BLACK
69 self.text = "Press 1-9 to apply an easing function."
70
71 def setup(self):
72 """ Set up the game and initialize the variables. """
73
74 # Sprite lists
75 self.player_list = arcade.SpriteList()
76
77 # Set up the player
78 self.player_sprite = Player(":resources:images/space_shooter/playerShip1_orange.png",
79 SPRITE_SCALING)
80 self.player_sprite.angle = 0
81 self.player_sprite.center_x = SCREEN_WIDTH / 2
82 self.player_sprite.center_y = SCREEN_HEIGHT / 2
83 self.player_list.append(self.player_sprite)
84
85 def on_draw(self):
86 """ Render the screen. """
87
88 # This command has to happen before we start drawing
89 self.clear()
90
91 # Draw all the sprites.
92 self.player_list.draw()
93
94 arcade.draw_text(self.text, 10, 10, arcade.color.WHITE, 18)
95
96 def on_update(self, delta_time):
97 """ Movement and game logic """
98
99 # Call update on all sprites (The sprites don't do much in this
100 # example though.)
101 self.player_list.on_update(delta_time)
102
103 def on_key_press(self, key, modifiers):
104 x = self.mouse["x"]
105 y = self.mouse["y"]
106
107 if key == arcade.key.KEY_1:
108 point = x, y
109 self.player_sprite.face_point(point)
110 self.text = "Instant angle change"
111 if key in [arcade.key.KEY_2, arcade.key.KEY_3, arcade.key.KEY_4, arcade.key.KEY_5]:
112 p1 = self.player_sprite.position
113 p2 = (x, y)
114 end_angle = -arcade.get_angle_degrees(p1[0], p1[1], p2[0], p2[1])
115 start_angle = self.player_sprite.angle
116 if key == arcade.key.KEY_2:
117 ease_function = easing.linear
118 self.text = "Linear easing - angle"
119 elif key == arcade.key.KEY_3:
120 ease_function = easing.ease_in
121 self.text = "Ease in - angle"
122 elif key == arcade.key.KEY_4:
123 ease_function = easing.ease_out
124 self.text = "Ease out - angle"
125 elif key == arcade.key.KEY_5:
126 ease_function = easing.smoothstep
127 self.text = "Smoothstep - angle"
128 else:
129 raise ValueError("?")
130
131 self.player_sprite.easing_angle_data = easing.ease_angle(start_angle,
132 end_angle,
133 rate=180,
134 ease_function=ease_function)
135
136 if key in [arcade.key.KEY_6, arcade.key.KEY_7, arcade.key.KEY_8, arcade.key.KEY_9]:
137 p1 = self.player_sprite.position
138 p2 = (x, y)
139 if key == arcade.key.KEY_6:
140 ease_function = easing.linear
141 self.text = "Linear easing - position"
142 elif key == arcade.key.KEY_7:
143 ease_function = easing.ease_in
144 self.text = "Ease in - position"
145 elif key == arcade.key.KEY_8:
146 ease_function = easing.ease_out
147 self.text = "Ease out - position"
148 elif key == arcade.key.KEY_9:
149 ease_function = easing.smoothstep
150 self.text = "Smoothstep - position"
151 else:
152 raise ValueError("?")
153
154 ex, ey = easing.ease_position(p1, p2, rate=180, ease_function=ease_function)
155 self.player_sprite.easing_x_data = ex
156 self.player_sprite.easing_y_data = ey
157
158 def on_mouse_press(self, x: float, y: float, button: int, modifiers: int):
159 self.player_sprite.face_point((x, y))
160
161
162def main():
163 """ Main function """
164 window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
165 window.setup()
166 arcade.run()
167
168
169if __name__ == "__main__":
170 main()