Move with a Scrolling Screen - Centered#

Using a arcade.Camera, a program can easily scroll around a larger “world” while only showing part of it on the screen.

If you are displaying a GUI or some other items that should NOT scroll, you’ll need two cameras. One that shows the unscrolled GUI, and one that shows the scrolled sprites.

See also Move with a Scrolling Screen - Margins.

Screen shot of using a scrolling window
sprite_move_scrolling.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
  8"""
  9
 10from __future__ import annotations
 11
 12import random
 13import arcade
 14from pyglet.math import Vec2
 15
 16SPRITE_SCALING = 0.5
 17
 18DEFAULT_SCREEN_WIDTH = 800
 19DEFAULT_SCREEN_HEIGHT = 600
 20SCREEN_TITLE = "Sprite Move with Scrolling Screen 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
 32
 33class MyGame(arcade.Window):
 34    """ Main application class. """
 35
 36    def __init__(self, width, height, title):
 37        """
 38        Initializer
 39        """
 40        super().__init__(width, height, title, resizable=True)
 41
 42        # Sprite lists
 43        self.player_list = None
 44        self.wall_list = None
 45
 46        # Set up the player
 47        self.player_sprite = None
 48
 49        # Physics engine so we don't run into walls.
 50        self.physics_engine = None
 51
 52        # Track the current state of what key is pressed
 53        self.left_pressed = False
 54        self.right_pressed = False
 55        self.up_pressed = False
 56        self.down_pressed = False
 57
 58        # Create the cameras. One for the GUI, one for the sprites.
 59        # We scroll the 'sprite world' but not the GUI.
 60        self.camera_sprites = arcade.SimpleCamera()
 61        self.camera_gui = arcade.SimpleCamera()
 62
 63    def setup(self):
 64        """ Set up the game and initialize the variables. """
 65
 66        # Sprite lists
 67        self.player_list = arcade.SpriteList()
 68        self.wall_list = arcade.SpriteList()
 69
 70        # Set up the player
 71        self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
 72                                           scale=0.4)
 73        self.player_sprite.center_x = 256
 74        self.player_sprite.center_y = 512
 75        self.player_list.append(self.player_sprite)
 76
 77        # -- Set up several columns of walls
 78        for x in range(200, 1650, 210):
 79            for y in range(0, 1600, 64):
 80                # Randomly skip a box so the player can find a way through
 81                if random.randrange(5) > 0:
 82                    wall = arcade.Sprite(":resources:images/tiles/grassCenter.png", scale=SPRITE_SCALING)
 83                    wall.center_x = x
 84                    wall.center_y = y
 85                    self.wall_list.append(wall)
 86
 87        self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.wall_list)
 88
 89        # Set the background color
 90        self.background_color = arcade.color.AMAZON
 91
 92    def on_draw(self):
 93        """ Render the screen. """
 94
 95        # This command has to happen before we start drawing
 96        self.clear()
 97
 98        # Select the camera we'll use to draw all our sprites
 99        self.camera_sprites.use()
100
101        # Draw all the sprites.
102        self.wall_list.draw()
103        self.player_list.draw()
104
105        # Select the (unscrolled) camera for our GUI
106        self.camera_gui.use()
107
108        # Draw the GUI
109        arcade.draw_rectangle_filled(self.width // 2,
110                                     20,
111                                     self.width,
112                                     40,
113                                     arcade.color.ALMOND)
114        text = f"Scroll value: ({self.camera_sprites.position[0]:5.1f}, " \
115               f"{self.camera_sprites.position[1]:5.1f})"
116        arcade.draw_text(text, 10, 10, arcade.color.BLACK_BEAN, 20)
117
118    def on_key_press(self, key, modifiers):
119        """Called whenever a key is pressed. """
120
121        if key == arcade.key.UP:
122            self.up_pressed = True
123        elif key == arcade.key.DOWN:
124            self.down_pressed = True
125        elif key == arcade.key.LEFT:
126            self.left_pressed = True
127        elif key == arcade.key.RIGHT:
128            self.right_pressed = True
129
130    def on_key_release(self, key, modifiers):
131        """Called when the user releases a key. """
132
133        if key == arcade.key.UP:
134            self.up_pressed = False
135        elif key == arcade.key.DOWN:
136            self.down_pressed = False
137        elif key == arcade.key.LEFT:
138            self.left_pressed = False
139        elif key == arcade.key.RIGHT:
140            self.right_pressed = False
141
142    def on_update(self, delta_time):
143        """ Movement and game logic """
144
145        # Calculate speed based on the keys pressed
146        self.player_sprite.change_x = 0
147        self.player_sprite.change_y = 0
148
149        if self.up_pressed and not self.down_pressed:
150            self.player_sprite.change_y = PLAYER_MOVEMENT_SPEED
151        elif self.down_pressed and not self.up_pressed:
152            self.player_sprite.change_y = -PLAYER_MOVEMENT_SPEED
153        if self.left_pressed and not self.right_pressed:
154            self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
155        elif self.right_pressed and not self.left_pressed:
156            self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
157
158        # Call update on all sprites (The sprites don't do much in this
159        # example though.)
160        self.physics_engine.update()
161
162        # Scroll the screen to the player
163        self.scroll_to_player()
164
165    def scroll_to_player(self):
166        """
167        Scroll the window to the player.
168
169        if CAMERA_SPEED is 1, the camera will immediately move to the desired position.
170        Anything between 0 and 1 will have the camera move to the location with a smoother
171        pan.
172        """
173
174        position = Vec2(self.player_sprite.center_x - self.width / 2,
175                        self.player_sprite.center_y - self.height / 2)
176        self.camera_sprites.move_to(position, CAMERA_SPEED)
177
178    def on_resize(self, width: int, height: int):
179        """
180        Resize window
181        Handle the user grabbing the edge and resizing the window.
182        """
183        self.camera_sprites.resize(width, height)
184        self.camera_gui.resize(width, height)
185
186
187def main():
188    """ Main function """
189    window = MyGame(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, SCREEN_TITLE)
190    window.setup()
191    arcade.run()
192
193
194if __name__ == "__main__":
195    main()