Camera Shake#

Screen shot of using a scrolling window

You can cause the camera to shake, as this example does when the player encounters a bomb. See the highlighted lines below.

sprite_move_scrolling_shake.py#
  1"""
  2Scroll around a large screen.
  3
  4Artwork from https://kenney.nl
  5
  6If Python and Arcade are installed, this example can be run from the command line with:
  7python -m arcade.examples.sprite_move_scrolling_shake
  8"""
  9
 10from __future__ import annotations
 11
 12import random
 13import math
 14import arcade
 15
 16SPRITE_SCALING = 0.5
 17
 18DEFAULT_SCREEN_WIDTH = 800
 19DEFAULT_SCREEN_HEIGHT = 600
 20SCREEN_TITLE = "Camera Shake Example"
 21
 22# How many pixels to keep as a minimum margin between the character
 23# and the edge of the screen.
 24VIEWPORT_MARGIN = 220
 25
 26# How fast the camera pans to the player. 1.0 is instant.
 27CAMERA_SPEED = 0.1
 28
 29# How fast the character moves
 30PLAYER_MOVEMENT_SPEED = 7
 31
 32BOMB_COUNT = 50
 33PLAYING_FIELD_WIDTH = 1600
 34PLAYING_FIELD_HEIGHT = 1600
 35
 36
 37class MyGame(arcade.Window):
 38    """ Main application class. """
 39
 40    def __init__(self, width, height, title):
 41        """
 42        Initializer
 43        """
 44        super().__init__(width, height, title, resizable=True)
 45
 46        # Sprite lists
 47        self.player_list = None
 48        self.wall_list = None
 49        self.bomb_list = None
 50
 51        # Set up the player
 52        self.player_sprite = None
 53
 54        # Physics engine so we don't run into walls.
 55        self.physics_engine = None
 56
 57        # Create the cameras. One for the GUI, one for the sprites.
 58        # We scroll the 'sprite world' but not the GUI.
 59        self.camera_sprites = arcade.Camera()
 60        self.camera_gui = arcade.Camera()
 61
 62        self.explosion_sound = arcade.load_sound(":resources:sounds/explosion1.wav")
 63
 64    def setup(self):
 65        """ Set up the game and initialize the variables. """
 66
 67        # Sprite lists
 68        self.player_list = arcade.SpriteList()
 69        self.wall_list = arcade.SpriteList()
 70        self.bomb_list = arcade.SpriteList()
 71
 72        # Set up the player
 73        self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
 74                                           scale=0.4)
 75        self.player_sprite.center_x = 512
 76        self.player_sprite.center_y = 512
 77        self.player_list.append(self.player_sprite)
 78
 79        # -- Set up several columns of walls
 80        for x in range(200, PLAYING_FIELD_WIDTH, 210):
 81            for y in range(0, PLAYING_FIELD_HEIGHT, 64):
 82                # Randomly skip a box so the player can find a way through
 83                if random.randrange(5) > 0:
 84                    wall = arcade.Sprite(":resources:images/tiles/grassCenter.png", scale=SPRITE_SCALING)
 85                    wall.center_x = x
 86                    wall.center_y = y
 87                    self.wall_list.append(wall)
 88
 89        for i in range(BOMB_COUNT):
 90            bomb = arcade.Sprite(":resources:images/tiles/bomb.png", scale=0.25)
 91            placed = False
 92            while not placed:
 93                bomb.center_x = random.randrange(PLAYING_FIELD_WIDTH)
 94                bomb.center_y = random.randrange(PLAYING_FIELD_HEIGHT)
 95                if not arcade.check_for_collision_with_list(bomb, self.wall_list):
 96                    placed = True
 97            self.bomb_list.append(bomb)
 98
 99        self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.wall_list)
100
101        # Set the background color
102        self.background_color = arcade.color.AMAZON
103
104    def on_draw(self):
105        """
106        Render the screen.
107        """
108
109        # This command has to happen before we start drawing
110        self.clear()
111
112        # Select the camera we'll use to draw all our sprites
113        self.camera_sprites.use()
114
115        # Draw all the sprites.
116        self.wall_list.draw()
117        self.bomb_list.draw()
118        self.player_list.draw()
119
120    def on_key_press(self, key, modifiers):
121        """Called whenever a key is pressed. """
122
123        if key == arcade.key.UP:
124            self.player_sprite.change_y = PLAYER_MOVEMENT_SPEED
125        elif key == arcade.key.DOWN:
126            self.player_sprite.change_y = -PLAYER_MOVEMENT_SPEED
127        elif key == arcade.key.LEFT:
128            self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
129        elif key == arcade.key.RIGHT:
130            self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
131
132    def on_key_release(self, key, modifiers):
133        """Called when the user releases a key. """
134
135        if key == arcade.key.UP or key == arcade.key.DOWN:
136            self.player_sprite.change_y = 0
137        elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
138            self.player_sprite.change_x = 0
139
140    def on_update(self, delta_time):
141        """ Movement and game logic """
142
143        # Call update on all sprites (The sprites don't do much in this
144        # example though.)
145        self.physics_engine.update()
146
147        # Scroll the screen to the player
148        self.scroll_to_player()
149
150        hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.bomb_list)
151        for bomb in hit_list:
152            # Remove the bomb and go 'boom'
153            bomb.remove_from_sprite_lists()
154            self.explosion_sound.play()
155
156            # --- Shake the camera ---
157            # Pick a random direction
158            shake_direction = random.random() * 2 * math.pi
159            # How 'far' to shake
160            shake_amplitude = 10
161            # Calculate a vector based on that
162            shake_vector = (
163                math.cos(shake_direction) * shake_amplitude,
164                math.sin(shake_direction) * shake_amplitude
165            )
166            # Frequency of the shake
167            shake_speed = 1.5
168            # How fast to damp the shake
169            shake_damping = 0.9
170            # Do the shake
171            self.camera_sprites.shake(shake_vector,
172                                      speed=shake_speed,
173                                      damping=shake_damping)
174
175    def scroll_to_player(self):
176        """
177        Scroll the window to the player.
178
179        if CAMERA_SPEED is 1, the camera will immediately move to the desired position.
180        Anything between 0 and 1 will have the camera move to the location with a smoother
181        pan.
182        """
183
184        position = (
185            self.player_sprite.center_x - self.width / 2,
186            self.player_sprite.center_y - self.height / 2
187        )
188        self.camera_sprites.move_to(position, CAMERA_SPEED)
189
190    def on_resize(self, width: int, height: int):
191        """
192        Resize window
193        Handle the user grabbing the edge and resizing the window.
194        """
195        self.camera_sprites.resize(width, height)
196        self.camera_gui.resize(width, height)
197
198
199def main():
200    """ Main function """
201    window = MyGame(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, SCREEN_TITLE)
202    window.setup()
203    arcade.run()
204
205
206if __name__ == "__main__":
207    main()