checkpoint: trying to move all drawing functions to Graphics
This commit is contained in:
parent
4c7e50ec39
commit
429990d12e
6 changed files with 98 additions and 95 deletions
13
app.py
13
app.py
|
@ -1,4 +1,4 @@
|
||||||
from tkinter import ttk, Tk, Canvas, StringVar, BooleanVar
|
from tkinter import ttk, Tk, StringVar, BooleanVar
|
||||||
from maze import Maze
|
from maze import Maze
|
||||||
from solver import Solver
|
from solver import Solver
|
||||||
from graphics import Graphics
|
from graphics import Graphics
|
||||||
|
@ -26,15 +26,8 @@ class App(Tk):
|
||||||
self.columnconfigure(0, weight=1)
|
self.columnconfigure(0, weight=1)
|
||||||
self.columnconfigure(1, weight=1)
|
self.columnconfigure(1, weight=1)
|
||||||
|
|
||||||
self.canvas = Canvas(self)
|
self.graphics = Graphics(self)
|
||||||
self.canvas.config(
|
self.graphics.grid(column=1, row=0)
|
||||||
bg=self.background_colour,
|
|
||||||
width=800,
|
|
||||||
height=800
|
|
||||||
)
|
|
||||||
self.canvas.grid(column=1, row=0)
|
|
||||||
|
|
||||||
self.graphics = Graphics(self.canvas)
|
|
||||||
|
|
||||||
self.maze = Maze(
|
self.maze = Maze(
|
||||||
x_position=10,
|
x_position=10,
|
||||||
|
|
72
cell.py
72
cell.py
|
@ -1,7 +1,6 @@
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from tkinter import Canvas
|
from line import Point, Line
|
||||||
from graphics import Graphics, Point, Line
|
|
||||||
import errors
|
import errors
|
||||||
|
|
||||||
|
|
||||||
|
@ -22,17 +21,45 @@ class CellWall:
|
||||||
a Cell's wall.
|
a Cell's wall.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, line: Line, graphics: Graphics) -> None:
|
def __init__(self, line: Line) -> None:
|
||||||
self.exists = True
|
self._exists = True
|
||||||
self.line = line
|
self._line = line
|
||||||
self._graphics = graphics
|
self._line_colour = "black"
|
||||||
|
|
||||||
def draw(self):
|
def configure(self, build: bool) -> None:
|
||||||
fill_colour = "black"
|
if build:
|
||||||
if not self.exists:
|
self._build_wall()
|
||||||
fill_colour = "white"
|
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:
|
class Cell:
|
||||||
|
@ -44,7 +71,7 @@ class Cell:
|
||||||
self,
|
self,
|
||||||
x1: int, y1: int,
|
x1: int, y1: int,
|
||||||
x2: int, y2: int,
|
x2: int, y2: int,
|
||||||
graphics: Graphics = None,
|
graphics=None,
|
||||||
) -> None:
|
) -> None:
|
||||||
# Validation
|
# Validation
|
||||||
if (x2 < x1) or (y2 < y1):
|
if (x2 < x1) or (y2 < y1):
|
||||||
|
@ -63,10 +90,10 @@ class Cell:
|
||||||
right_wall = Line(Point(x2, y1), Point(x2, y2))
|
right_wall = Line(Point(x2, y1), Point(x2, y2))
|
||||||
|
|
||||||
self._walls: Dict[CellWallLabels, CellWall] = {
|
self._walls: Dict[CellWallLabels, CellWall] = {
|
||||||
CellWallLabels.TOP: CellWall(top_wall, graphics),
|
CellWallLabels.TOP: CellWall(top_wall),
|
||||||
CellWallLabels.BOTTOM: CellWall(bottom_wall, graphics),
|
CellWallLabels.BOTTOM: CellWall(bottom_wall),
|
||||||
CellWallLabels.LEFT: CellWall(left_wall, graphics),
|
CellWallLabels.LEFT: CellWall(left_wall),
|
||||||
CellWallLabels.RIGHT: CellWall(right_wall, graphics),
|
CellWallLabels.RIGHT: CellWall(right_wall),
|
||||||
}
|
}
|
||||||
|
|
||||||
# Calculate the cell's central point
|
# Calculate the cell's central point
|
||||||
|
@ -92,13 +119,13 @@ class Cell:
|
||||||
configure_walls configures the existence of the Cell's walls.
|
configure_walls configures the existence of the Cell's walls.
|
||||||
"""
|
"""
|
||||||
if top is not None:
|
if top is not None:
|
||||||
self._walls[CellWallLabels.TOP].exists = top
|
self._walls[CellWallLabels.TOP].configure(top)
|
||||||
if bottom is not None:
|
if bottom is not None:
|
||||||
self._walls[CellWallLabels.BOTTOM].exists = bottom
|
self._walls[CellWallLabels.BOTTOM].configure(bottom)
|
||||||
if left is not None:
|
if left is not None:
|
||||||
self._walls[CellWallLabels.LEFT].exists = left
|
self._walls[CellWallLabels.LEFT].configure(left)
|
||||||
if right is not None:
|
if right is not None:
|
||||||
self._walls[CellWallLabels.RIGHT].exists = right
|
self._walls[CellWallLabels.RIGHT].configure(right)
|
||||||
|
|
||||||
def centre(self) -> Point:
|
def centre(self) -> Point:
|
||||||
"""
|
"""
|
||||||
|
@ -114,7 +141,7 @@ class Cell:
|
||||||
raise TypeError(
|
raise TypeError(
|
||||||
"The argument does not appear to be a valid cell wall."
|
"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:
|
def draw(self) -> None:
|
||||||
"""
|
"""
|
||||||
|
@ -123,8 +150,7 @@ class Cell:
|
||||||
if not self._graphics:
|
if not self._graphics:
|
||||||
return
|
return
|
||||||
|
|
||||||
for label in CellWallLabels:
|
self._graphics.draw_cell_walls(self._walls)
|
||||||
self._walls[label].draw()
|
|
||||||
|
|
||||||
def draw_move(self, to_cell: 'Cell', undo: bool = False) -> None:
|
def draw_move(self, to_cell: 'Cell', undo: bool = False) -> None:
|
||||||
"""
|
"""
|
||||||
|
|
79
graphics.py
79
graphics.py
|
@ -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:
|
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 redraw(self) -> None:
|
||||||
"""
|
"""
|
||||||
Point represents the position of a point.
|
redraw redraws all the graphics in the window.
|
||||||
"""
|
"""
|
||||||
|
self.update_idletasks()
|
||||||
def __init__(self, x: int, y: int):
|
self.update()
|
||||||
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 draw_line(
|
def draw_line(
|
||||||
self,
|
self,
|
||||||
|
@ -34,45 +29,19 @@ class Graphics:
|
||||||
"""
|
"""
|
||||||
draws a line onto the canvas.
|
draws a line onto the canvas.
|
||||||
"""
|
"""
|
||||||
self._canvas.create_line(
|
self.create_line(
|
||||||
line.point_a.x, line.point_a.y,
|
line.point_a.x, line.point_a.y,
|
||||||
line.point_b.x, line.point_b.y,
|
line.point_b.x, line.point_b.y,
|
||||||
fill=fill_colour,
|
fill=fill_colour,
|
||||||
width=width,
|
width=width,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def draw_cell_walls(self, walls: Dict[CellWallLabels, CellWall]) -> None:
|
||||||
class Window:
|
|
||||||
"""
|
"""
|
||||||
Window is a Graphical window.
|
draws the walls of a cell onto the canvas.
|
||||||
"""
|
"""
|
||||||
|
for label in CellWallLabels:
|
||||||
def __init__(self, width: int, height: int) -> None:
|
self.draw_line(
|
||||||
self._root = Tk()
|
line=walls[label].get_line(),
|
||||||
self._root.title("Maze Solver")
|
fill_colour=walls[label].get_line_colour(),
|
||||||
|
|
||||||
# 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:
|
|
||||||
"""
|
|
||||||
redraw redraws all the graphics in the window.
|
|
||||||
"""
|
|
||||||
self._root.update_idletasks()
|
|
||||||
self._root.update()
|
|
||||||
|
|
18
line.py
Normal file
18
line.py
Normal 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
|
3
main.py
3
main.py
|
@ -1,6 +1,3 @@
|
||||||
from graphics import Window
|
|
||||||
from maze import Maze
|
|
||||||
from solver import Solver
|
|
||||||
from app import App
|
from app import App
|
||||||
|
|
||||||
|
|
||||||
|
|
4
maze.py
4
maze.py
|
@ -284,8 +284,8 @@ class Maze:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self._cell_grid[i][j].draw()
|
self._cell_grid[i][j].draw()
|
||||||
#self._canvas.redraw()
|
self._graphics.redraw()
|
||||||
#sleep(0.05)
|
sleep(0.05)
|
||||||
|
|
||||||
def _draw_path(self, current_cell: Cell, next_cell: Cell, undo: bool = False) -> None:
|
def _draw_path(self, current_cell: Cell, next_cell: Cell, undo: bool = False) -> None:
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in a new issue