-
Notifications
You must be signed in to change notification settings - Fork 1
/
game_system.py
357 lines (307 loc) · 9.63 KB
/
game_system.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
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
351
352
353
354
355
356
import os
import pygame
from copy import deepcopy
import anim2 as anim
from constants import *
from sprites_load import *
def to_dodec(number):
chars="0123456789GH"
ans=""
while(number>0):
ans=chars[number%12]+ans
number=number//12
return ans or "0"
class game_map:
data=list()
width=0
height=0
def __init__(self,continues,screen,width=11,height=7):
self.r()
self.show=True
self.animations=list()
self.screen=screen
self.animslist=list()
self.width=width
self.height=height
self.data=[[None]*width for i in range(height)]
self.level=list()
self.name=None
self.spritesheet=dict()
self.level_end=False
self.next_step=30
self.damagecount=0
self.can_step=True
self.continues=continues
self.life=3
self.level_name=None
self.score=0
self.init_level()
self.cursor = None
#print("data[0][0] to data[%s][%s]"%(len(self.data),len(self.data[0])))
def has_lost(self):
return self.level_end and self.life<=0
def kill(self):
self.life=0
def r(self):
global chartable, turnip
chartable,turnip=reload_character()
anim.reset()
def put_jumper(self,up=True,curtain=7):
mp=pygame.mouse.get_pos()
mx,my=((mp[0]-gx)//ts,(mp[1]-gy)//ts)
if(0<=mx<11 and 0<=my<7 and my<curtain):
#print("Trying to jump the",mx,my)
a= self.put(mx,my,up)
#print a
return a
return False
def put(self,x,y,up=True):
#print("Direct tile",up,"in",self.level[y][x])
if(self.level[y][x]=="0" or self.level[y][x]==None):
if(up):
self.level[y][x]="U"
else:
self.level[y][x]="D"
return True
return False
def can_put(self,x,y):
return (self.level[y][x]=="0")
def _load(self,levelname):
try:
datafile=open(os.path.join("levels",str(levelname)+".rawdata") )
except Exception as e:
print("Cannot load level!",levelname,e)
return False
line=datafile.readline().strip()
if(line=="This is a map"):
self.level_name=datafile.readline().strip()
line=datafile.readline().split()
self.width,self.height=int(line[0]),int(line[1])
self.data=[[None]*self.width for i in range(self.height)]
for i in range(self.height):
line=datafile.readline().strip()
#print("Line",i,line)
for j,elem in enumerate(line):
#print elem,
self.data[i][j]=elem
else:
print("Tried to open",levelname)
print("This is not a map")
return False
return True
def show(self):
print("")
print(self.name)
for i,line in enumerate(self.level):
for j,element in enumerate(line):
print(str(element),)
print()
print()
def show_level(self):
print("")
print(self.name)
for i,line in enumerate(self.data):
for j,element in enumerate(line):
print(str(element),)
print()
print()
def init_level(self):
self.animslist=list()
self.px=0
self.py=0
self.level=deepcopy(self.data)
self.level_end=False
self.pd=1
self.life=3
self.maxdepth=0
self.jumpers=0
self.namesurf=myfont.render(self.level_name, ANTIALIAS, (255,255,255))
def endofline(self):
return(self.px+self.pd>=self.width)or(self.px+self.pd<0)
def next_collision(self,letter="B"):
return(self.level[self.py][self.px+self.pd]==letter)
def draw(self,screen):
screen.fill((0,0,0))
data=self.level
mp=pygame.mouse.get_pos()
mx,my=((mp[0]-gx)//ts*ts,(mp[1]-gy)//ts*ts)
#screen.blit(cbnasurf, (0,0))
for i in range(7):
for j in range(11):
jj=6-(i+j)%2*2
colors=( (255,0,0),(255,255,0),(0,255,0),(0,255,255),(0,0,255),(255,0,255),(128,128,0) )
color=tuple(a*(jj+3)/14 for a in colors[i])
grey=((128,128,128),(100,100,100))[(i+j)%2]
if(i>=self.maxdepth and not self.py==self.height):
color=grey
pygame.draw.rect(screen, color, (gx+ts*j, gy+ts*i, ts, ts))
screen.blit(cadre, (gx+j*ts, gy+i*ts))
if(data[i][j]=="B"):
#color=(0,0,0)
#pygame.draw.rect(screen, color, (gx+ts*j, gy+ts*i, ts, ts))
screen.blit(wall, (gx+j*ts, gy+i*ts))
if(i==6):
screen.blit(gradtile, (gx+j*ts, gy+i*ts))
if(self.data[i][j]=="D"):
screen.blit(natural_boostdown, (gx+j*ts, gy+i*ts))
elif(data[i][j]=="D"):
screen.blit(boostdown, (gx+j*ts, gy+i*ts))
elif(self.data[i][j]=="U"):
screen.blit(natural_boostup, (gx+j*ts, gy+i*ts))
elif(data[i][j]=="U"):
screen.blit(boostup, (gx+j*ts, gy+i*ts))
elif(data[i][j]=="V"):
screen.blit(lifevial, (gx+j*ts, gy+i*ts))
pygame.draw.lines(screen, (255,255,255), False, ((gx,gy+gh-1),(gx,gy),(gx+gw,gy),(gx+gw,gy+gh-1)))
if(mx>=0 and my>=0 and mx <gw and my<gh):
pygame.draw.lines(screen, (255,255,255), True, ((gx+mx,gy+my),(gx+mx+ts,gy+my),(gx+mx+ts,gy+my+ts-1),(gx+mx,gy+my+ts-1)))
if(pygame.mouse.get_pressed()[0]==True):
#screen.blit(boostingsurf,(gx+mx,gy+my))
pass
self.damagecount=max(0,self.damagecount-1)
#print("Damage is",anim.damagecontrol)
dc=(self.damagecount<=0) or (self.damagecount>half) or ((self.damagecount%3)==0)
"""
if not anim.damagecontrol:
print("Damage control set to",anim.damagecontrol)"""
anim.damagecontrol=dc
"""
for anim in self.animslist:
anim.damagecontrol=dc"""
index=0
while(index<len(self.animslist)):
if(not next(self.animslist[index])):
self.animslist.pop(index)
else:
index+=1
#pygame.draw.rect(screen, (0,0,0), ((1+gx+gw,gy+self.maxdepth*ts+ts),(gx+gw+ts,gy+gh)))
if(self.maxdepth<7 and self.maxdepth>=0 and self.show):
screen.blit(marker, (gx+gw,gy+ts*self.maxdepth))
#pygame.draw.rect(screen, (0,0,0), ((0,0),(ts*7,gy-1)))
for i in range(self.continues):
screen.blit(starsurf, (ts*i,0))
#pygame.draw.rect(screen, (0,0,0), ((0,gy+ts*2.5),(ts*1.5,gy+gh)))
if(self.show):
if(self.jumpers>=0):
for i in range(3):
if(i<self.jumpers):
screen.blit(boostnote,(ts/2,wh+i*ts*1-5.5*ts))
else:
screen.blit(boost_socket,(ts/2,wh+i*ts*1-5.5*ts))
elif(self.jumpers==-1):
screen.blit(infinity,(ts/2,wh-5.5*ts))
for v in range(3):
if(v<self.life):
screen.blit(heartsurf, (ts*v,ts))
else:
screen.blit(emptyheartsurf, (ts*v,ts))
screen.blit(leveltitle,(ww/2,0))
screen.blit(scoretitle,(ww/2,ts))
screen.blit(boosttitle,(ts/2,4*ts))
screen.blit(myfont.render(to_dodec(self.score), ANTIALIAS, (255,255,255)),(ww-ts*5,gy-ts))
screen.blit(self.namesurf,(ww-ts*5,0))
def draw_char(self,screen):
screen.blit(chartable[0][0],(gx,gy))
def step(self):
#print self.px,self.py,
l=self.level
if(self.life==0):
self.animslist.append(anim.diet(self.screen,self.px,self.py,self.pd,half))
self.level_end=True
elif self.endofline(): #next step is end of line
#TODO turnanimation
#self.animations.append(turnanimation(self.px,self.py,self.pd))
self.animslist.append(anim.turn(self.screen,self.px,self.py,self.pd))
self.pd=-self.pd
elif self.next_collision("B"): #block
self.px+=self.pd
if(self.can_step):
self.animslist.append(anim.forward(self.screen,self.px,self.py,self.pd))
self.life-=1
if(self.life>0): #following interpretatino, >0 or >=0
#print "DAMAGE!"
self.score-=1
self.animslist.append(anim.crush(self.screen,self.px,self.py,-(not self.can_step)*quarter))
#TODO damageanimation
#self.animations.append(damageanimation(self.px,self.py,self.pd)
l[self.py][self.px]="0" #remove the wall marker
self.damagecount=half+e+(not self.can_step)*quarter
else:
#print "DEAD!"
self.score-=5
self.animslist.append(anim.diet(self.screen,self.px,self.py,self.pd))
#TODO deadanimation
#self.animations.append(deadanimation(self.px,self.py,self.pd)
self.level_end=True
#self.next_step=30 #???? not using this?
elif self.next_collision("V"): #vial of vitality (vie)
#print "REGEN"
self.score+=2
self.px+=self.pd
if(self.can_step):
self.animslist.append(anim.forward(self.screen,self.px,self.py,self.pd))
self.animslist.append(anim.bonus(self.screen,self.px,self.py,-(not self.can_step)*quarter))
l[self.py][self.px]="0"
self.life=min(self.life+1,3) #3 is max, gotta write this one down
#TODO bonusanimation
#self.animations.append(bonusanimation
elif self.next_collision("U"):
if(self.can_step):
if(self.py==0):
self.px+=self.pd
self.animslist.append(anim.forward(self.screen,self.px,self.py,self.pd))
#TODO up edge animation
#up edge sound bump
pass
else:
#print "JUMP UP"
self.py-=1
self.can_step=False
self.animslist.append(anim.jump_up(self.screen,self.px+self.pd,self.py,self.pd))
self.step()
self.can_step=True
else:
self.px+=self.pd
#sound can't step
elif self.next_collision("D"):
if(self.can_step):
if(self.py==self.height-1):
self.level_end=True
#print "* >>WIN<< *"
self.py+=1
self.animslist.append(anim.jump_down(self.screen,self.px+self.pd,self.py,self.pd))
self.animslist.append(anim.endlevel(self.screen,self.px+self.pd,self.py,self.pd))
self.score+=self.life*3
self.score+=self.jumpers
#TODO win animation
#win sound
else:
#print "JUMP DOWN"
self.py+=1
self.animslist.append(anim.jump_down(self.screen,self.px+self.pd,self.py,self.pd))
self.can_step=False
self.step()
self.can_step=True
if(self.py>self.maxdepth):
self.maxdepth+=1
self.jumpers=min(self.jumpers+1,3) #3 is max apparently
#print ">>JUMPER BONUS!"
#jumpbonus sound
else:
self.px+=self.pd
#sound can't step
else:
self.px+=self.pd
if(self.can_step):
self.animslist.append(anim.forward(self.screen,self.px,self.py,self.pd))
#print ""
#def draw(self,screen):
if __name__=="__main__":
level=game_map()
level._load("test")
level.show_level()
level.init_level()
while not level.level_end:
#for i in range(10):
level.step()
level.show()