-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualiser.py
More file actions
67 lines (56 loc) · 2.3 KB
/
visualiser.py
File metadata and controls
67 lines (56 loc) · 2.3 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
from tkinter import *
class window():
def __init__(self, window_x, window_y):
self.root = Tk()
self.root.title("labyrinth visualiser")
self.root.geometry(f"{window_x}x{window_y}")
self.my_canvas = Canvas(self.root, width=window_x, height=window_y, bg="grey")
self.my_canvas.pack()
self.my_canvas.bind("<Button-1>", self.switch_state)
self.my_canvas.focus_set()
self.my_canvas.bind("<p>", self.printmap)
self.my_canvas.bind("<b>", self.map_black)
self.my_canvas.bind("<w>", self.map_white)
self.my_canvas.bind("<g>", self.gradiant)
self.my_canvas.bind("<q>", self.quit)
return self.root
def draw_rectangle(self, x: int, y: int, color:str, outline: str = "black"):
self.my_canvas.create_rectangle(x * self.pixel_size,
y * self.pixel_size, (x + 1) * self.pixel_size,
(y + 1) * self.pixel_size, fill=color, outline=outline)
def draw_map(self, map):
for y in range(len(map)):
for x in range(len(map[y])):
if map[y][x] == "X":
self.draw_rectangle(x, y, "black")
elif map[y][x] == "o":
self.draw_rectangle(x, y, "red", "red")
else:
self.draw_rectangle(x, y, "white")
def quit(self, event):
self.my_canvas.quit()
def switch_state(self, event):
x = event.x // self.pixel_size
y = event.y // self.pixel_size
if self.map[y][x] == "X":
self.draw_rectangle(x, y, "white")
self.map[y][x] = '*'
else:
self.draw_rectangle(x, y, "black")
self.map[y][x] = 'X'
def printmap(self, event):
for line in self.map:
print("".join(line), end="\n")
def map_black(self, event):
self.set_map("X", "black")
def map_white(self, event):
self.set_map("*", "white")
def gradiant(self, event):
for y in range(len(self.map)):
for x in range(len(self.map[y])):
if self.map[y][x] == "*":
self.draw_rectangle(x, y,
rgb_to_hex((int((x+y) / (self.x + self.y) * 255),
int((1 - (x+y) / (self.x + self.y)) * 255), 255)))
def rgb_to_hex(rgb):
return '#%02x%02x%02x' % rgb