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