feat: add the Cell class

Add the Cell class to represent a grid in the maze. Each 'Cell' can
be configured to specify which walls exists for it.
This commit is contained in:
Dan Anglin 2024-02-12 23:32:03 +00:00
parent 340eec773d
commit 14c6f659ff
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 55 additions and 8 deletions

View file

@ -75,7 +75,7 @@ class Window:
while self.__is_running:
self.redraw()
def draw_line(self, line: Line, fill_colour: str):
def draw_line(self, line: Line, fill_colour: str = "black"):
"""
draw_line draws a line on the canvas.
"""
@ -86,3 +86,41 @@ class Window:
close sets the window to close.
"""
self.__is_running = False
class Cell:
"""
A Cell represents a grid on the maze.
"""
def __init__(
self,
x1: int, y1: int,
x2: int, y2: int,
window: Window
):
self.__top_wall_exists = True
self.__bottom_wall_exists = True
self.__left_wall_exists = True
self.__right_wall_exists = True
self.__top_wall = Line(Point(x1, y1), Point(x2, y1))
self.__bottom_wall = Line(Point(x1, y2), Point(x2, y2))
self.__left_wall = Line(Point(x1, y1), Point(x1, y2))
self.__right_wall = Line(Point(x2, y1), Point(x2, y2))
self.__window = window
def set_walls(self, top: bool, bottom: bool, left: bool, right: bool):
self.__top_wall_exists = top
self.__bottom_wall_exists = bottom
self.__left_wall_exists = left
self.__right_wall_exists = right
def draw(self):
if self.__top_wall_exists:
self.__window.draw_line(self.__top_wall)
if self.__bottom_wall_exists:
self.__window.draw_line(self.__bottom_wall)
if self.__left_wall_exists:
self.__window.draw_line(self.__left_wall)
if self.__right_wall_exists:
self.__window.draw_line(self.__right_wall)

23
main.py
View file

@ -1,14 +1,23 @@
from graphics import Window, Line, Point
from graphics import Window, Cell
def main():
window = Window(800, 800)
line_1 = Line(Point(10, 50), Point(100, 70))
line_2 = Line(Point(300, 145), Point(456, 200))
line_3 = Line(Point(20, 498), Point(50, 600))
window.draw_line(line_1, "black")
window.draw_line(line_2, "red")
window.draw_line(line_3, "black")
# Draw all walls
cell_1 = Cell(10, 30, 100, 100, window)
cell_1.draw()
# Draw vertical walls only
cell_2 = Cell(300, 50, 400, 100, window)
cell_2.set_walls(False, False, True, True)
cell_2.draw()
# Draw horizontal walls only
cell_3 = Cell(200, 400, 500, 600, window)
cell_3.set_walls(True, True, False, False)
cell_3.draw()
window.wait_for_close()