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