-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcellClasses.py
More file actions
142 lines (108 loc) · 3.93 KB
/
Copy pathcellClasses.py
File metadata and controls
142 lines (108 loc) · 3.93 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
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
#The Cell class, from which grids can be created, and its subclasses, different objects for the A* section.
from tkinter import PhotoImage
class Cell:
def __init__(self, x, y, height, width, theme):
self.__xy = (x, y)
self._visited = False
self.__neighbours = self.__calculateNeighbours(height, width)
self.__edges = self.__calculateEdges()
self._passages = [] #used when checking if the sprite can move here in Maze Game
self._colour = theme['background']
self._type = 'empty' #determines which kind of cell a particular Cell object is
self._cost = 1 #cost to cross this cell
#endsub
def getxy(self):
return self.__xy
#endsub
def getVisited(self):
return self._visited
#endsub
def setVisited(self, new):
self._visited = new
#endsub
def getNeighbours(self):
return self.__neighbours
#endsub
def getEdges(self):
return self.__edges
#endsub
def getPassages(self):
return self._passages
#endsub
def getColour(self):
return self._colour
#endsub
def setColour(self, new):
self._colour = new
#endsub
def getType(self):
return self._type
#endsub
def getCost(self):
return self._cost
#endsub
def __calculateNeighbours(self, height, width):
'''Given a cell's coordinates, return a list of its neighbouring cells'''
directions = [[0,1], [1,0], [0,-1], [-1,0]]
neighbours = []
for d in directions:
neighbour = [self.__xy[0] + d[0], self.__xy[1] +d[1]]
if -1<neighbour[0] <width and -1<neighbour[1]<height:
neighbours.append(neighbour) #only add the neighbour if it falls inside the grid.
#endif
#endfor
return neighbours
#endsub
def __calculateEdges(self):
'''Calculate the coordinates of the four edges of this cell'''
edge1 = ((self.__xy[0]-0.5, self.__xy[1]-0.5), (self.__xy[0]+0.5, self.__xy[1]-0.5))
edge2 = ((self.__xy[0]-0.5, self.__xy[1]-0.5), (self.__xy[0]-0.5, self.__xy[1]+0.5))
edge3 = ((self.__xy[0]+0.5, self.__xy[1]-0.5), (self.__xy[0]+0.5, self.__xy[1]+0.5))
edge4 = ((self.__xy[0]-0.5, self.__xy[1]+0.5), (self.__xy[0]+0.5, self.__xy[1]+0.5))
edges = [edge1, edge2, edge3, edge4]
return edges
#endsub
#endclass
class StartCell(Cell):
def __init__(self, x, y, height, width, theme):
super().__init__(x, y, height, width, theme)
self._type = 'start'
self._colour = PhotoImage(file = 'startcell.png')
#endsub
#endclass
class EndCell(Cell):
def __init__(self, x, y, height, width, theme):
super().__init__(x, y, height, width, theme)
self._type = 'end'
self._colour = PhotoImage(file = 'endcell.png')
#endsub
#endclass
class Wall(Cell):
def __init__(self, x, y, height, width, theme):
super().__init__(x, y, height, width, theme)
self._type = 'wall'
self._colour = '#000000'
#endsub
#endclass
class Puddle(Cell):
def __init__(self, x, y, height, width, theme):
super().__init__(x, y, height, width, theme)
self._type = 'puddle'
self._colour = PhotoImage(file = 'puddle.png')
self._cost = 5
#endsub
#endclass
class WarpIn(Cell):
def __init__(self, x, y, height, width, theme):
super().__init__(x, y, height, width, theme)
self._type = 'warp in'
self._colour = PhotoImage(file = 'warpin.png')
#endsub
#endclass
class WarpOut(Cell):
def __init__(self, x, y, height, width, theme):
super().__init__(x, y, height, width, theme)
self._type = 'warp out'
self._colour = PhotoImage(file = 'warpout.png')
#endsub
#endclass