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