feat: add the Maze class

The Maze class stored a 2-dimensional grid of Cells. When the Maze is
initialised, the Cell's are created and drawn onto the canvas.
This commit is contained in:
Dan Anglin 2024-02-13 12:18:47 +00:00
parent 12b4e2367c
commit 30792f25b0
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 69 additions and 15 deletions

17
main.py
View file

@ -1,24 +1,11 @@
from graphics import Window
from cell import Cell
from maze import Maze
def main():
window = Window(800, 800)
cell_1 = Cell(10, 30, 100, 100, window)
cell_1.configure_walls(top=True, bottom=True, left=True, right=False)
cell_1.draw()
cell_2 = Cell(210, 30, 300, 100, window)
cell_2.configure_walls(top=True, bottom=False, left=False, right=True)
cell_2.draw()
cell_3 = Cell(210, 130, 300, 200, window)
cell_3.configure_walls(top=False, bottom=True, left=True, right=True)
cell_3.draw()
cell_1.draw_move(cell_2)
cell_2.draw_move(cell_3, undo=True)
_ = Maze(10, 10, 39, 39, 20, 20, window)
window.wait_for_close()

67
maze.py Normal file
View file

@ -0,0 +1,67 @@
from typing import List
from time import sleep
from graphics import Window
from cell import Cell
class Maze:
"""
Maze represents a two-dimensional grid of Cells.
"""
def __init__(
self,
x_position: int,
y_position: int,
num_rows: int,
num_columns: int,
cell_size_x: int,
cell_size_y: int,
window: Window,
):
self.__x_position = x_position
self.__y_position = y_position
self.__num_rows = num_rows
self.__num_columns = num_columns
self.__cell_size_x = cell_size_x
self.__cell_size_y = cell_size_y
self.__window = window
# Create the Maze's cells
self.__cells: List[List[Cell]] = [None for i in range(self.__num_rows)]
self.__create_cells()
def __create_cells(self):
cursor_x = self.__x_position
cursor_y = self.__y_position
for i in range(self.__num_rows):
column: List[Cell] = [None for j in range(self.__num_columns)]
for j in range(self.__num_columns):
cell = Cell(
cursor_x,
cursor_y,
(cursor_x + self.__cell_size_x),
(cursor_y + self.__cell_size_y),
self.__window
)
column[j] = cell
if j == self.__num_columns - 1:
cursor_x = self.__x_position
else:
cursor_x += self.__cell_size_x
self.__cells[i] = column
cursor_y += self.__cell_size_y
# Draw the maze in a dramatic way.
self.__draw_cells()
def __draw_cells(self):
for i in range(self.__num_rows):
for j in range(self.__num_columns):
self.__cells[i][j].draw()
self.__animate()
def __animate(self):
self.__window.redraw()
sleep(0.05)