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