Mini-Map#

Screen shot of a screen and mini-map.

This example shows how to create a ‘mini-map’ using frame buffers.

minimap.py#
  1"""
  2Work with a mini-map
  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.minimap
  8"""
  9
 10import random
 11from uuid import uuid4
 12
 13import arcade
 14from pyglet.math import Vec2
 15
 16SPRITE_SCALING = 0.5
 17
 18DEFAULT_SCREEN_WIDTH = 800
 19DEFAULT_SCREEN_HEIGHT = 600
 20SCREEN_TITLE = "Minimap 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
 32MINIMAP_BACKGROUND_COLOR = arcade.color.ALMOND
 33MINIMAP_WIDTH = 256
 34MINIMAP_HEIGHT = 256
 35MAP_WIDTH = 2048
 36MAP_HEIGHT = 2048
 37
 38
 39class MyGame(arcade.Window):
 40    """ Main application class. """
 41
 42    def __init__(self, width, height, title):
 43        """
 44        Initializer
 45        """
 46        super().__init__(width, height, title, resizable=True)
 47
 48        # Sprite lists
 49        self.player_list = None
 50        self.wall_list = None
 51
 52        # Mini-map related
 53        # List of all our minimaps (there's just one)
 54        self.minimap_sprite_list = None
 55        # Texture and associated sprite to render our minimap to
 56        self.minimap_texture = None
 57        self.minimap_sprite = None
 58
 59        # Set up the player
 60        self.player_sprite = None
 61
 62        self.physics_engine = None
 63
 64        # Camera for sprites, and one for our GUI
 65        viewport = (0, 0, DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT)
 66        self.camera_sprites = arcade.SimpleCamera(viewport=viewport)
 67        self.camera_gui = arcade.SimpleCamera(viewport=viewport)
 68
 69    def setup(self):
 70        """ Set up the game and initialize the variables. """
 71
 72        # Sprite lists
 73        self.player_list = arcade.SpriteList()
 74        self.wall_list = arcade.SpriteList()
 75
 76        # Set up the player
 77        self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/"
 78                                           "femalePerson_idle.png",
 79                                           scale=0.4)
 80        self.player_sprite.center_x = 256
 81        self.player_sprite.center_y = 512
 82        self.player_list.append(self.player_sprite)
 83
 84        # -- Set up several columns of walls
 85        for x in range(0, MAP_WIDTH, 210):
 86            for y in range(0, MAP_HEIGHT, 64):
 87                # Randomly skip a box so the player can find a way through
 88                if random.randrange(5) > 0:
 89                    wall = arcade.Sprite(":resources:images/tiles/grassCenter.png", scale=SPRITE_SCALING)
 90                    wall.center_x = x
 91                    wall.center_y = y
 92                    self.wall_list.append(wall)
 93
 94        self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.wall_list)
 95
 96        # Set the background color
 97        self.background_color = arcade.color.AMAZON
 98
 99        # Construct the minimap
100        size = (MINIMAP_WIDTH, MINIMAP_HEIGHT)
101        self.minimap_texture = arcade.Texture.create_empty(str(uuid4()), size)
102        self.minimap_sprite = arcade.Sprite(
103            self.minimap_texture,
104            center_x=MINIMAP_WIDTH / 2,
105            center_y=self.height - MINIMAP_HEIGHT / 2,
106        )
107
108        self.minimap_sprite_list = arcade.SpriteList()
109        self.minimap_sprite_list.append(self.minimap_sprite)
110
111    def update_minimap(self):
112        proj = 0, MAP_WIDTH, 0, MAP_HEIGHT
113        with self.minimap_sprite_list.atlas.render_into(self.minimap_texture, projection=proj) as fbo:
114            fbo.clear(MINIMAP_BACKGROUND_COLOR)
115            self.wall_list.draw()
116            self.player_sprite.draw()
117
118    def on_draw(self):
119        """
120        Render the screen.
121        """
122
123        # This command has to happen before we start drawing
124        self.clear()
125
126        # Select the camera we'll use to draw all our sprites
127        self.camera_sprites.use()
128
129        # Draw all the sprites.
130        self.wall_list.draw()
131        self.player_list.draw()
132
133        # Select the (unscrolled) camera for our GUI
134        self.camera_gui.use()
135
136        # Update the minimap
137        self.update_minimap()
138
139        # Draw the minimap
140        self.minimap_sprite_list.draw()
141
142        # Draw the GUI
143        arcade.draw_rectangle_filled(self.width // 2, 20, self.width, 40, arcade.color.ALMOND)
144        text = f"Scroll value: {self.camera_sprites.position[0]:4.1f}, {self.camera_sprites.position[1]:4.1f}"
145        arcade.draw_text(text, 10, 10, arcade.color.BLACK_BEAN, 20)
146
147    def on_key_press(self, key, modifiers):
148        """Called whenever a key is pressed. """
149
150        if key == arcade.key.UP:
151            self.player_sprite.change_y = PLAYER_MOVEMENT_SPEED
152        elif key == arcade.key.DOWN:
153            self.player_sprite.change_y = -PLAYER_MOVEMENT_SPEED
154        elif key == arcade.key.LEFT:
155            self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
156        elif key == arcade.key.RIGHT:
157            self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
158
159    def on_key_release(self, key, modifiers):
160        """Called when the user releases a key. """
161
162        if key == arcade.key.UP or key == arcade.key.DOWN:
163            self.player_sprite.change_y = 0
164        elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
165            self.player_sprite.change_x = 0
166
167    def on_update(self, delta_time):
168        """ Movement and game logic """
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        """
181
182        # Scroll to the proper location
183        position = Vec2(self.player_sprite.center_x - self.width / 2,
184                        self.player_sprite.center_y - self.height / 2)
185        self.camera_sprites.move_to(position, CAMERA_SPEED)
186
187    def on_resize(self, width: int, height: int):
188        """
189        Resize window
190        Handle the user grabbing the edge and resizing the window.
191        """
192        self.camera_sprites.resize(width, height)
193        self.camera_gui.resize(width, height)
194
195
196def main():
197    """ Main function """
198    window = MyGame(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, SCREEN_TITLE)
199    window.setup()
200    arcade.run()
201
202
203if __name__ == "__main__":
204    main()