checkpoint: trying to move all drawing functions to Graphics

This commit is contained in:
Dan Anglin 2024-02-17 21:25:03 +00:00
parent 4c7e50ec39
commit 429990d12e
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
6 changed files with 98 additions and 95 deletions

13
app.py
View file

@ -1,4 +1,4 @@
from tkinter import ttk, Tk, Canvas, StringVar, BooleanVar
from tkinter import ttk, Tk, StringVar, BooleanVar
from maze import Maze
from solver import Solver
from graphics import Graphics
@ -26,15 +26,8 @@ class App(Tk):
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=1)
self.canvas = Canvas(self)
self.canvas.config(
bg=self.background_colour,
width=800,
height=800
)
self.canvas.grid(column=1, row=0)
self.graphics = Graphics(self.canvas)
self.graphics = Graphics(self)
self.graphics.grid(column=1, row=0)
self.maze = Maze(
x_position=10,

72
cell.py
View file

@ -1,7 +1,6 @@
from typing import Dict
from enum import Enum
from tkinter import Canvas
from graphics import Graphics, Point, Line
from line import Point, Line
import errors
@ -22,17 +21,45 @@ class CellWall:
a Cell's wall.
"""
def __init__(self, line: Line, graphics: Graphics) -> None:
self.exists = True
self.line = line
self._graphics = graphics
def __init__(self, line: Line) -> None:
self._exists = True
self._line = line
self._line_colour = "black"
def draw(self):
fill_colour = "black"
if not self.exists:
fill_colour = "white"
def configure(self, build: bool) -> None:
if build:
self._build_wall()
else:
self._destroy_wall()
self._graphics.draw_line(self.line, fill_colour=fill_colour)
def _build_wall(self) -> None:
"""
builds the cell wall
"""
self._exists = True
self._line_colour = "black"
def _destroy_wall(self) -> None:
"""
destroys the cell wall
"""
self._exists = False
self._line_colour = "white"
def wall_up(self) -> bool:
"""
returns true if the cell wall is up.
"""
return self._exists
def get_line(self) -> Line:
return self._line
def get_line_colour(self) -> str:
"""
returns the line colour of the wall.
"""
return self._line_colour
class Cell:
@ -44,7 +71,7 @@ class Cell:
self,
x1: int, y1: int,
x2: int, y2: int,
graphics: Graphics = None,
graphics=None,
) -> None:
# Validation
if (x2 < x1) or (y2 < y1):
@ -63,10 +90,10 @@ class Cell:
right_wall = Line(Point(x2, y1), Point(x2, y2))
self._walls: Dict[CellWallLabels, CellWall] = {
CellWallLabels.TOP: CellWall(top_wall, graphics),
CellWallLabels.BOTTOM: CellWall(bottom_wall, graphics),
CellWallLabels.LEFT: CellWall(left_wall, graphics),
CellWallLabels.RIGHT: CellWall(right_wall, graphics),
CellWallLabels.TOP: CellWall(top_wall),
CellWallLabels.BOTTOM: CellWall(bottom_wall),
CellWallLabels.LEFT: CellWall(left_wall),
CellWallLabels.RIGHT: CellWall(right_wall),
}
# Calculate the cell's central point
@ -92,13 +119,13 @@ class Cell:
configure_walls configures the existence of the Cell's walls.
"""
if top is not None:
self._walls[CellWallLabels.TOP].exists = top
self._walls[CellWallLabels.TOP].configure(top)
if bottom is not None:
self._walls[CellWallLabels.BOTTOM].exists = bottom
self._walls[CellWallLabels.BOTTOM].configure(bottom)
if left is not None:
self._walls[CellWallLabels.LEFT].exists = left
self._walls[CellWallLabels.LEFT].configure(left)
if right is not None:
self._walls[CellWallLabels.RIGHT].exists = right
self._walls[CellWallLabels.RIGHT].configure(right)
def centre(self) -> Point:
"""
@ -114,7 +141,7 @@ class Cell:
raise TypeError(
"The argument does not appear to be a valid cell wall."
)
return self._walls[wall].exists
return self._walls[wall].wall_up()
def draw(self) -> None:
"""
@ -123,8 +150,7 @@ class Cell:
if not self._graphics:
return
for label in CellWallLabels:
self._walls[label].draw()
self._graphics.draw_cell_walls(self._walls)
def draw_move(self, to_cell: 'Cell', undo: bool = False) -> None:
"""

View file

@ -1,29 +1,24 @@
from tkinter import Tk, Canvas
from typing import Dict
from tkinter import Canvas
from line import Line
from cell import CellWallLabels, CellWall
class Point:
"""
Point represents the position of a point.
"""
class Graphics(Canvas):
def __init__(self, container, background="white", width=800, height=800) -> None:
super().__init__(container)
self.config(
bg=background,
width=width,
height=height,
)
def __init__(self, x: int, y: int):
self.x = x
self.y = y
class Line:
"""
Line represents a graphical line.
"""
def __init__(self, point_a: Point, point_b: Point) -> None:
self.point_a = point_a
self.point_b = point_b
class Graphics:
def __init__(self, canvas: Canvas) -> None:
self._canvas = canvas
def redraw(self) -> None:
"""
redraw redraws all the graphics in the window.
"""
self.update_idletasks()
self.update()
def draw_line(
self,
@ -34,45 +29,19 @@ class Graphics:
"""
draws a line onto the canvas.
"""
self._canvas.create_line(
self.create_line(
line.point_a.x, line.point_a.y,
line.point_b.x, line.point_b.y,
fill=fill_colour,
width=width,
)
class Window:
"""
Window is a Graphical window.
"""
def __init__(self, width: int, height: int) -> None:
self._root = Tk()
self._root.title("Maze Solver")
# Position the window to the centre of the screen
screen_width = self._root.winfo_screenwidth()
screen_height = self._root.winfo_screenheight()
centre_x = int(screen_width/2 - width/2)
centre_y = int(screen_height/2 - height/2)
self._root.geometry(f"{width}x{height}+{centre_x}+{centre_y}")
# Styling
self.background_colour = "white"
self.cell_grid_colour = "black"
self._canvas = Canvas(self._root)
self._canvas.config(
bg=self.background_colour,
height=height,
width=width,
)
self._canvas.pack()
def redraw(self) -> None:
def draw_cell_walls(self, walls: Dict[CellWallLabels, CellWall]) -> None:
"""
redraw redraws all the graphics in the window.
draws the walls of a cell onto the canvas.
"""
self._root.update_idletasks()
self._root.update()
for label in CellWallLabels:
self.draw_line(
line=walls[label].get_line(),
fill_colour=walls[label].get_line_colour(),
)

18
line.py Normal file
View file

@ -0,0 +1,18 @@
class Point:
"""
Point represents the position of a point.
"""
def __init__(self, x: int, y: int):
self.x = x
self.y = y
class Line:
"""
Line represents a graphical line.
"""
def __init__(self, point_a: Point, point_b: Point) -> None:
self.point_a = point_a
self.point_b = point_b

View file

@ -1,6 +1,3 @@
from graphics import Window
from maze import Maze
from solver import Solver
from app import App

View file

@ -284,8 +284,8 @@ class Maze:
"""
self._cell_grid[i][j].draw()
#self._canvas.redraw()
#sleep(0.05)
self._graphics.redraw()
sleep(0.05)
def _draw_path(self, current_cell: Cell, next_cell: Cell, undo: bool = False) -> None:
"""