Creating a Depth First Maze#

Screen shot of a maze created by depth first
maze_depth_first.py#
  1"""
  2Create a maze using a depth-first search maze generation algorithm.
  3For more information on this algorithm see:
  4https://www.algosome.com/articles/maze-generation-depth-first.html
  5...or search up some other examples.
  6
  7Artwork from https://kenney.nl
  8
  9If Python and Arcade are installed, this example can be run from the command line with:
 10python -m arcade.examples.maze_depth_first
 11"""
 12import random
 13import arcade
 14import timeit
 15
 16NATIVE_SPRITE_SIZE = 128
 17SPRITE_SCALING = 0.25
 18SPRITE_SIZE = int(NATIVE_SPRITE_SIZE * SPRITE_SCALING)
 19
 20SCREEN_WIDTH = 1000
 21SCREEN_HEIGHT = 700
 22SCREEN_TITLE = "Maze Depth First Example"
 23
 24MOVEMENT_SPEED = 8
 25
 26TILE_EMPTY = 0
 27TILE_CRATE = 1
 28
 29# Maze must have an ODD number of rows and columns.
 30# Walls go on EVEN rows/columns.
 31# Openings go on ODD rows/columns
 32MAZE_HEIGHT = 51
 33MAZE_WIDTH = 51
 34
 35MERGE_SPRITES = True
 36
 37# How many pixels to keep as a minimum margin between the character
 38# and the edge of the screen.
 39VIEWPORT_MARGIN = 200
 40
 41
 42def _create_grid_with_cells(width, height):
 43    """ Create a grid with empty cells on odd row/column combinations. """
 44    grid = []
 45    for row in range(height):
 46        grid.append([])
 47        for column in range(width):
 48            if column % 2 == 1 and row % 2 == 1:
 49                grid[row].append(TILE_EMPTY)
 50            elif column == 0 or row == 0 or column == width - 1 or row == height - 1:
 51                grid[row].append(TILE_CRATE)
 52            else:
 53                grid[row].append(TILE_CRATE)
 54    return grid
 55
 56
 57def make_maze_depth_first(maze_width, maze_height):
 58    maze = _create_grid_with_cells(maze_width, maze_height)
 59
 60    w = (len(maze[0]) - 1) // 2
 61    h = (len(maze) - 1) // 2
 62    vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]
 63
 64    def walk(x: int, y: int):
 65        vis[y][x] = 1
 66
 67        d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
 68        random.shuffle(d)
 69        for (xx, yy) in d:
 70            if vis[yy][xx]:
 71                continue
 72            if xx == x:
 73                maze[max(y, yy) * 2][x * 2 + 1] = TILE_EMPTY
 74            if yy == y:
 75                maze[y * 2 + 1][max(x, xx) * 2] = TILE_EMPTY
 76
 77            walk(xx, yy)
 78
 79    walk(random.randrange(w), random.randrange(h))
 80
 81    return maze
 82
 83
 84class MyGame(arcade.Window):
 85    """ Main application class. """
 86
 87    def __init__(self, width, height, title):
 88        """
 89        Initializer
 90        """
 91        super().__init__(width, height, title)
 92
 93        # Sprite lists
 94        self.player_list = None
 95        self.wall_list = None
 96
 97        # Player info
 98        self.score = 0
 99        self.player_sprite = None
100
101        # Physics engine
102        self.physics_engine = None
103
104        # Used to scroll
105        self.view_bottom = 0
106        self.view_left = 0
107
108        # Time to process
109        self.processing_time = 0
110        self.draw_time = 0
111
112    def setup(self):
113        """ Set up the game and initialize the variables. """
114
115        # Sprite lists
116        self.player_list = arcade.SpriteList()
117        self.wall_list = arcade.SpriteList()
118
119        self.score = 0
120
121        # Create the maze
122        maze = make_maze_depth_first(MAZE_WIDTH, MAZE_HEIGHT)
123
124        # Create sprites based on 2D grid
125        if not MERGE_SPRITES:
126            # This is the simple-to-understand method. Each grid location
127            # is a sprite.
128            for row in range(MAZE_HEIGHT):
129                for column in range(MAZE_WIDTH):
130                    if maze[row][column] == 1:
131                        wall = arcade.Sprite(":resources:images/tiles/grassCenter.png", SPRITE_SCALING)
132                        wall.center_x = column * SPRITE_SIZE + SPRITE_SIZE / 2
133                        wall.center_y = row * SPRITE_SIZE + SPRITE_SIZE / 2
134                        self.wall_list.append(wall)
135        else:
136            # This uses new Arcade 1.3.1 features, that allow me to create a
137            # larger sprite with a repeating texture. So if there are multiple
138            # cells in a row with a wall, we merge them into one sprite, with a
139            # repeating texture for each cell. This reduces our sprite count.
140            for row in range(MAZE_HEIGHT):
141                column = 0
142                while column < len(maze):
143                    while column < len(maze) and maze[row][column] == 0:
144                        column += 1
145                    start_column = column
146                    while column < len(maze) and maze[row][column] == 1:
147                        column += 1
148                    end_column = column - 1
149
150                    column_count = end_column - start_column + 1
151                    column_mid = (start_column + end_column) / 2
152
153                    wall = arcade.Sprite(":resources:images/tiles/grassCenter.png", SPRITE_SCALING)
154                    wall.center_x = column_mid * SPRITE_SIZE + SPRITE_SIZE / 2
155                    wall.center_y = row * SPRITE_SIZE + SPRITE_SIZE / 2
156                    wall.width = SPRITE_SIZE * column_count
157                    self.wall_list.append(wall)
158
159        # Set up the player
160        self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/"
161                                           "femalePerson_idle.png",
162                                           SPRITE_SCALING)
163        self.player_list.append(self.player_sprite)
164
165        # Randomly place the player. If we are in a wall, repeat until we aren't.
166        placed = False
167        while not placed:
168
169            # Randomly position
170            self.player_sprite.center_x = random.randrange(MAZE_WIDTH * SPRITE_SIZE)
171            self.player_sprite.center_y = random.randrange(MAZE_HEIGHT * SPRITE_SIZE)
172
173            # Are we in a wall?
174            walls_hit = arcade.check_for_collision_with_list(self.player_sprite, self.wall_list)
175            if len(walls_hit) == 0:
176                # Not in a wall! Success!
177                placed = True
178
179        self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.wall_list)
180
181        # Set the background color
182        arcade.set_background_color(arcade.color.AMAZON)
183
184        # Set the viewport boundaries
185        # These numbers set where we have 'scrolled' to.
186        self.view_left = 0
187        self.view_bottom = 0
188
189    def on_draw(self):
190        """
191        Render the screen.
192        """
193
194        # This command has to happen before we start drawing
195        self.clear()
196
197        # Start timing how long this takes
198        draw_start_time = timeit.default_timer()
199
200        # Draw all the sprites.
201        self.wall_list.draw()
202        self.player_list.draw()
203
204        # Draw info on the screen
205        sprite_count = len(self.wall_list)
206
207        output = f"Sprite Count: {sprite_count}"
208        arcade.draw_text(output,
209                         self.view_left + 20,
210                         SCREEN_HEIGHT - 20 + self.view_bottom,
211                         arcade.color.WHITE, 16)
212
213        output = f"Drawing time: {self.draw_time:.3f}"
214        arcade.draw_text(output,
215                         self.view_left + 20,
216                         SCREEN_HEIGHT - 40 + self.view_bottom,
217                         arcade.color.WHITE, 16)
218
219        output = f"Processing time: {self.processing_time:.3f}"
220        arcade.draw_text(output,
221                         self.view_left + 20,
222                         SCREEN_HEIGHT - 60 + self.view_bottom,
223                         arcade.color.WHITE, 16)
224
225        self.draw_time = timeit.default_timer() - draw_start_time
226
227    def on_key_press(self, key, modifiers):
228        """Called whenever a key is pressed. """
229
230        if key == arcade.key.UP:
231            self.player_sprite.change_y = MOVEMENT_SPEED
232        elif key == arcade.key.DOWN:
233            self.player_sprite.change_y = -MOVEMENT_SPEED
234        elif key == arcade.key.LEFT:
235            self.player_sprite.change_x = -MOVEMENT_SPEED
236        elif key == arcade.key.RIGHT:
237            self.player_sprite.change_x = MOVEMENT_SPEED
238
239    def on_key_release(self, key, modifiers):
240        """Called when the user releases a key. """
241
242        if key == arcade.key.UP or key == arcade.key.DOWN:
243            self.player_sprite.change_y = 0
244        elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
245            self.player_sprite.change_x = 0
246
247    def on_update(self, delta_time):
248        """ Movement and game logic """
249
250        start_time = timeit.default_timer()
251
252        # Call update on all sprites (The sprites don't do much in this
253        # example though.)
254        self.physics_engine.update()
255
256        # --- Manage Scrolling ---
257
258        # Track if we need to change the viewport
259
260        changed = False
261
262        # Scroll left
263        left_bndry = self.view_left + VIEWPORT_MARGIN
264        if self.player_sprite.left < left_bndry:
265            self.view_left -= left_bndry - self.player_sprite.left
266            changed = True
267
268        # Scroll right
269        right_bndry = self.view_left + SCREEN_WIDTH - VIEWPORT_MARGIN
270        if self.player_sprite.right > right_bndry:
271            self.view_left += self.player_sprite.right - right_bndry
272            changed = True
273
274        # Scroll up
275        top_bndry = self.view_bottom + SCREEN_HEIGHT - VIEWPORT_MARGIN
276        if self.player_sprite.top > top_bndry:
277            self.view_bottom += self.player_sprite.top - top_bndry
278            changed = True
279
280        # Scroll down
281        bottom_bndry = self.view_bottom + VIEWPORT_MARGIN
282        if self.player_sprite.bottom < bottom_bndry:
283            self.view_bottom -= bottom_bndry - self.player_sprite.bottom
284            changed = True
285
286        if changed:
287            arcade.set_viewport(self.view_left,
288                                SCREEN_WIDTH + self.view_left,
289                                self.view_bottom,
290                                SCREEN_HEIGHT + self.view_bottom)
291
292        # Save the time it took to do this.
293        self.processing_time = timeit.default_timer() - start_time
294
295
296def main():
297    """ Main function """
298    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
299    window.setup()
300    arcade.run()
301
302
303if __name__ == "__main__":
304    main()