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