-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtile.py
executable file
·253 lines (206 loc) · 5.65 KB
/
tile.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
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
# -*- coding: utf-8 -*-
from numpy import random as random
import pygame
import numpy
import math
from particle import *
from sound import *
def nothing(a=None, b=None, c=None):
pass
def shotBreakableWall(shot, tile):
tile.life -= shot.damage
if tile.life <= 0:
tile.sprite = pygame.image.load(tile.spriteFile)
walldown[numpy.random.randint(0,len(walldown))].play()
tile.world.background.blit(tile.sprite, [tile.pos[0]*32,tile.pos[1]*32])
tile.tankhit = nothing
tile.shothit = nothing
tile.colfunc = nothing
tile.coldetect = False
for i in xrange(10):
tile.world.ps.create(Particle(tile.world, numpy.array(tile.pos)*32, numpy.random.randint(-4,5,2),random.randint(60,140)))
def shotBreakableObsidian(shot, tile):
shotBreakableWall(shot,tile)
def quicksand(tank, tile):
tank.multiplier = 2.0
def slowsand(tank, tile):
tank.multiplier = 0.5
def mover(tank, tile, xdir, ydir):
if tank.tilecol([xdir, ydir]):
return
tank.pos[0] += xdir
tank.pos[1] += ydir
def rotateshot(shot, tile, direction):
shot.rotateshot(direction)
def uptile(tank, tile):
mover(tank, tile, 0, -1.0)
def downtile(tank, tile):
mover(tank, tile, 0, 1.0)
def lefttile(tank, tile):
mover(tank, tile, -1.0, 0)
def righttile(tank, tile):
mover(tank, tile, 1.0, 0)
def rotup(shot, tile):
rotateshot(shot, tile, math.pi/2.0)
def rotdown(shot, tile):
rotateshot(shot, tile, 3.0*math.pi/2.0)
def rotleft(shot, tile):
rotateshot(shot, tile, math.pi)
def rotright(shot, tile):
rotateshot(shot, tile, 0)
tileTypeArguments = {
"shotBreakableWall" : {
"life" : 50,
"spriteFile" : "'gfx/destroyedstone.png'"
},
"shotBreakableObsidian" : {
"life" : 200,
"spriteFile" : "'gfx/destroyedstone.png'"
},
"quicksand" : {
},
"slowsand" : {
},
"uptile" : {},
"downtile" : {},
"lefttile": {},
"righttile": {},
"rotup" : {},
"rotdown" : {},
"rotleft" : {},
"rotright" : {},
}
class Tile:
def __init__(self, world, pos, filename, coldetect=False, tankhit=nothing,
shothit=nothing, colfunc=nothing, varArgs={}):
self.sprite = pygame.image.load(filename)
self.coldetect = coldetect
self.tankhit = tankhit
self.shothit = shothit
self.colfunc = colfunc
self.world = world
self.pos = pos
for k in varArgs:
exec("self." + k + " = " + str(varArgs[k]))
def blitTo(self, surface, pos):
surface.blit(surface, self.sprite, pos)
class WeightingHandler:
def __init__(self):
self.weightings = []
self.total = 0
def addWeighting(self, val):
self.weightings.append(val)
self.total += val
def getRandomIndex(self):
rndVal = random.random()*self.total
index = -1
indexSum = 0
while indexSum < rndVal:
index+=1
indexSum += self.weightings[index]
return index
class World:
def __init__(self, ps, shots):
self.shots = shots
self.ps = ps
def load(self, fileStr, screen_width, screen_height):
self.screen_width = screen_width
self.screen_height = screen_height
self.tiles = []
fp = open(fileStr, "r")
lines = fp.readlines()
# Split level file into chunks
lvLines = []
infoLines = []
typeState = 0
for L in lines:
L = L.strip("\n\r")
if len(L)==0:
continue
if L == "---":
typeState+=1
continue
if typeState == 0:
lvLines.append(L)
elif typeState == 1:
infoLines.append(L)
# Create tile types
tileTypes = {}
weightIndexes = WeightingHandler()
for L in infoLines:
line = [val for val in L.split("\t") if val != ""]
for string in line:
if len(string)==0:
print("Empty")
# TILE DATA
tid = int(line[0])
sprite = line[1]
isCol = int(line[2])
# Tile collision functions
tankCol = eval(line[3])
shotHit = eval(line[4])
tileCol = eval(line[5])
# Tile arguments
tileArgList = {}
if line[3] in tileTypeArguments:
tileArgList.update(tileTypeArguments[line[3]])
if line[4] in tileTypeArguments:
tileArgList.update(tileTypeArguments[line[4]])
if line[5] in tileTypeArguments:
tileArgList.update(tileTypeArguments[line[5]])
if len(line)>=7:
weighting = float(line[6])
else:
weighting = 1.0
weightIndexes.addWeighting(weighting)
# END
tileTypes[tid] = (sprite, isCol, tankCol, shotHit, tileCol, tileArgList)
# Create tiles
# Random:
self.tiles = []
p1y = screen_height/32-5
p1x = screen_width/32-5
if lvLines[0] == "random":
for y in xrange(screen_height/32):
tileLine = []
for x in xrange(screen_width/32):
if (x in [3,4,5] and y in [3,4,5]) or (x in [p1x, p1x+1, p1x+2] and y in [p1y, p1y+1, p1y+2]):
T = tileTypes[0]
else:
T = tileTypes[weightIndexes.getRandomIndex()]
tileLine.append( Tile( self, (x,y), T[0], T[1], T[2], T[3], T[4], T[5] ) )
self.tiles.append(tileLine)
# Normal:
else:
y=0
for L in lvLines:
tileLine = []
Lspl = L.split(" ")
x=0
for n in Lspl:
T = tileTypes[int(n)]
tileLine.append( Tile( self, (x,y), T[0], T[1], T[2], T[3], T[4], T[5] ) )
x+=1
self.tiles.append(tileLine)
y+=1
fp.close()
pass
def getTile(self,pos):
xPos = int(pos[0]/32)
yPos = int(pos[1]/32)
if pos[0] < 0 or pos[1] < 0 or xPos >= self.screen_width/32 or yPos >= self.screen_height/32:
return None
else:
return self.tiles[yPos][xPos]
def process(self):
pass
def draw(self, surf):
self.background = surf
for y in xrange(len(self.tiles)):
for x in xrange(len(self.tiles[y])):
tile = self.tiles[y][x]
surf.blit(tile.sprite, [x*32,y*32])
def checkCollision(self, pos):
if pos[0] < 0 or pos[1] < 0 or pos[0] > self.screen_width or pos[1] > self.screen_height:
return True
return self.tiles[int(pos[1])/32][int(pos[0])/32].coldetect