-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.py
More file actions
92 lines (78 loc) · 3.05 KB
/
grid.py
File metadata and controls
92 lines (78 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from tkinter import Tk, BOTH, Canvas
#from window import Line, Point, Window
class Window():
def __init__(self, width, height):
self.root= Tk()
self.root.minsize(width, height)
self.root.maxsize(width, height)
self.root.title("testni naslov")
self.widget = Canvas(self.root, bg = "white", height = height, width = width)
self.widget.pack(fill = BOTH, expand = 1)
self.running = False
self.root.protocol("WM_DELETE_WINDOW", self.close)
def redraw(self):
self.widget.update_idletasks()
self.widget.update()
def wait_for_close(self):
self.running = True
while self.running == True:
self.redraw()
print("window closed")
def close(self):
self.running = False
def draw_line(self, line, fill_color):
line.draw(self.widget, fill_color)
class Point():
def __init__(self, x, y):
self.x = x
self.y = y
class Line():
def __init__(self, point1, point2):
self.point1 = point1
self.point2 = point2
def draw(self, canvas, fill_color):
canvas.create_line(
self.point1.x, self.point1.y,
self.point2.x, self.point2.y,
fill = fill_color, width = 2
)
class Cell():
def __init__(self, window = None):
self.has_left_wall = True
self.has_right_wall = True
self.has_top_wall = True
self.has_bottom_wall = True
self.visited = False
self._x1 = -1
self._x2 = -1
self._y1 = -1
self._y2 = -1
self._win = window
def draw(self, x1, y1, x2, y2):
self._x1 = x1
self._y1 = y1
self._x2 = x2
self._y2 = y2
if self.has_left_wall:
self._win.draw_line(Line(Point(self._x1, self._y1), Point(self._x1, self._y2)), "black")
else:
self._win.draw_line(Line(Point(self._x1, self._y1), Point(self._x1, self._y2)), "white")
if self.has_right_wall:
self._win.draw_line(Line(Point(self._x2, self._y1), Point(self._x2, self._y2)), "black")
else:
self._win.draw_line(Line(Point(self._x2, self._y1), Point(self._x2, self._y2)), "white")
if self.has_top_wall:
self._win.draw_line(Line(Point(self._x1, self._y1), Point(self._x2, self._y1)), "black")
else:
self._win.draw_line(Line(Point(self._x1, self._y1), Point(self._x2, self._y1)), "white")
if self.has_bottom_wall:
self._win.draw_line(Line(Point(self._x1, self._y2), Point(self._x2, self._y2)), "black")
else:
self._win.draw_line(Line(Point(self._x1, self._y2), Point(self._x2, self._y2)), "white")
def draw_move(self, to_cell, undo = False):
colour = "gray"
if undo == False:
colour = "red"
self._win.draw_line(Line(Point(((self._x1 + self._x2)/2), (self._y1 + self._y2)/2),
Point((to_cell._x1 + to_cell._x2)/2, (to_cell._y1 + to_cell._y2)/2)),
colour)