forked from flyingthing/PyBerzerk
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpyberzerk.py
More file actions
627 lines (523 loc) · 21.5 KB
/
Copy pathpyberzerk.py
File metadata and controls
627 lines (523 loc) · 21.5 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
#__author__ = 'TerryO'
__author__ = 'starwindz' # current developer
import sys, os, platform, pygame
from pygame.locals import *
from config import *
from robot import *
from player import *
from bullet import *
from otto import *
from maze import *
from wall import *
from lives import *
from score import *
from utils import *
from grid import *
from sound import *
from debug import *
import itertools
import globals
#--- Global constants ---
SCREEN_RECT = Rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) # screen dimensions
# image cache
IMAGEKEYS = ['player', 'robot', 'otto', 'robotexplode', 'lives', 'bullets', 'icon'] #image keys
DIRECT_DICT = {K_LEFT : 0x01, K_RIGHT : 0x02, K_UP : 0x04, K_DOWN : 0x08, K_LCTRL : 0x10} # keyboard keys
GAME_STATES = ["HighScores", "Cntrls", "Play"] # game states
screen = None
# class handles our game states(screens)
class GameState:
def __init__(self):
# prepare pygame environment
os.environ['SDL_VIDEO_CENTERED'] = '1' # center the screen
pygame.init()
pygame.display.set_caption(CAPTION)
pygame.mouse.set_visible(0)
pygame.font.init()
# get directory where game & images are located
path = os.path.dirname(os.path.abspath(__file__))
os.chdir(path)
# images(sprites)
global imagefiles
imagefiles = {}
try:
imagefiles = {key: os.path.join('images', "{}.png".format(key)) for key in IMAGEKEYS}
except:
pass
global screen
screen = pygame.display.set_mode(SCREEN_RECT.size)
# if platform.system() == "Windows":
icon = pygame.image.load(imagefiles['icon']).convert_alpha()
pygame.display.set_icon(icon)
self.game = None
# highscores state
def HighScore(self):
gamestate = "Play"
keys = highScoresScreen(screen)
if keys[K_F1]:
gamestate = "Cntrls"
self.Go(gamestate)
# controls help state
def Cntrls(self):
gameCntrls(screen)
self.Go("HighScore")
# play game state
def Play(self):
if self.game == None:
self.game = Game()
"""
robot color level bullet
DARK YELLOW 0 0
RED 500 1
DARK CYAN 1500 2
GREEN 3K 3
DARK PURPLE 4.5K 4
LIGHT YELLOW 6K 5
WHITE 7.5K 1 *fast bullets
DARK CYAN 10K 2
LIGHT PURPLE 11K 3
GRAY 13K 4
DARK YELLOW 15K 5
RED 17K 5
LIGHT CYAN 19K 5
"""
robotcolors = ((YELLOW, False, 0, 500, 5), (RED, True, 1, 1500, 4), (CYAN, True, 2, 3000, 3),
(GREEN, True, 3, 4500, 2), (PURPLE, True, 4, 6000, 1), (YELLOW, True, 5, 7500, 1),
(WHITE, True, -1, 10000, 1), (LIGHTSKYBLUE, True, -2, 11000, 1), (PURPLE, True, -3, 13000, 1),
(GRAY, True, -4, 15000, 1), (YELLOW, True, -5, 17000, 1), (RED, True, -5, 19000, 1),
(LIGHTSKYBLUE, True, -5, 999999, 1)
)
# maxbullets is currently not used...need to add this feature
for color, fire, maxbullets, max_score, frameupdate in robotcolors:
if Score.score < max_score:
break
self.game.run(color, fire, frameupdate)
Debug.print('# run...')
gamestate = "Play"
if self.game.lives <= 0:
if gameOver(screen,Score.score):
gamestate = "HighScore"
Score.score = 0
tmp = sys.getrefcount(self.game)
del self.game
self.game = None
self.Go(gamestate)
# we are done
def Quit(self):
pygame.quit()
sys.exit()
def Go(self,gamestate):
if hasattr(self,gamestate):
state = getattr(self,gamestate)
state()
class Game:
def __init__(self):
self.done = False
self.event_type = None
self.shotflg = False
self.keycnt = 0
self.movdir = 0
self.maze = Maze(83, 49) # (0x53,0x31), starting room X,Y -> a kind of random seed for generating maze
Debug.print('# maze initialized')
self.mazeexit = None
self.lives = MAX_LIVES
self.bonuspts = None
self.keys = pygame.key.get_pressed()
self.sprites = pygame.sprite.Group()
Robot.totalKillcnt = 0
globals.arenaCnt = 0
def process_events(self):
for e in pygame.event.get():
if e.type == QUIT or self.keys[K_ESCAPE]:
self.done = True
pygame.quit()
sys.exit()
break
elif e.type == SPAWN_OTTO:
self.spawnOtto()
elif e.type == ROBOT_ACTIVE:
self.robotActive()
Debug.print('# robots are activated')
Debug.print('# random robot voice played when robots are activated')
sound.playRobotVoice(random.randrange(0, 14))
elif e.type == PLAYER_EXIT:
Debug.print('# player is exiting room')
Debug.print('# random robot voice played when player is exiting room')
sound.playRobotVoice(random.randrange(0, 14))
self.playerExit(e.mazeexit)
elif e.type == BONUS_POINTS:
self.bonusPoints(e.bonuspoints)
elif e.type == BONUS_LIFE:
self.bonusLife()
elif e.type == PLAYER_ELECTROCUTED:
self.playerElectrocuted()
elif e.type in (KEYUP,KEYDOWN):
self.keys = pygame.key.get_pressed()
if self.keys[K_F1]:
gamePause(screen)
if self.keys[K_F2]:
gameFPS.toggleDisplay()
if e.type == KEYDOWN:
self.keycnt += 1
if e.key in DIRECT_DICT:
self.movdir |= DIRECT_DICT[e.key]
elif e.type == KEYUP:
self.keycnt -= 1
if e.key in DIRECT_DICT:
self.movdir &= ~DIRECT_DICT[e.key]
# checking if left modifier is pressed
if pygame.key.get_mods() & pygame.KMOD_LSHIFT:
pass
def playerElectrocuted(self):
pygame.time.set_timer(PLAYER_ELECTROCUTED,0) # kill timer
self.maze.exit()
self.mazeexit = None
self.done = True
if hasattr(self, 'sprites'):
for obj in self.sprites:
if type(obj) is Lives:
if obj.life == self.lives:
obj.kill()
break
self.lives -= 1
if self.lives <= 0:
if hasattr(self, 'wall2grp'):
for obj in self.wall2grp:
obj.kill()
if hasattr(self, 'player'):
self.player.kill()
self.refreshSprites()
else:
screenMazeClear(screen)
self.spriteCleanUp()
def electrocute(self):
if self.player.patternkey != "electrocuting":
# disable any active timer(s)
pygame.time.set_timer(SPAWN_OTTO,0)
pygame.time.set_timer(ROBOT_ACTIVE,0)
# put player in electrocut mode
self.player.electrocute()
pygame.time.set_timer(PLAYER_ELECTROCUTED, 1000) # 1sec
# robot(s) stop firing
Robot.laserEnable = False
for obj in self.wall2grp:
obj.kill()
def robot_explode(self,robot):
# after robot explode sprite is done this is it's callback
def callback(robot):
robot.kill() # get rid of explode sprite
explode = RobotExplode(robot, callback, imagefiles['robotexplode'], ROBOTEXPLODE_RECT, ROBOTEXPLODE_SPRITES )
self.sprites.add(explode)
robot.kill() # get rid of robot sprite
self.score.addpoints(ROBOT_KILL_POINTS)
def bonusPoints(self, bonus):
points = Bonus(GRAY, bonus)
self.sprites.add(points)
def bonusLife(self):
self.lives += 1
life = Lives(self.lives, imagefiles['lives'], LIVES_RECT, GREEN)
self.sprites.add(life)
def check_collisions(self):
# check bullet collision with robots/otto
collidedict = pygame.sprite.groupcollide(self.bullets, Robot.getGroup(), False, False)
if collidedict:
for bullet in collidedict.keys():
bullet.kill()
for value in collidedict.values():
for robot in value:
if type(robot) is Robot:
self.robot_explode(robot)
else:
pass
# check bullet collision with player
collidedict = pygame.sprite.groupcollide(self.bullets, self.playergrp, False, False)
if collidedict:
for bullet in collidedict.keys():
if type(bullet) is RobotBullet:
bullet.kill()
for value in collidedict.values():
for player in value:
if type(player) is Player:
self.electrocute()
return
else:
pass
# check bullet to bullet collision
combo = list(itertools.combinations(self.bullets, 2))
for a,b in combo:
if pygame.sprite.collide_rect(a,b):
a.kill() # remove from grp
b.kill()
# check bullet collision with wall
for bullet in self.bullets:
if pygame.sprite.spritecollideany(bullet, self.wallgrp):
bullet.kill()
# check robot collision with wall
collidedict = pygame.sprite.groupcollide(Robot.getGroup(), self.wallgrp, False, False)
if collidedict:
for robot in collidedict.keys():
if type(robot) is Robot:
self.robot_explode(robot)
# remove any old wall warnings
for x in self.wall2grp:
x.kill()
# warn player if close to wall by highlighting wall area
r = self.player.rect.copy()
self.player.rect = self.player.rect.inflate(16, 16)
collide_list = pygame.sprite.spritecollide(self.player, self.wallgrp, False)
for collide in collide_list:
c = collide.rect.clip(self.player.rect)
if collide.rect.w == 8:
c.left = collide.rect.left
c.w = 8
else:
c.top = collide.rect.top
c.h = 8
wall2 = WallObject(screen, GREEN, c)
self.sprites.add(wall2)
self.wall2grp.add(wall2)
self.player.rect = r.copy()
# check player collision with wall
if pygame.sprite.spritecollideany(self.player, self.wallgrp):
self.electrocute()
# check player collision with robots
collide = pygame.sprite.spritecollideany(self.player, Robot.getGroup())
if collide:
if type(collide) is Robot:
self.robot_explode(collide)
self.electrocute()
# check robot to robot collision
combo = list(itertools.combinations(Robot.getGroup(), 2))
for a,b in combo:
if pygame.sprite.collide_rect(a,b):
if type(a) is Robot:
self.robot_explode(a)
if type(b) is Robot:
self.robot_explode(b)
def spawnOtto(self):
pygame.time.set_timer(SPAWN_OTTO,0) # disable timer
otto = Otto(self.levelcolor, self.player, imagefiles['otto'], OTTO_RECT, OTTO_SPRITES )
self.sprites.add(otto)
Robot.getGroup().add(otto)
def robotActive(self):
pygame.time.set_timer(ROBOT_ACTIVE,0) # disable timer
# wake up robots
for robot in Robot.getGroup():
if type(robot) == Robot:
robot.active = True
def playerExit(self,mazeexit):
pygame.time.set_timer(SPAWN_OTTO,0) # disable any active timer(s)
pygame.time.set_timer(ROBOT_ACTIVE,0)
screenScroll(screen,mazeexit,self.levelcolor)
self.maze.exit(mazeexit)
self.mazeexit = mazeexit
self.done = True
for obj in self.sprites:
if type(obj) == Bonus:
obj.kill()
break
self.spriteCleanUp()
def spriteCleanUp(self):
# clean up sprites
self.bullets.empty()
self.wallgrp.empty()
self.wall2grp.empty()
self.playergrp.empty()
self.explodegrp.empty()
self.lifegrp.empty()
Robot.getGroup().empty()
self.sprites.empty()
del self.bullets
del self.wallgrp
del self.wall2grp
del self.playergrp
del self.explodegrp
del self.lifegrp
del self.sprites
del self.score
del self.gamefps
def run(self, levelcolor, robotLaser, frameupdate):
self.levelcolor = levelcolor
# keep track of sprites
self.explodegrp = pygame.sprite.Group()
self.bullets = pygame.sprite.Group()
Bullet.bulletcnt = BULLETS_MAX
self.wallgrp = pygame.sprite.Group()
self.wall2grp = pygame.sprite.Group()
self.playergrp = pygame.sprite.GroupSingle()
self.sprites = pygame.sprite.LayeredUpdates(self.wallgrp, self.playergrp,self.explodegrp)
self.lifegrp = pygame.sprite.Group()
door = {"E":"W","W":"E","N":"S","S":"N"}.get(self.mazeexit,"W")
self.player = Player(door, PLAYER_COLOR, imagefiles['player'], PLAYER_RECT, PLAYER_SPRITES)
self.sprites.add(self.player)
self.playergrp.add(self.player)
self.score = Score(GREEN)
self.sprites.add(self.score)
self.Level = Level(GREEN)
self.sprites.add(self.Level)
self.killCount = KillCount(GREEN)
self.sprites.add(self.killCount)
self.gamefps = gameFPS(GREEN)
self.sprites.add(self.gamefps)
globals.arenaCnt += 1
self.Arena(screen,self.maze,levelcolor)
grid = Grid(GRID_WIDTH, GRID_HEIGHT, self.maze.pillars) # 22 has been changed to 21
Debug.print('# grid initialized')
# sound.playRobotVoice(random.randrange(0, 14))
Debug.print('# player entered room');
def robotCallBack(cmd, *argv):
if cmd == "FIRE":
bullet = argv[0]
self.sprites.add(bullet)
self.bullets.add(bullet)
# create robots
Robot.newLevel(robotLaser)
for i in range(random.randrange(MIN_ROBOTS,MAX_ROBOTS)):
robot = Robot(levelcolor, robotCallBack, self.player, grid, self.maze.pillars, self.wallgrp, imagefiles, ROBOT_RECT, ROBOT_SPRITES, frameupdate )
self.sprites.add(robot)
# create lives
for i in range(2, self.lives+1):
life = Lives(i, imagefiles['lives'], LIVES_RECT, GREEN)
self.sprites.add(life)
# keep track of time
clock = pygame.time.Clock()
self.timer = pygame.time.get_ticks()
self.cooldown = 600
# set timer event for Otto
# pygame.time.set_timer(SPAWN_OTTO,1500*Robot.robotcnt) # 1.5sec/robot
if DEBUG_HIDE_OTTO == True:
ottoSpawnTime = 100000
else:
ottoSpawnTime = 2000
pygame.time.set_timer(SPAWN_OTTO,ottoSpawnTime*Robot.robotcnt) # 1.75sec/robot
# set timer event for robot movement
pygame.time.set_timer(ROBOT_ACTIVE,3000) # 3sec
# maze loop
self.done = False
electrocuted = False;
while not self.done:
now = pygame.time.get_ticks()
self.process_events()
if self.done:
break
gameFPS.clock = clock.get_fps()
if self.player.patternkey != "electrocuting":
self.player.mov(self.movdir)
if self.movdir & 0x10:
# fire laser only if cooldown has been 0.6 secs
if now - self.timer > self.cooldown:
bullet = self.player.fire(levelcolor, imagefiles['bullets'])
if bullet != None:
self.sprites.add(bullet)
self.bullets.add(bullet)
self.timer = now
else:
if electrocuted == False:
electrocuted = True
sound.playPlayerIsDestroyed()
Debug.print('# player is destroyed')
self.check_collisions()
self.refreshSprites()
clock.tick(FPS) # maintain frame rate
def refreshSprites(self):
# used to erase the sprite
def clear_callback(surf, rect):
surf.fill(BLACK, rect)
pass
# erase previous positions
self.sprites.clear(screen, clear_callback)
# update sprites
self.sprites.update()
if DEBUG_ROBOT_AI == True:
self.debugAIgrid()
# redraw sprites
dirty = self.sprites.draw(screen)
pygame.display.update(dirty)
def debugAIgrid(self):
# debug code for robot AI grid
for x in range(MAZE_XMIN-4,MAZE_XMAX+16, CELL_WIDTH):
#for x in range(MAZE_XMIN, MAZE_XMAX,24):
for y in range(MAZE_YMIN+8,MAZE_YMAX, CELL_HEIGHT):
#for y in range(MAZE_YMIN, MAZE_YMAX,24):
pygame.draw.line(screen, RED, (x, y), (MAZE_XMAX, y), (1))
pygame.draw.line(screen, RED, (x, y), (x, MAZE_YMAX), (1))
# build border walls and maze walls
def Arena(self, screen, maze, doorcolor=None):
# borders: west top, west bottom, east top, east bottom, left top, left bottom, right top, right bottom
borders = [
(BORDER_XMIN, BORDER_YMIN, WALLTHICKNESS, BORDER_VSEGMENT + WALLTHICKNESS),
(BORDER_XMIN, BORDER_YMIN + WALLTHICKNESS + BORDER_VSEGMENT*2, WALLTHICKNESS, BORDER_VSEGMENT),
(BORDER_XMAX, BORDER_YMIN, WALLTHICKNESS, BORDER_VSEGMENT + WALLTHICKNESS),
(BORDER_XMAX, BORDER_YMIN + WALLTHICKNESS + BORDER_VSEGMENT*2, WALLTHICKNESS, BORDER_VSEGMENT),
(BORDER_XMIN, BORDER_YMIN, WALLTHICKNESS + BORDER_HSEGMENT*2, WALLTHICKNESS),
(BORDER_XMIN, BORDER_YMIN + WALLTHICKNESS + BORDER_VSEGMENT*3, WALLTHICKNESS + BORDER_HSEGMENT*2, WALLTHICKNESS),
(BORDER_XMIN + WALLTHICKNESS + BORDER_HSEGMENT*3, BORDER_YMIN, WALLTHICKNESS + BORDER_HSEGMENT*2, WALLTHICKNESS),
(BORDER_XMIN + WALLTHICKNESS + BORDER_HSEGMENT*3, BORDER_YMIN + WALLTHICKNESS + BORDER_VSEGMENT*3, BORDER_HSEGMENT*2 + WALLTHICKNESS, WALLTHICKNESS)
]
for left,top,width,height in borders:
wall = WallObject(screen, WALL_COLOR, pygame.Rect(left,top,width,height))
self.sprites.add(wall)
self.wallgrp.add(wall)
PILLAR_HSEGMENT = BORDER_HSEGMENT
PILLAR_VSEGMENT = BORDER_VSEGMENT
HPILLAR = (PILLAR_HSEGMENT, WALLTHICKNESS)
VPILLAR = (WALLTHICKNESS, PILLAR_VSEGMENT)
pillars = maze.getPillars()
i = 0
for p in pillars[:]:
fx,fy = {0:(1,1), 1:(2,1), 2:(3,1), 3:(4,1), 4:(1,2), 5:(2,2), 6:(3,2), 7:(4,2)}[i]
x = MAZE_XMIN + PILLAR_HSEGMENT*fx
y = MAZE_YMIN + PILLAR_VSEGMENT*fy
x_offset,y_offset,w,h = 0,0,0,0
if p == "N":
w,h = VPILLAR
h += {1:WALLTHICKNESS}.get(i,0)
y_offset -= PILLAR_VSEGMENT
y_offset += {1:-WALLTHICKNESS}.get(i,0)
elif p == "S":
w,h = VPILLAR
h += {5:WALLTHICKNESS}.get(i,0)
elif p == "E":
w,h = HPILLAR
w += WALLTHICKNESS
y_offset += {0:-WALLTHICKNESS,1:-WALLTHICKNESS,2:-WALLTHICKNESS,3:-WALLTHICKNESS}.get(i,0)
elif p == "W":
w,h = HPILLAR
w += WALLTHICKNESS
x_offset -= PILLAR_HSEGMENT
y_offset += {0:-WALLTHICKNESS,1:-WALLTHICKNESS,2:-WALLTHICKNESS,3:-WALLTHICKNESS}.get(i,0)
else:
pass
i += 1
if p != "O":
rect = pygame.Rect(x+x_offset, y+y_offset, w, h)
mazewall = WallObject(screen, WALL_COLOR, rect)
self.sprites.add(mazewall)
self.wallgrp.add(mazewall)
# NOTE: exit door is opposite of where player exited maze
x,y,w,h = 0,0,0,0
doors = maze.getDoors()
for door in doors[:]:
if door in ("E", "W"):
y = BORDER_YMIN + WALLTHICKNESS + BORDER_VSEGMENT
w,h = WALLTHICKNESS/2, BORDER_VSEGMENT
if door == "E":
x = MAZE_XMAX + WALLTHICKNESS/4
else:
x = BORDER_XMIN + WALLTHICKNESS/4
elif door in ("N", "S"):
x = BORDER_XMIN + WALLTHICKNESS + BORDER_HSEGMENT*2
w,h = BORDER_HSEGMENT, WALLTHICKNESS/2
if door == "N":
y = BORDER_YMIN + WALLTHICKNESS/4
else:
y = MAZE_YMAX + WALLTHICKNESS/4
rect = pygame.Rect(x, y, w, h)
mazewall = WallObject(screen, doorcolor, rect)
self.sprites.add(mazewall)
self.wallgrp.add(mazewall)
def main():
gs = GameState()
gs.Go("HighScore")
if __name__ == "__main__":
main()
sys.exit()