-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTile.py
More file actions
350 lines (318 loc) · 13.1 KB
/
Tile.py
File metadata and controls
350 lines (318 loc) · 13.1 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#import RobotAI
import pygame
import random
import scoring
import Config
import EmailClient
import threading
import gui
import Sounds
"""
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
"""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
"""returns true if the tile is the same as tile at x,y or
tile at x,y is solid
@Params:
level(level): the level object
x(int) x pos of tile
y(int): y pos of tile
@Return:
bool: solid or self
"""
def thisOrSolid(self,level,x,y):
t = level.getTile(x,y)
if t.isSolid:
return True
if t == self:
return True
return False
def bump(self,level,entity,x,y):
if entity.canPickUpTreasure==True:
if self == Treasure1:
level.setTile(x,y,water)
gui.removeTreasure((x<<5,y<<5))
entity.score.incrementScore()
if entity == level.player:
t = threading.Thread(target=EmailClient.sendRandomEmail,args=(Config.config['email'],))
t.start()
self.updateAI(level,x,y)
if self == Treasure2:
level.setTile(x,y,sand)
gui.removeTreasure((x<<5,y<<5))
entity.score.incrementScore()
if entity == level.player:
t = threading.Thread(target=EmailClient.sendRandomEmail,args=(Config.config['email'],))
t.start()
self.updateAI(level,x,y)
if self == Treasure3:
level.setTile(x,y,grass)
gui.removeTreasure((x<<5,y<<5))
entity.score.incrementScore()
if entity == level.player:
t = threading.Thread(target=EmailClient.sendRandomEmail,args=(Config.config['email'],))
t.start()
self.updateAI(level,x,y)
if self == cactus and entity == level.player:
sounFXs = Sounds.Audio(False)
sounFXs.Plysound(False,True,False,False,False)
entity.health -= 1
print "Ouch!"
if entity.xa>0:
entity.x-=4
if entity.xa<0:
entity.x+=4
if entity.ya>0:
entity.y-=4
if entity.ya<0:
entity.y+=4
def updateAI(self,level,x,y):
for e in level.entities:
if e.canPickUpTreasure:
for d in e.destinations:
print d[0]>>5,x , d[1]>>5,y
if d[0]>>5==x and d[1]>>5==y:
e.destinations.remove(d)
level.paths.remove(d)
gui.removeTreasure(d)
e.inHand = "treasasdasd"#temp thing
print "Removed",d
if len(e.destinations) >0:
e.goHome = True
print "Changed direction!"
else:
gui.gameOver=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 not self.thisOrSolid(level,xx,yy-1) and not self.thisOrSolid(level,xx+1,yy) and not self.thisOrSolid(level,xx,yy+1) and not self.thisOrSolid(level,xx-1,yy):
screen.blit(level.getTile(xx,yy-1).img, (x,y))
screen.blit(self.images[1], (x,y))#surrounded an all sides
return
elif not self.thisOrSolid(level,xx,yy-1) and not self.thisOrSolid(level,xx+1,yy) and not self.thisOrSolid(level,xx-1,yy):
screen.blit(level.getTile(xx,yy-1).img, (x,y))
screen.blit(self.images[5], (x,y))#surrounded top and sides
return
elif not self.thisOrSolid(level,xx+1,yy) and not self.thisOrSolid(level,xx,yy+1) and not self.thisOrSolid(level,xx-1,yy):
screen.blit(level.getTile(xx-1,yy).img, (x,y))
screen.blit(pygame.transform.rotate(self.images[5], 180), (x,y))#surrounded bottom and sides
return
elif not self.thisOrSolid(level,xx,yy-1) and not self.thisOrSolid(level,xx,yy+1) and not self.thisOrSolid(level,xx-1,yy):
screen.blit(level.getTile(xx-1,yy).img, (x,y))
screen.blit(pygame.transform.rotate(self.images[5], 90), (x,y))#surrounded an left top and bottom
return
elif not self.thisOrSolid(level,xx,yy-1) and not self.thisOrSolid(level,xx+1,yy) and not self.thisOrSolid(level,xx,yy+1):
screen.blit(level.getTile(xx,yy-1).img, (x,y))
screen.blit(pygame.transform.rotate(self.images[5], -90), (x,y))#surrounded an right top and bottom
return
elif not self.thisOrSolid(level,xx,yy-1) and not self.thisOrSolid(level,xx,yy+1):
screen.blit(level.getTile(xx,yy-1).img, (x,y))
screen.blit(pygame.transform.rotate(self.images[4], -90), (x,y))#surrounded top and bottom
return
elif not self.thisOrSolid(level,xx-1,yy) and not self.thisOrSolid(level,xx+1,yy):
screen.blit(level.getTile(xx-1,yy).img, (x,y))
screen.blit(self.images[4], (x,y))#surrounded left and right
return
elif not self.thisOrSolid(level,xx,yy-1) and not self.thisOrSolid(level,xx-1,yy):
screen.blit(level.getTile(xx,yy-1).img, (x,y))
screen.blit(self.images[2], (x,y))
return
elif not self.thisOrSolid(level,xx,yy-1) and not self.thisOrSolid(level,xx+1,yy):
screen.blit(level.getTile(xx,yy-1).img, (x,y))
screen.blit(pygame.transform.rotate(self.images[2], -90), (x,y))#top right
return
elif not self.thisOrSolid(level,xx,yy+1) and not self.thisOrSolid(level,xx+1,yy):
screen.blit(level.getTile(xx+1,yy).img, (x,y))
screen.blit(pygame.transform.rotate(self.images[2], 180), (x,y))#bottom right
return
elif not self.thisOrSolid(level,xx,yy+1) and not self.thisOrSolid(level,xx-1,yy):
screen.blit(level.getTile(xx-1,yy).img, (x,y))
screen.blit(pygame.transform.rotate(self.images[2], 90), (x,y))#bottom left
return
elif not self.thisOrSolid(level,xx,yy+1):
screen.blit(level.getTile(xx,yy+1).img, (x,y))
screen.blit(pygame.transform.rotate(self.images[3], 90),(x,y))#bottom
return
elif not self.thisOrSolid(level,xx,yy-1):
screen.blit(level.getTile(xx,yy-1).img, (x,y))
screen.blit(pygame.transform.rotate(self.images[3], -90), (x,y))#top
return
elif not self.thisOrSolid(level,xx-1,yy):
screen.blit(level.getTile(xx-1,yy).img, (x,y))
screen.blit(self.images[3], (x,y))#left
return
elif not self.thisOrSolid(level,xx+1,yy):
screen.blit(level.getTile(xx+1,yy).img, (x,y))
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)
Treasure1 = Tile(10,"z","tiles/brokenChest.png")
Treasure2 = Tile(11,"x","tiles/burntChest.png")
Treasure3 = Tile(12,"y","tiles/darkChest.png")
Treasure4 = Tile(13,"X","tiles/glassChest.png")
Treasure5 = Tile(14,"Y","tiles/goldChest.png")
Treasure6 = Tile(15,"P","tiles/normalChest.png")
Treasure7 = Tile(16,"M","tiles/OverFlowChest.png")
Treasure8 = Tile(17,"p","tiles/crystalChest.png")
Treasure9 = Tile(18,"Z","tiles/sandT.png")
Treasure10 = Tile(19,"f","tiles/grownOverChest.png")
grass2 = Tile(20,"-","tiles/grass2.png")
grass3 = Tile(21,"-","tiles/grass3.png")
cactus = Tile(22,"-","tiles/cactus.png")
sandbush = Tile(23,"-","tiles/sandbush.png")
larva = WaterTile(24,"L",["tiles/larva.png","tiles/waterall.png","tiles/larval.png","tiles/larvaside.png","tiles/larva2side.png","tiles/larva3side.png"])
larva.setSpeed(2)
pillar = Tile(25,"H","tiles/pillar.png")
quicksand = Tile(26,".","tiles/quicksand.png")
cactus.setSolid(True)
grass.setChild(grass2)
grass.setChild(grass3)
sand.setChild(cactus)
sand.setChild(sandbush)
sand.setChild(quicksand)
"""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,20)
if rand == 0 and t.hasChildren():
return t.getChildren()[random.randint(0,len(t.getChildren())-1)].id
return t.id
return 0