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