Easing Example 2#

Easing Example

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