-
Notifications
You must be signed in to change notification settings - Fork 5
/
grid.py
146 lines (115 loc) · 4.16 KB
/
grid.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/python3
from tkinter import *
import numpy as np
from world_gen import *
import os
class Cell():
EMPTY_COLOR_BG = "white"
EMPTY_COLOR_BORDER = "black"
def __init__(self, master, x, y, size):
#Constructor of the object called by Cell
self.master = master
self.abs = x
self.ord = y
self.size= size
self.fill= 0
def _switch(self):
#Switch the cell fill color
self.fill+=1
def draw(self):
#Fill selected cell on grid canvas
if self.master != None :
if self.fill%3==1:
fill = "dark blue"
outline = "dark blue"
elif self.fill%3==2:
fill = "blue"
outline = "blue"
else:
fill = "white"
outline = "black"
xmin = self.abs * self.size
xmax = xmin + self.size
ymin = self.ord * self.size
ymax = ymin + self.size
self.master.create_rectangle(xmin, ymin, xmax, ymax, fill = fill, outline = outline)
def drawinitial(self):
if self.master != None :
if not self.fill:
fill = Cell.EMPTY_COLOR_BG
outline = Cell.EMPTY_COLOR_BORDER
xmin = self.abs * self.size
xmax = xmin + self.size
ymin = self.ord * self.size
ymax = ymin + self.size
self.master.create_rectangle(xmin, ymin, xmax, ymax, fill = fill, outline = outline)
class CellGrid(Canvas):
def __init__(self,master, rowNumber, columnNumber, cellSize, *args, **kwargs):
Canvas.__init__(self, master, width = cellSize * columnNumber , height = cellSize * rowNumber, *args, **kwargs)
self.cellSize = cellSize
self.totalRows = rowNumber
self.totalColumns = columnNumber
self.CompleteGrid = np.zeros((rowNumber,columnNumber))
self.grid = []
for row in range(rowNumber):
line = []
for column in range(columnNumber):
line.append(Cell(self, column, row, cellSize))
self.grid.append(line)
#memorize the cells that have been modified to avoid many switching of state during mouse motion.
self.switched = []
#bind click action
self.bind("<Button-1>", self.handleMouseClick)
#bind moving while clicking
# self.bind("<B1-Motion>", self.handleMouseMotion)
#bind release button action - clear the memory of midified cells.
#self.bind("<ButtonRelease-1>", lambda event: self.switched.clear())
self.drawinitial()
def draw(self):
for row in self.grid:
for cell in row:
cell.draw()
def drawinitial(self):
for row in self.grid:
for cell in row:
cell.drawinitial()
def _eventCoords(self, event):
row = int(event.y / self.cellSize)
column = int(event.x / self.cellSize)
return row, column
def handleMouseClick(self, event):
row, column = self._eventCoords(event)
self.setRoad(row,column)
cell = self.grid[row][column]
cell._switch()
cell.draw()
#add the cell to the list of cell switched during the click
self.switched.append(cell)
"""
def handleMouseMotion(self, event):
row, column = self._eventCoords(event)
cell = self.grid[row][column]
if cell not in self.switched:
cell._switch()
cell.draw()
self.switched.append(cell)
"""
def printgrid(self):
for row in self.CompleteGrid:
print(" ".join(str(int(i)) for i in row))
def setRoad(self,row,col):
if self.CompleteGrid[row][col] == 1:
self.CompleteGrid[row][col] = 2
elif self.CompleteGrid[row][col] == 2:
self.CompleteGrid[row][col] = 0
else:
self.CompleteGrid[row][col] = 1
#if __name__ == "__main__" :
def OpenGrid(w):
app = Tk()
grid = CellGrid(app,w.rows,w.cols,25)
grid.pack()
Button(app,text="Generate Roadmap",command=app.destroy).pack()
app.mainloop()
grid.printgrid()
worldGenerator(grid,w)