迷你地图#

这个例子展示了如何使用帧缓冲区创建一个‘迷你贴图’。
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 viewport = (0, 0, DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT)
65 self.camera_sprites = arcade.camera.Camera2D.from_raw_data(viewport=viewport)
66 self.camera_gui = arcade.camera.Camera2D.from_raw_data(viewport=viewport)
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(":resources:images/animated_characters/female_person/"
77 "femalePerson_idle.png",
78 scale=0.4)
79 self.player_sprite.center_x = 256
80 self.player_sprite.center_y = 512
81 self.player_list.append(self.player_sprite)
82
83 # -- Set up several columns of walls
84 for x in range(0, MAP_WIDTH, 210):
85 for y in range(0, MAP_HEIGHT, 64):
86 # Randomly skip a box so the player can find a way through
87 if random.randrange(5) > 0:
88 wall = arcade.Sprite(":resources:images/tiles/grassCenter.png", scale=SPRITE_SCALING)
89 wall.center_x = x
90 wall.center_y = y
91 self.wall_list.append(wall)
92
93 self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.wall_list)
94
95 # Set the background color
96 self.background_color = arcade.color.AMAZON
97
98 # Construct the minimap
99 size = (MINIMAP_WIDTH, MINIMAP_HEIGHT)
100 self.minimap_texture = arcade.Texture.create_empty(str(uuid4()), size)
101 self.minimap_sprite = arcade.Sprite(
102 self.minimap_texture,
103 center_x=MINIMAP_WIDTH / 2,
104 center_y=self.height - MINIMAP_HEIGHT / 2,
105 )
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(color=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 = (self.player_sprite.center_x, self.player_sprite.center_y)
183 self.camera_sprites.position = arcade.math.lerp_2d(self.camera_sprites.position, position, CAMERA_SPEED)
184
185 def on_resize(self, width: int, height: int):
186 """
187 Resize window
188 Handle the user grabbing the edge and resizing the window.
189 """
190 super().on_resize(width, height)
191 self.camera_sprites.match_screen(and_projection=True)
192 self.camera_gui.match_screen(and_projection=True)
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()