-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTile.py
More file actions
239 lines (207 loc) · 7.39 KB
/
Tile.py
File metadata and controls
239 lines (207 loc) · 7.39 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import pygame
import random
"""
Global list of tile objects
"""
tiles = []
"""
Class to hold the attributes of tiles
"""
class Tile(object):
"""Constructor for tile class
@Params:
iD(int): id of tile
char(char): lookup char of tile
imagePath(string): path to tile texture
@Return:
Tile(object)
"""
def __init__(self,iD,char,imagePath):
self.id = iD
self.char =char
self.img = pygame.image.load(imagePath)
self.img = pygame.transform.scale(self.img, (32, 32))
self.isSolid = False
self.speed = 1.0
self.children = []
tiles.append(self)
"""render tiles to screeen
@Params:
screen(pygame.surface): pygame surface
x(int): x co-ordinate of tile
y(int): y co-ordinate of tile
@Return:
None
"""
def render(self,level,screen,x,y,xx,yy):
screen.blit(self.img, (x,y))
"""update tile properties
@Params:
None
@Return:
None
"""
def tick(self):
pass
#print self,self.isSolid
"""set the movement cost of the tile
@Params:
speed(float): movement cost of tile
@Return:
None
"""
def setSpeed(self,speed):
self.speed = speed
"""gets the movement cost of the tile
@Params:
None
@Return:
speed(float): movement cost of tile
"""
def getSpeed(self):
return self.speed
"""sets the solidity of the tile
@Params:
solid(boolean): solidity of tile
@Return:
None
"""
def setSolid(self,solid):
self.isSolid=solid
"""gets the solidity of the tile
@Params:
None
@Return:
solid(boolean): solidity of tile
"""
def isSolid(self):
return self.isSolid
"""gets the id of the tile
@Params:
None
@Return:
id(int): solidity of tile
"""
def getId(self):
return self.id
"""gets the tiles children
@Params:
None
@Return:
children(list)
"""
def getChildren(self):
return self.children
"""adds child to tiles children
@Params:
child(tile): the tile to add to tiles children
@Return:
None
"""
def setChild(self,child):
self.children.append(child)
"""tests whether tile has children
@Params:
None:
@Return:
hasChildren(boolean): true if tile has child
"""
def hasChildren(self):
if len(self.children)==0:
return False
return True
class WaterTile(Tile):
def __init__(self,iD,char,imagePaths):
self.images = []
for i in imagePaths:
img = pygame.image.load(i)
img = pygame.transform.scale(img, (32, 32))
self.images.append(img)
super(self.__class__, self).__init__(iD,char,imagePaths[0])
def render(self,level,screen,x,y,xx,yy):
if level.getTile(xx,yy-1)!=water and level.getTile(xx+1,yy)!=water and level.getTile(xx,yy+1)!=water and level.getTile(xx-1,yy)!=water:
screen.blit(self.images[1], (x,y))#surrounded an all sides
return
elif level.getTile(xx,yy-1)!=water and level.getTile(xx+1,yy)!=water and level.getTile(xx-1,yy)!=water:
screen.blit(self.images[5], (x,y))#surrounded top and sides
return
elif level.getTile(xx+1,yy)!=water and level.getTile(xx,yy+1)!=water and level.getTile(xx-1,yy)!=water:
screen.blit(pygame.transform.rotate(self.images[5], 180), (x,y))#surrounded bottom and sides
return
elif level.getTile(xx,yy-1)!=water and level.getTile(xx,yy+1)!=water and level.getTile(xx-1,yy)!=water:
screen.blit(pygame.transform.rotate(self.images[5], 90), (x,y))#surrounded an left top and bottom
return
elif level.getTile(xx,yy-1)!=water and level.getTile(xx+1,yy)!=water and level.getTile(xx,yy+1)!=water:
screen.blit(pygame.transform.rotate(self.images[5], -90), (x,y))#surrounded an right top and bottom
return
elif level.getTile(xx,yy-1)!=water and level.getTile(xx,yy+1)!=water:
screen.blit(pygame.transform.rotate(self.images[4], -90), (x,y))#surrounded top and bottom
return
elif level.getTile(xx-1,yy)!=water and level.getTile(xx+1,yy)!=water:
screen.blit(self.images[4], (x,y))#surrounded left and right
return
elif level.getTile(xx,yy-1)!=water and level.getTile(xx-1,yy)!=water:
screen.blit(self.images[2], (x,y))#top left
return
elif level.getTile(xx,yy-1)!=water and level.getTile(xx+1,yy)!=water:
screen.blit(pygame.transform.rotate(self.images[2], -90), (x,y))#top right
return
elif level.getTile(xx,yy+1)!=water and level.getTile(xx+1,yy)!=water:
screen.blit(pygame.transform.rotate(self.images[2], 180), (x,y))#bottom right
return
elif level.getTile(xx,yy+1)!=water and level.getTile(xx-1,yy)!=water:
screen.blit(pygame.transform.rotate(self.images[2], 90), (x,y))#bottom left
return
elif level.getTile(xx,yy+1)!=water:
screen.blit(pygame.transform.rotate(self.images[3], 90),(x,y))#bottom
return
elif level.getTile(xx,yy-1)!=water:
screen.blit(pygame.transform.rotate(self.images[3], -90), (x,y))#top
return
elif level.getTile(xx-1,yy)!=water:
screen.blit(self.images[3], (x,y))#left
return
elif level.getTile(xx+1,yy)!=water:
screen.blit(pygame.transform.rotate(self.images[3], 180), (x,y))#right
return
else:
screen.blit(self.images[0], (x,y))#no suroundings
return
"""
Tile creation
"""
void = Tile(0,"v","tiles/void.png")
grass = Tile(1,"g","tiles/grass.png")
water = WaterTile(2,"w",["tiles/water.png","tiles/waterall.png","tiles/watertl.png","tiles/waterside.png","tiles/watert2side.png","tiles/watert3side.png"])
water.setSpeed(3)
wall = Tile(3,"a","tiles/wall.png")
wall.setSolid(True)
sand = Tile(4,"s","tiles/sand.png")
sand.setSpeed(2)
redlight = Tile(5,"r","tiles/redlight.png")
redlight.setSolid(True)
greenlight = Tile(6,"l","tiles/greenlight.png")
start1 = Tile(7,"S","tiles/start.png")
start2 = Tile(8,"R","tiles/start.png")
amberlight = Tile(9,"m","tiles/amberlight.png")
amberlight.setSolid(True)
Treasure0 = Tile(10,"z","tiles/start.png")
Treasure2 = Tile(11,"x","tiles/start.png")
Treasure3 = Tile(12,"y","tiles/start.png")
Treasure4 = Tile(13,"p","tiles/start.png")
grass.setChild(sand)
"""gets the tile id from the tiles char.
Used to populate level.tiles[]
@Params:
char(char): character loaded from level file
@Return:
id(int): id of tile with char of char
"""
def getID(char):
for t in tiles:
if t.char == char:
rand=random.randrange(0,10)
if rand == 0 and t.hasChildren():
return t.getChildren()[0].id
return t.id
return 0