|
| 1 | +""" |
| 2 | +Work with a mini-map |
| 3 | +
|
| 4 | +Artwork from https://kenney.nl |
| 5 | +
|
| 6 | +If Python and Arcade are installed, this example can be run from the command line with: |
| 7 | +python -m arcade.examples.minimap |
| 8 | +""" |
| 9 | + |
| 10 | +import random |
| 11 | +from uuid import uuid4 |
| 12 | + |
| 13 | +import arcade |
| 14 | +from pyglet.math import Vec2 |
| 15 | +from base_view import BaseView |
| 16 | + |
| 17 | +SPRITE_SCALING = 0.5 |
| 18 | + |
| 19 | +# How many pixels to keep as a minimum margin between the character |
| 20 | +# and the edge of the screen. |
| 21 | +VIEWPORT_MARGIN = 220 |
| 22 | + |
| 23 | +# How fast the camera pans to the player. 1.0 is instant. |
| 24 | +CAMERA_SPEED = 0.1 |
| 25 | + |
| 26 | +# How fast the character moves |
| 27 | +PLAYER_MOVEMENT_SPEED = 7 |
| 28 | + |
| 29 | +# Background color must include an alpha component |
| 30 | +MINIMAP_BACKGROUND_COLOR = arcade.get_four_byte_color(arcade.color.ALMOND) |
| 31 | +MINIMAP_WIDTH = 600 |
| 32 | +MINIMAP_HEIGHT = 300 |
| 33 | +MAP_WIDTH = 2048 |
| 34 | +MAP_HEIGHT = 1024 |
| 35 | +MAP_MARGIN = 5 |
| 36 | + |
| 37 | + |
| 38 | +class Minimap(BaseView): |
| 39 | + """ Main application class. """ |
| 40 | + |
| 41 | + def __init__(self, time_on_screen): |
| 42 | + super().__init__(time_on_screen) |
| 43 | + |
| 44 | + # Sprite lists |
| 45 | + self.player_list = None |
| 46 | + self.wall_list = None |
| 47 | + |
| 48 | + # Mini-map related |
| 49 | + |
| 50 | + # List of all our minimaps (there's just one) |
| 51 | + |
| 52 | + self.minimap_sprite_list = None |
| 53 | + |
| 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 | + self.physics_engine = None |
| 61 | + |
| 62 | + # Camera for sprites, and one for our GUI |
| 63 | + self.camera_sprites = arcade.Camera(self.window.width, self.window.height) |
| 64 | + self.camera_gui = arcade.Camera(self.window.width, self.window.height) |
| 65 | + |
| 66 | + self.setup() |
| 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 and y != 512: |
| 88 | + wall = arcade.Sprite(":resources:images/tiles/grassCenter.png", 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 | + arcade.set_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(center_x=(MINIMAP_WIDTH + MAP_MARGIN) / 2, |
| 102 | + center_y=(MINIMAP_HEIGHT + MAP_MARGIN) / 2, |
| 103 | + texture=self.minimap_texture) |
| 104 | + |
| 105 | + self.minimap_sprite_list = arcade.SpriteList() |
| 106 | + self.minimap_sprite_list.append(self.minimap_sprite) |
| 107 | + |
| 108 | + def update_minimap(self): |
| 109 | + proj = 0, MAP_WIDTH, 0, MAP_HEIGHT |
| 110 | + with self.minimap_sprite_list.atlas.render_into(self.minimap_texture, projection=proj) as fbo: |
| 111 | + fbo.clear(MINIMAP_BACKGROUND_COLOR) |
| 112 | + self.wall_list.draw() |
| 113 | + self.player_sprite.draw() |
| 114 | + |
| 115 | + def on_draw(self): |
| 116 | + """ |
| 117 | + Render the screen. |
| 118 | + """ |
| 119 | + |
| 120 | + # This command has to happen before we start drawing |
| 121 | + arcade.start_render() |
| 122 | + |
| 123 | + # Select the camera we'll use to draw all our sprites |
| 124 | + self.camera_sprites.use() |
| 125 | + |
| 126 | + # Draw all the sprites. |
| 127 | + self.wall_list.draw() |
| 128 | + self.player_list.draw() |
| 129 | + |
| 130 | + # Select the (unscrolled) camera for our GUI |
| 131 | + self.camera_gui.use() |
| 132 | + |
| 133 | + arcade.draw_rectangle_filled((MINIMAP_WIDTH + MAP_MARGIN) / 2, |
| 134 | + (MINIMAP_HEIGHT + MAP_MARGIN) / 2, |
| 135 | + MINIMAP_WIDTH + MAP_MARGIN, |
| 136 | + MINIMAP_HEIGHT + MAP_MARGIN, |
| 137 | + arcade.color.BLACK) |
| 138 | + # Update the minimap |
| 139 | + self.update_minimap() |
| 140 | + |
| 141 | + # Draw the minimap |
| 142 | + self.minimap_sprite_list.draw() |
| 143 | + self.draw_line_one("Create mini-maps") |
| 144 | + |
| 145 | + def on_update(self, delta_time): |
| 146 | + """ Movement and game logic """ |
| 147 | + |
| 148 | + self.total_time += delta_time |
| 149 | + if self.total_time > self.time_on_screen: |
| 150 | + |
| 151 | + if not self.window.view_list: |
| 152 | + self.window.create_views() |
| 153 | + |
| 154 | + new_view = self.window.view_list.pop(0) |
| 155 | + self.window.show_view(new_view) |
| 156 | + |
| 157 | + self.player_sprite.center_x += 5 |
| 158 | + |
| 159 | + # Scroll the screen to the player |
| 160 | + self.scroll_to_player() |
| 161 | + |
| 162 | + def scroll_to_player(self): |
| 163 | + """ |
| 164 | + Scroll the window to the player. |
| 165 | + """ |
| 166 | + |
| 167 | + # Scroll to the proper location |
| 168 | + position = Vec2(self.player_sprite.center_x - self.window.width / 2, |
| 169 | + self.player_sprite.center_y - self.window.height / 2) |
| 170 | + self.camera_sprites.move_to(position, CAMERA_SPEED) |
| 171 | + |
| 172 | + def on_resize(self, width, height): |
| 173 | + """ |
| 174 | + Resize window |
| 175 | + Handle the user grabbing the edge and resizing the window. |
| 176 | + """ |
| 177 | + self.camera_sprites.resize(int(width), int(height)) |
| 178 | + self.camera_gui.resize(int(width), int(height)) |
| 179 | + |
0 commit comments