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_texture
8"""
9
10import random
11from uuid import uuid4
12
13import arcade
14
15SPRITE_SCALING = 0.5
16
17WINDOW_WIDTH = 1280
18WINDOW_HEIGHT = 720
19ASPECT_RATIO = WINDOW_WIDTH / WINDOW_HEIGHT
20WINDOW_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 GameView(arcade.View):
40 """ Main application class. """
41
42 def __init__(self):
43 """
44 Initializer
45 """
46 super().__init__()
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 self.camera_sprites = arcade.camera.Camera2D()
66 self.camera_gui = arcade.camera.Camera2D()
67
68 def setup(self):
69 """ Set up the game and initialize the variables. """
70
71 # Sprite lists
72 self.player_list = arcade.SpriteList()
73 self.wall_list = arcade.SpriteList()
74
75 # Set up the player
76 self.player_sprite = arcade.Sprite(
77 ":resources:images/animated_characters/female_person/"
78 "femalePerson_idle.png",
79 scale=0.4,
80 )
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(
91 ":resources:images/tiles/grassCenter.png",
92 scale=SPRITE_SCALING,
93 )
94 wall.center_x = x
95 wall.center_y = y
96 self.wall_list.append(wall)
97
98 self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.wall_list)
99
100 # Set the background color
101 self.background_color = arcade.color.AMAZON
102
103 # Construct the minimap
104 size = (MINIMAP_WIDTH, MINIMAP_HEIGHT)
105 self.minimap_texture = arcade.Texture.create_empty(str(uuid4()), size)
106 self.minimap_sprite = arcade.Sprite(
107 self.minimap_texture,
108 center_x=MINIMAP_WIDTH / 2,
109 center_y=self.height - MINIMAP_HEIGHT / 2,
110 )
111
112 self.minimap_sprite_list = arcade.SpriteList()
113 self.minimap_sprite_list.append(self.minimap_sprite)
114
115 def update_minimap(self):
116 proj = 0, MAP_WIDTH, 0, MAP_HEIGHT
117 atlas = self.minimap_sprite_list.atlas
118 with atlas.render_into(self.minimap_texture, projection=proj) as fbo:
119 fbo.clear(color=MINIMAP_BACKGROUND_COLOR)
120 self.wall_list.draw()
121 arcade.draw_sprite(self.player_sprite)
122
123 def on_draw(self):
124 """
125 Render the screen.
126 """
127
128 # This command has to happen before we start drawing
129 self.clear()
130
131 # Select the camera we'll use to draw all our sprites
132 with self.camera_sprites.activate():
133 # Draw all the sprites.
134 self.wall_list.draw()
135 self.player_list.draw()
136
137 # Select the (unscrolled) camera for our GUI
138 with self.camera_gui.activate():
139 # Update the minimap
140 self.update_minimap()
141
142 # Draw the minimap
143 self.minimap_sprite_list.draw()
144
145 # Draw the GUI
146 arcade.draw_rect_filled(
147 arcade.rect.XYWH(self.width // 2, 20, self.width, 40),
148 color=arcade.color.ALMOND,
149 )
150
151 text = (
152 f"Scroll value: "
153 f"{self.camera_sprites.position[0]:4.1f}, "
154 f"{self.camera_sprites.position[1]:4.1f}"
155 )
156 arcade.draw_text(text, 10, 10, arcade.color.BLACK_BEAN, 20)
157
158 def on_key_press(self, key, modifiers):
159 """Called whenever a key is pressed. """
160
161 if key == arcade.key.UP:
162 self.player_sprite.change_y = PLAYER_MOVEMENT_SPEED
163 elif key == arcade.key.DOWN:
164 self.player_sprite.change_y = -PLAYER_MOVEMENT_SPEED
165 elif key == arcade.key.LEFT:
166 self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
167 elif key == arcade.key.RIGHT:
168 self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
169
170 def on_key_release(self, key, modifiers):
171 """Called when the user releases a key. """
172
173 if key == arcade.key.UP or key == arcade.key.DOWN:
174 self.player_sprite.change_y = 0
175 elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
176 self.player_sprite.change_x = 0
177
178 def on_update(self, delta_time):
179 """ Movement and game logic """
180
181 # Call update on all sprites (The sprites don't do much in this
182 # example though.)
183 self.physics_engine.update()
184
185 # Scroll the screen to the player
186 self.scroll_to_player()
187
188 def scroll_to_player(self):
189 """
190 Scroll the window to the player.
191 """
192
193 # Scroll to the proper location
194 position = (self.player_sprite.center_x, self.player_sprite.center_y)
195 self.camera_sprites.position = arcade.math.lerp_2d(
196 self.camera_sprites.position,
197 position,
198 CAMERA_SPEED,
199 )
200
201 def on_resize(self, width: int, height: int):
202 """
203 Resize window
204 Handle the user grabbing the edge and resizing the window.
205 """
206 super().on_resize(width, height)
207 self.camera_sprites.match_window(aspect=ASPECT_RATIO, projection=False)
208 self.camera_gui.match_window(aspect=ASPECT_RATIO, projection=False)
209
210
211def main():
212 """ Main function """
213 # Create a window class. This is what actually shows up on screen
214 window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE, resizable=True)
215
216 # Create and setup the GameView
217 game = GameView()
218 game.setup()
219
220 # Show GameView on screen
221 window.show_view(game)
222
223 # Start the arcade game loop
224 arcade.run()
225
226
227if __name__ == "__main__":
228 main()