feat: add draw_move method to Cell

Add the draw_move method to the Cell class to draw a path between the
centre of two cells.
This commit is contained in:
Dan Anglin 2024-02-13 10:48:26 +00:00
parent a19d9260d2
commit 45a003270a
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 42 additions and 11 deletions

38
cell.py
View file

@ -12,10 +12,23 @@ class Cell:
x2: int, y2: int,
window: Window
):
self.__top_wall = CellWall(Line(Point(x1, y1), Point(x2, y1)))
self.__bottom_wall = CellWall(Line(Point(x1, y2), Point(x2, y2)))
self.__left_wall = CellWall(Line(Point(x1, y1), Point(x1, y2)))
self.__right_wall = CellWall(Line(Point(x2, y1), Point(x2, y2)))
# Define the cell walls
top_wall = Line(Point(x1, y1), Point(x2, y1))
bottom_wall = Line(Point(x1, y2), Point(x2, y2))
left_wall = Line(Point(x1, y1), Point(x1, y2))
right_wall = Line(Point(x2, y1), Point(x2, y2))
self.__top_wall = CellWall(top_wall)
self.__bottom_wall = CellWall(bottom_wall)
self.__left_wall = CellWall(left_wall)
self.__right_wall = CellWall(right_wall)
# Calculate the cell's central point
centre_x = x1 + ((x2 - x1) / 2)
centre_y = y1 + ((y2 - y1) / 2)
self.__centre = Point(centre_x, centre_y)
# A reference to the root Window class for drawing purposes.
self.__window = window
def configure_walls(
@ -33,6 +46,12 @@ class Cell:
self.__left_wall.exists = left
self.__right_wall.exists = right
def centre(self) -> Point:
"""
centre returns the Cell's central point
"""
return self.__centre
def draw(self):
"""
draw draws the cell onto the canvas
@ -46,6 +65,17 @@ class Cell:
if self.__right_wall.exists:
self.__window.draw_line(self.__right_wall.line)
def draw_move(self, to_cell: 'Cell', undo: bool = False):
"""
draw_move draws a path between the centre of this cell and
the centre of the given cell.
"""
fill_colour = "red"
if undo:
fill_colour = "grey"
line = Line(self.centre(), to_cell.centre())
self.__window.draw_line(line, fill_colour)
class CellWall:
"""

15
main.py
View file

@ -5,20 +5,21 @@ from cell import Cell
def main():
window = Window(800, 800)
# Draw all walls
cell_1 = Cell(10, 30, 100, 100, window)
cell_1.configure_walls(top=True, bottom=True, left=True, right=False)
cell_1.draw()
# Draw vertical walls only
cell_2 = Cell(300, 50, 400, 100, window)
cell_2.configure_walls(top=False, bottom=False, left=True, right=True)
cell_2 = Cell(210, 30, 300, 100, window)
cell_2.configure_walls(top=True, bottom=False, left=False, right=True)
cell_2.draw()
# Draw horizontal walls only
cell_3 = Cell(200, 400, 500, 600, window)
cell_3.configure_walls(top=True, bottom=True, left=False, right=False)
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)
window.wait_for_close()