From 45a003270a842a19c539b85e84b50e7830ac7579 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Tue, 13 Feb 2024 10:48:26 +0000 Subject: [PATCH] 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. --- cell.py | 38 ++++++++++++++++++++++++++++++++++---- main.py | 15 ++++++++------- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/cell.py b/cell.py index 47b19d0..97eb636 100644 --- a/cell.py +++ b/cell.py @@ -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: """ diff --git a/main.py b/main.py index 7c972b3..478667f 100644 --- a/main.py +++ b/main.py @@ -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()