Move with a Scrolling Screen - Margins#

Unlike the Move with a Scrolling Screen - Centered which centers the camera on the player, this example only moves the camera if the user gets within so many pixels of the edge. It allows for a ‘box’ in the middle where the user can move around and NOT move the camera.

Screen shot of using a scrolling window
sprite_move_scrolling_box.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_box
  8"""
  9
 10from __future__ import annotations
 11
 12import random
 13import arcade
 14
 15SPRITE_SCALING = 0.5
 16
 17DEFAULT_SCREEN_WIDTH = 800
 18DEFAULT_SCREEN_HEIGHT = 600
 19SCREEN_TITLE = "Sprite Move with Scrolling Screen Example"
 20
 21# How many pixels to keep as a minimum margin between the character
 22# and the edge of the screen.
 23VIEWPORT_MARGIN = 200
 24
 25# How fast the camera pans to the player. 1.0 is instant.
 26CAMERA_SPEED = 0.1
 27
 28# How fast the character moves
 29PLAYER_MOVEMENT_SPEED = 7
 30
 31
 32class MyGame(arcade.Window):
 33    """ Main application class. """
 34
 35    def __init__(self, width, height, title):
 36        """
 37        Initializer
 38        """
 39        super().__init__(width, height, title, resizable=True)
 40
 41        # Sprite lists
 42        self.player_list = None
 43        self.wall_list = None
 44
 45        # Set up the player
 46        self.player_sprite = None
 47
 48        self.physics_engine = None
 49
 50        # Used in scrolling
 51        self.view_bottom = 0
 52        self.view_left = 0
 53
 54        # Track the current state of what key is pressed
 55        self.left_pressed = False
 56        self.right_pressed = False
 57        self.up_pressed = False
 58        self.down_pressed = False
 59
 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        # Set the viewport boundaries
 93        # These numbers set where we have 'scrolled' to.
 94        self.view_left = 0
 95        self.view_bottom = 0
 96
 97    def on_draw(self):
 98        """
 99        Render the screen.
100        """
101
102        # This command has to happen before we start drawing
103        self.clear()
104
105        # Select the camera we'll use to draw all our sprites
106        self.camera_sprites.use()
107
108        # Draw all the sprites.
109        self.wall_list.draw()
110        self.player_list.draw()
111
112        # Select the (unscrolled) camera for our GUI
113        self.camera_gui.use()
114
115        # Draw the GUI
116        arcade.draw_rectangle_filled(self.width // 2, 20, self.width, 40, arcade.color.ALMOND)
117        text = f"Scroll value: ({self.camera_sprites.position[0]:5.1f}, {self.camera_sprites.position[1]:5.1f})"
118        arcade.draw_text(text, 10, 10, arcade.color.BLACK_BEAN, 20)
119
120        # Draw the box that we work to make sure the user stays inside of.
121        # This is just for illustration purposes. You'd want to remove this
122        # in your game.
123        left_boundary = VIEWPORT_MARGIN
124        right_boundary = self.width - VIEWPORT_MARGIN
125        top_boundary = self.height - VIEWPORT_MARGIN
126        bottom_boundary = VIEWPORT_MARGIN
127        arcade.draw_lrbt_rectangle_outline(left_boundary, right_boundary, bottom_boundary, top_boundary,
128                                           arcade.color.RED, 2)
129
130    def on_key_press(self, key, modifiers):
131        """Called whenever a key is pressed. """
132
133        if key == arcade.key.UP:
134            self.up_pressed = True
135        elif key == arcade.key.DOWN:
136            self.down_pressed = True
137        elif key == arcade.key.LEFT:
138            self.left_pressed = True
139        elif key == arcade.key.RIGHT:
140            self.right_pressed = True
141
142    def on_key_release(self, key, modifiers):
143        """Called when the user releases a key. """
144
145        if key == arcade.key.UP:
146            self.up_pressed = False
147        elif key == arcade.key.DOWN:
148            self.down_pressed = False
149        elif key == arcade.key.LEFT:
150            self.left_pressed = False
151        elif key == arcade.key.RIGHT:
152            self.right_pressed = False
153
154    def on_update(self, delta_time):
155        """ Movement and game logic """
156
157        # Calculate speed based on the keys pressed
158        self.player_sprite.change_x = 0
159        self.player_sprite.change_y = 0
160
161        if self.up_pressed and not self.down_pressed:
162            self.player_sprite.change_y = PLAYER_MOVEMENT_SPEED
163        elif self.down_pressed and not self.up_pressed:
164            self.player_sprite.change_y = -PLAYER_MOVEMENT_SPEED
165        if self.left_pressed and not self.right_pressed:
166            self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
167        elif self.right_pressed and not self.left_pressed:
168            self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
169
170        # Call update on all sprites (The sprites don't do much in this
171        # example though.)
172        self.physics_engine.update()
173
174        # Scroll the screen to the player
175        self.scroll_to_player()
176
177    def scroll_to_player(self):
178        """
179        Scroll the window to the player.
180        This method will attempt to keep the player at least VIEWPORT_MARGIN
181        pixels away from the edge.
182
183        if CAMERA_SPEED is 1, the camera will immediately move to the desired position.
184        Anything between 0 and 1 will have the camera move to the location with a smoother
185        pan.
186        """
187
188        # --- Manage Scrolling ---
189
190        # Scroll left
191        left_boundary = self.view_left + VIEWPORT_MARGIN
192        if self.player_sprite.left < left_boundary:
193            self.view_left -= left_boundary - self.player_sprite.left
194
195        # Scroll right
196        right_boundary = self.view_left + self.width - VIEWPORT_MARGIN
197        if self.player_sprite.right > right_boundary:
198            self.view_left += self.player_sprite.right - right_boundary
199
200        # Scroll up
201        top_boundary = self.view_bottom + self.height - VIEWPORT_MARGIN
202        if self.player_sprite.top > top_boundary:
203            self.view_bottom += self.player_sprite.top - top_boundary
204
205        # Scroll down
206        bottom_boundary = self.view_bottom + VIEWPORT_MARGIN
207        if self.player_sprite.bottom < bottom_boundary:
208            self.view_bottom -= bottom_boundary - self.player_sprite.bottom
209
210        # Scroll to the proper location
211        position = self.view_left, self.view_bottom
212        self.camera_sprites.move_to(position, CAMERA_SPEED)
213
214    def on_resize(self, width: int, height: int):
215        """
216        Resize window
217        Handle the user grabbing the edge and resizing the window.
218        """
219        self.camera_sprites.resize(width, height)
220        self.camera_gui.resize(width, height)
221
222
223def main():
224    """ Main function """
225    window = MyGame(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, SCREEN_TITLE)
226    window.setup()
227    arcade.run()
228
229
230if __name__ == "__main__":
231    main()