-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
executable file
·3918 lines (3175 loc) · 127 KB
/
game.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
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# vim:set sts=4 et sw=4 ts=4 ci ai:
"""
Mushroom_and_Monsters -- a python+pygame based remake of the Atari
arcade version of Millipede.
Copyright 2007 Don E. Llopis (llopis.don@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA
"""
import os
import pygame
import random
import sys
"""
millipede dimensions -- from atari acrcade version
total screen area 240x256
score area (0,0) - (239,7) 8 pixels
arena area (0,8) - (239,247) 240 pixels
player area (0,200) - (239,247) 48 pixels
bottom score area (0,248) - (239,255) 8 pixles
this version
arena area (0,0) - (239,239) 240 pixels
player area (0,192) - (239,239) 48 pixel
bottom score area (0,240) - (239,255) 16 pixels
mushroom grid 30x29
mushroom grid 30x35
mushroom 8x8
player 7x7
player-missiler 5x7
ddt 16x8
1 millipede 8x8
Player Life Bonus @ every 20,000pts
Num Spiders on screen increases from 1 to n?
"""
DEBUG = False
class Game:
FRAME_RATE = 60
SCREEN_W = 360
SCREEN_H = 376
ARENA_W = 360
ARENA_H = 360
PLAYER_W = 360
PLAYER_H = 72
SCORE_W = 360
SCORE_H = 16
LIFE_BONUS = 20000
MAX_DDTS = 5
# delay times are in milliseconds
SLOW_DOWN_TIME_TTL = 5000
SLOW_DOWN_TIME_TTL_DT = 125
SWARM_STAGE_SPAWN_DELAY = 250
SPAWN_QUEUE_DELAY = 100
LEVEL_UP_DELAY = 1250
START_RANDOM_EVENT_DELAY = 1000
THE_NINTH_MILLIPEDE_DELAY = 750
BEE_SWARM = 0
DRAGONFLY_SWARM = 1
MOSQUITO_SWARM = 2
BEEDRAGONFLY_SWARM = 3
BEEDRAGONFLYMOSQUITO_SWARM = 4
SWARM_BONUS_INC = 100
SWARM_MAX_BONUS_POINTS = 1000
START_PLAYER_LIVES = 3
RANDOM_EVENT_FREQ = 50
def __init__(self):
max_columns = Game.SCREEN_W / MushroomField.MUSHROOM_WIDTH
pygame.init()
if not pygame.font: print "Warning: fonts disabled."
mixer_init = pygame.mixer.get_init()
if not mixer_init: print "Warning: mixer disabled."
if mixer_init:
print "Mixer available channels: ", \
pygame.mixer.get_num_channels()
pygame.display.set_caption('Monsters and Mushrooms')
system_font = pygame.font.get_default_font()
self.font = pygame.font.SysFont( None, 24 )
# init joystick 0
num_joystick = pygame.joystick.get_count()
if num_joystick > 0:
joy = pygame.joystick.Joystick( num_joystick - 1 )
if not joy.init():
print "Warning: could not initialize joystick(s)"
# allocate backbuffer
size = Game.SCREEN_W, Game.SCREEN_H
#self.screen = pygame.display.set_mode(size,pygame.FULLSCREEN)
self.screen = pygame.display.set_mode(size)
self.background = pygame.Surface(size)
self.background.fill([0,0,0])
self.screen.blit(self.background, [0,0])
# initialize sounds
self.millipede_snd = load_sound("sounds/millipede.ogg")
self.player_missile_snd = load_sound("sounds/shot1.ogg")
self.player_hit_snd = load_sound("sounds/exp1.ogg")
self.ddt_snd = load_sound("sounds/exp2.ogg")
self.bee_snd = load_sound("sounds/bee1.ogg")
self.mosquito_snd = load_sound("sounds/fly.ogg")
self.spider_snd = load_sound("sounds/spider.ogg")
self.dragonfly_snd = load_sound("sounds/bee2.ogg")
# load main game static screens
self.title_img = load_image("images/title.png")
self.gameover_img = load_image("images/gameover.png")
self.paused_img = load_image("images/paused.png")
self.start_img = []
self.start_img.append(load_image("images/start0.png"))
self.start_img.append(load_image("images/start1.png"))
self.start_img.append(load_image("images/start2.png"))
self.init_vars()
def init_vars(self):
# init game objects
self.keys = [False, False, False, False, False]
self.score = 0
self.prev_score = 0
self.cur_level = 0
self.p_table_idx = 0
self.swarm_level = 0
self.player_lives = Game.START_PLAYER_LIVES
self.player_dead = False
self.slow_down_time = False
self.time_delay = 0
self.repeat_level = False
self.menu_delay = 0
self.level_up_delay = 0
self.clock = pygame.time.Clock()
self.cur_tick = 0
self.prev_tick = 0
self.spawn_queue = {
'bees' : 0,
'beetles' : 0,
'dragonflies' : 0,
'earwigs' : 0,
'inchworms' : 0,
'mosquitos' : 0,
'spiders' : 0,
'ttl' : 0
};
self.actionfn = self.main_menu_init
self.prev_actionfn = None
self.swarmfn = None
self.swarm_count = 0
self.swarm_launch_delay = 0
self.swarm_monster = None
self.swarm_monster_lst = None
self.swarm_points = 0
self.swarm_next_monster = 0
self.birthanddeathfn = None
self.player = Player(self)
self.players = pygame.sprite.Group()
self.playerMissile = PlayerMissile(self)
self.playerMissiles = pygame.sprite.Group()
self.playerMissiles.add( self.playerMissile )
self.popups = PopUps(self)
self.particles = Particles(self)
self.mushroom_field = MushroomField(self)
self.millipedes = []
self.spiders = pygame.sprite.Group()
self.bees = pygame.sprite.Group()
self.mosquitos = pygame.sprite.Group()
self.beetles = pygame.sprite.Group()
self.earwigs = pygame.sprite.Group()
self.dragonflies = pygame.sprite.Group()
self.inchworms = pygame.sprite.Group()
self.monsters = pygame.sprite.Group()
self.ddts = pygame.sprite.Group()
self.damaged_mushrooms = []
self.random_event_delay = 0
self.cur_level_random_event_delay = Game.START_RANDOM_EVENT_DELAY
# probability table for spawning monsters
# (beetle,spider,bee,inchworm,mosquito,dragonfly,earwig)
# seven types of monsters
# one tuple per level example:
self.p_table = (
(25,-1,-1,-1,-1,-1,-1),
(30,50,-1,52,-1,-1,54),
(35,52,54,58,-1,-1,62),
(40,54,56,60,-1,-1,64),
(45,56,58,64,66,68,74),
(50,58,60,66,68,70,76),
(50,60,62,68,70,72,78),
(50,62,64,70,72,74,80),
(50,64,68,76,80,84,92),
(50,66,70,78,82,86,94),
(50,68,72,80,84,88,96),
(50,70,76,84,88,92,98),
(50,70,78,86,92,98,100) )
# max beetle,spider,earwig,inchworm perl level
self.max_table = (
(2,0,0,0),
(2,1,1,1),
(2,1,1,1),
(3,1,1,1),
(3,2,1,1),
(4,2,1,1),
(4,2,1,1),
(5,3,2,2),
(5,4,2,2),
(6,5,2,2),
(6,6,2,2),
(7,7,2,2),
(8,8,2,2))
def game_reset(self):
"""Reset game variables."""
self.keys = [False, False, False, False, False]
self.score = 0
self.prev_score = 0
self.slow_down_time = False
self.time_delay = 0
self.menu_delay = 0
self.level_up_delay = 0
# swarm stage variables
self.swarmfn = None
self.swarm_count = 0
self.swarm_launch_delay = 0
self.swarm_monster = None
self.swarm_monster_lst = None
self.swarm_next_monster = 0
self.birthanddeathfn = None
# Reset the stop-watch timer.
self.cur_tick = 0
self.reset_ticks()
# reset spawn queue
self.spawn_queue_reset()
# random events
self.random_event_delay = 0
self.cur_level_random_event_delay = Game.START_RANDOM_EVENT_DELAY
# keep track of damaged mushrooms
self.damaged_mushrooms = []
#player info
self.score = 0
self.cur_level = 0
self.swarm_level = 0
self.player_lives = Game.START_PLAYER_LIVES
self.player_dead = False
self.slow_down_time = False
self.time_delay = 0
self.repeat_level = False
self.menu_delay = 0
self.level_up_delay = 0
self.clock = pygame.time.Clock()
self.cur_tick = 0
self.prev_tick = 0
for m in self.monsters:
m.kill()
self.ddts.empty()
self.mushroom_field.reset()
self.mushroom_field.populate_randomly()
# special events
self.the_ninth_millipede = False
self.scroll_mushroomfield_delay = 0
self.eight_spider_attack = False
def spawn_queue_reset(self):
"""Reset the spawn queue."""
self.spawn_queue['bees'] = 0
self.spawn_queue['beetles'] = 0
self.spawn_queue['dragonflies'] = 0
self.spawn_queue['earwigs'] = 0
self.spawn_queue['inchworms'] = 0
self.spawn_queue['mosquitos'] = 0
self.spawn_queue['spiders'] = 0
self.spawn_queue['ttl'] = self.get_ticks()
def level_reset(self):
"""Reset the current level."""
# remove all actors from the game
self.popups.clear()
self.players.empty()
self.playerMissiles.empty()
self.millipedes = []
for m in self.monsters:
m.kill()
for d in self.ddts:
if d.active:
d.kill()
self.spawn_queue_reset()
self.slow_down_time = False
self.player_dead = False
self.keys = [False, False, False, False, False]
def level_init(self):
"""Setup current level data."""
self.players.add( self.player )
self.playerMissile.reset()
self.playerMissiles.add( self.playerMissile )
def get_ticks(self):
"""Return game clock ticks."""
t = pygame.time.get_ticks()
dt = t - self.prev_tick
self.prev_tick = t
self.cur_tick += dt
return self.cur_tick
def one_up_and_eight_spiders(self):
"""check for 1-UP and 8 spider attack."""
event = False
n = self.prev_score % Game.LIFE_BONUS
m = self.score % Game.LIFE_BONUS
# m will be less than n should a 1-UP occur
if ((m-n) < 0):
event = True
self.player_lives += 1
# play player_1_up sound here...
if DEBUG:
print "PLAYER 1-UP!!!!!"
# check for 100,000 8 spider attack
n = self.prev_score % 100000
m = self.score % 100000
if ((m-n) < 0):
event = True
self.spawn_queue['spiders'] += 8
self.eight_spider_attack = True
if DEBUG:
print "Eight Spider Attack"
# prevent multiple 1-ups or multiple 8 spider attacks
if event:
self.prev_score = self.score
def reset_ticks(self):
"""Reset game tick counter. Must be called after a Pause."""
self.prev_tick = pygame.time.get_ticks()
def run(self):
"""Main game loop."""
while True:
self.actionfn()
def main(self):
"""Game run."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.JOYAXISMOTION:
#print "move pad: ", event.joy, event.axis, event.value
if event.axis == 1:
if event.value == 0:
self.keys[2] = False
self.keys[3] = False
elif event.value == 1:
self.keys[3] = True
elif event.value == -1:
self.keys[2] = True
elif event.axis == 0:
if event.value == 0:
self.keys[0] = False
self.keys[1] = False
elif event.value == 1:
self.keys[1] = True
elif event.value == -1:
self.keys[0] = True
elif event.type == pygame.JOYBUTTONUP:
#print "button up: ", event.joy, event.button
if event.joy == 0 and event.button == 0:
self.keys[4] = False
elif event.type == pygame.JOYBUTTONDOWN:
#print "button down: ", event.joy, event.button
if event.joy == 0 and event.button == 0:
self.keys[4] = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.keys[0] = True
elif event.key == pygame.K_RIGHT:
self.keys[1] = True
elif event.key == pygame.K_UP:
self.keys[2] = True
elif event.key == pygame.K_DOWN:
self.keys[3] = True
elif (event.key == pygame.K_SPACE) or \
(event.key == pygame.K_LCTRL):
self.keys[4] = True
elif event.key == pygame.K_ESCAPE:
sys.exit()
elif event.key == pygame.K_p:
self.prev_actionfn = self.actionfn
self.menu_delay = self.get_ticks()
self.actionfn = self.pause
pygame.mixer.pause()
elif event.key == pygame.K_m:
mushroom_field_print()
elif event.key == pygame.K_g:
(x, y) = game.player.rect.topleft
gx = x / MushroomField.MUSHROOM_WIDTH
gy = y / MushroomField.MUSHROOM_HEIGHT
print "Player position: (%d, %d) : (%d, %d)" % \
(x, y, gx, gy)
elif event.key == pygame.K_0:
self.swarm_init(Game.BEE_SWARM)
elif event.key == pygame.K_1:
self.swarm_init(Game.DRAGONFLY_SWARM)
elif event.key == pygame.K_2:
self.swarm_init(Game.MOSQUITO_SWARM)
elif event.key == pygame.K_3:
self.swarm_init(Game.BEEDRAGONFLY_SWARM)
elif event.key == pygame.K_4:
self.swarm_init(Game.BEEDRAGONFLYMOSQUITO_SWARM)
elif event.key == pygame.K_l:
self.birthanddeathfn = self.birth_and_death_init
elif event.key == pygame.K_r:
self.mushroom_field.populate_randomly()
elif event.key == pygame.K_o:
self.mushroom_field.reset()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
self.keys[0] = False
elif event.key == pygame.K_RIGHT:
self.keys[1] = False
elif event.key == pygame.K_UP:
self.keys[2] = False
elif event.key == pygame.K_DOWN:
self.keys[3] = False
elif event.key == pygame.K_SPACE or \
(event.key == pygame.K_LCTRL):
self.keys[4] = False
# save current score -- used to check for player 1-UP
self.prev_score = self.score
# swarm stage?
if self.swarmfn:
self.swarmfn()
# the 9th Millipede?
if self.the_ninth_millipede:
# scroll MushroomField by 1 row down each frame
dt = self.get_ticks() - self.scroll_mushroomfield_delay
if dt > Game.THE_NINTH_MILLIPEDE_DELAY:
self.scroll_mushroomfield_delay = self.get_ticks()
self.mushroom_field.row_down()
# do not allow monsters to spawn before a level up
# the millipede is king!
if not self.level_up_delay:
self.random_events()
self.spawn_monsters()
# do we need to planet some mushrooms?
if self.birthanddeathfn:
self.birthanddeathfn()
# update actors - move player, update missile, move the milliepede
# ddts, ddt collisions, millipedes, monsters, player, player-missle
self.ddts.update()
# check for ddt collisions
self.ddt_collisions = True
for d in self.ddts:
if d.active:
# check for millipede collisions
for m in self.millipedes:
m.collision(d.rect)
# check for collisions against all other monsters
m_lst = pygame.sprite.spritecollide( d, self.monsters, False)
for m in m_lst:
m.collision()
self.ddt_collisions = False
# check for time slow down
if self.slow_down_time:
cur_time = self.get_ticks()
if (cur_time - self.slow_down_time_ttl) > Game.SLOW_DOWN_TIME_TTL:
self.slow_down_time = False
if (cur_time - self.time_delay) > Game.SLOW_DOWN_TIME_TTL_DT:
for m in self.millipedes:
m.move()
self.monsters.update()
self.time_delay = cur_time
else:
for m in self.millipedes:
m.move()
self.monsters.update()
self.particles.update()
self.players.update(self.keys, self.playerMissile)
if self.playerMissile.active:
self.playerMissile.move()
"""
Check for:
1 - player death
2 - end of level
3 - repeat current level
"""
# player dead?
if self.player_dead:
# need to add a delay before this method is called....
self.player_lives -= 1
self.actionfn = self.player_die_init
# check to see if we need to repeat the level
self.millipede_snd.stop()
if len(self.millipedes) > 0:
self.repeat_level = True
# is end of level ?
elif not self.repeat_level and \
not self.swarmfn and \
(len(self.millipedes) == 0):
self.millipede_snd.stop()
if self.level_up_delay:
dt = self.get_ticks() - self.level_up_delay
if dt > Game.LEVEL_UP_DELAY:
self.level_up_delay = 0
self.level_up()
else:
self.level_up_delay = self.get_ticks()
# repeat current level?
elif self.repeat_level:
if self.level_up_delay:
dt = self.get_ticks() - self.level_up_delay
if dt > Game.LEVEL_UP_DELAY:
self.level_up_delay = 0
self.level_redo()
self.repeat_level = False
else:
self.level_up_delay = self.get_ticks()
# check for 1-up
self.one_up_and_eight_spiders()
# start drawing a frame
self.screen.blit(self.background, [0,0])
# player area boundaries
pygame.draw.line(self.screen,
(0,0,128),
(0,Game.ARENA_H-Game.PLAYER_H),
(Game.ARENA_W,Game.ARENA_H-Game.PLAYER_H))
pygame.draw.line(self.screen,
(0,0,128),
(0,Game.ARENA_H),
(Game.ARENA_W,Game.ARENA_H))
# draw actors
self.players.draw(self.screen)
if self.playerMissile.active:
self.playerMissiles.draw(self.screen)
self.mushroom_field.draw(self.screen)
self.ddts.draw(self.screen)
for m in self.millipedes:
m.draw(self.screen)
self.monsters.draw(self.screen)
self.particles.draw(self.screen)
self.popups.draw(self.screen)
self.draw_score()
pygame.display.flip()
self.clock.tick(Game.FRAME_RATE)
def main_menu_init(self):
"""Initialize main menu screen."""
self.start_img_idx = 0
self.start_img_delay = self.get_ticks()
self.actionfn = self.main_menu
def main_menu(self):
"""Main menu screen."""
start = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.JOYBUTTONDOWN:
if event.joy == 0 and event.button == 0:
start = True
elif event.type == pygame.KEYDOWN:
if (event.key == pygame.K_SPACE) or \
(event.key == pygame.K_LCTRL):
start = True
elif event.key == pygame.K_ESCAPE:
sys.exit()
self.screen.blit(self.background, [0,0])
self.screen.blit(self.title_img, [0,50])
dt = self.get_ticks() - self.start_img_delay
if dt > 275:
self.start_img_delay = self.get_ticks()
self.start_img_idx += 1
if self.start_img_idx == len(self.start_img):
self.start_img_idx = 0
self.screen.blit(self.start_img[self.start_img_idx],\
[Game.SCREEN_W/4,200])
pygame.display.flip()
self.clock.tick(Game.FRAME_RATE)
if start:
self.game_reset()
self.level_reset()
self.level_init()
self.actionfn = self.main
self.prev_actionfn = None
self.game_start()
self.reset_ticks()
def pause(self):
"""Pause game loop."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
self.actionfn = self.prev_actionfn
self.prev_actionfn = None
self.reset_ticks()
pygame.mixer.unpause()
elif event.key == pygame.K_ESCAPE:
sys.exit()
self.screen.blit(self.background, [0,0])
#text = self.font.render("Paused", 1, (255,255,255))
self.screen.blit(self.paused_img, [0,Game.SCREEN_H/3])
pygame.display.flip()
self.clock.tick(Game.FRAME_RATE)
def game_over(self):
"""Game Over Screen."""
GAME_OVER_DELAY = 2500
quit = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.JOYBUTTONDOWN:
if event.joy == 0 and event.button == 0:
quit = True
elif event.type == pygame.KEYDOWN:
if (event.key == pygame.K_SPACE) or \
(event.key == pygame.K_LCTRL) or \
(event.key == pygame.K_q):
quit = True
elif event.key == pygame.K_ESCAPE:
sys.exit()
self.screen.blit(self.background, [0,0])
self.screen.blit(self.gameover_img, [0,Game.SCREEN_H/3])
pygame.display.flip()
self.clock.tick(Game.FRAME_RATE)
dt = self.get_ticks() - self.menu_delay
if (dt > GAME_OVER_DELAY) or quit:
self.actionfn = self.main_menu_init
self.prev_actionfn = None
self.reset_ticks()
def game_start(self):
"""Similar to Increment level but to start a new game."""
self.cur_level += 1
self.p_table_idx = (self.cur_level % len(self.p_table)) - 1
self.swarm_level += 1
#self.spawn_millipedes()
self.millipedes.append(Millipede(self))
self.millipede_snd.play(-1)
def level_up(self):
"""Increment game level."""
self.cur_level += 1
self.p_table_idx = (self.cur_level % len(self.p_table)) - 1
self.swarm_level += 1
if self.swarm_level > 17:
self.swarm_level = 1
print "special level: ", self.swarm_level
swarm_stage = None
if self.swarm_level == 3:
swarm_stage = Game.BEE_SWARM
elif self.swarm_level == 8:
swarm_stage = Game.DRAGONFLY_SWARM
elif self.swarm_level == 11:
swarm_stage = Game.MOSQUITO_SWARM
elif self.swarm_level == 12:
if DEBUG:
print "SCROLL MUSHROOM FIELD NOW!!!!"
self.the_ninth_millipede = True
self.scroll_mushroomfield_delay = self.get_ticks()
elif self.swarm_level == 13:
self.the_ninth_millipede = False
self.scroll_mushroomfield_delay = 0
elif self.swarm_level == 14:
swarm_stage = Game.BEEDRAGONFLY_SWARM
elif self.swarm_level == 17:
swarm_stage = Game.BEEDRAGONFLYMOSQUITO_SWARM
# normal level
if swarm_stage == None:
# move mushroom field down by one row
self.mushroom_field.row_down()
# make some more millipedes...
self.spawn_millipedes()
# swarm stage
else:
self.swarm_init(swarm_stage)
# remove time dilation effect
self.slow_down_time = False
# alternate mushroom field color
self.mushroom_field.change_color()
def level_redo(self):
"""Redo current level."""
self.spawn_millipedes()
def spawn_millipedes(self):
"""Used by level generators and level restart."""
# generate a random list of x-positions
start_x = range(0,MushroomField.FIELD_WIDTH)
random.shuffle(start_x)
# reduce Millipede segments by one each level
# cycle repeats at Millipede.MAX_SEGMENTS
if self.cur_level == 0:
segments = Millipede.MAX_SEGMENTS
else:
segments = (self.cur_level - 1) % Millipede.MAX_SEGMENTS
body_len = Millipede.MAX_SEGMENTS - segments
if(body_len):
xpos = start_x[0] * MushroomField.MUSHROOM_WIDTH
self.millipedes.append(Millipede(self,body_len,(xpos,Millipede.start_y)))
for i in range(segments):
xpos = start_x[i+1] * MushroomField.MUSHROOM_WIDTH
self.millipedes.append(Millipede(self,1,(xpos,Millipede.start_y)))
self.millipede_snd.play(-1)
def swarm_init(self, swarm_num):
"""Initialize Wave Stage."""
self.swarm_count = 100
self.swarm_next_monster = 0
self.swarm_launch_delay = self.get_ticks()
self.swarmfn = self.swarm_spawn
self.swarm_points = Game.SWARM_BONUS_INC
self.swarm_monster = []
self.swarm_monster_lst = []
self.swarm_snd = []
# bee swarm
if swarm_num == Game.BEE_SWARM:
self.swarm_monster.append(Bee)
self.swarm_monster_lst.append(self.bees)
self.swarm_snd.append(self.bee_snd)
# dragonfly swarm
elif swarm_num == Game.DRAGONFLY_SWARM:
self.swarm_monster.append(Dragonfly)
self.swarm_monster_lst.append(self.dragonflies)
self.swarm_snd.append(self.dragonfly_snd)
# mosquito swarm
elif swarm_num == Game.MOSQUITO_SWARM:
self.swarm_monster.append(Mosquito)
self.swarm_monster_lst.append(self.mosquitos)
self.swarm_snd.append(self.mosquito_snd)
# bees and dragonflies
elif swarm_num == Game.BEEDRAGONFLY_SWARM:
self.swarm_monster.append(Bee)
self.swarm_monster_lst.append(self.bees)
self.swarm_snd.append(self.bee_snd)
self.swarm_monster.append(Dragonfly)
self.swarm_monster_lst.append(self.dragonflies)
self.swarm_snd.append(self.dragonfly_snd)
# bees, dragonflies, and mosquitos
elif swarm_num == Game.BEEDRAGONFLYMOSQUITO_SWARM:
self.swarm_monster.append(Bee)
self.swarm_monster_lst.append(self.bees)
self.swarm_snd.append(self.bee_snd)
self.swarm_monster.append(Dragonfly)
self.swarm_monster_lst.append(self.dragonflies)
self.swarm_snd.append(self.dragonfly_snd)
self.swarm_monster.append(Mosquito)
self.swarm_monster_lst.append(self.mosquitos)
self.swarm_snd.append(self.mosquito_snd)
self.swarm_snd[0].play()
def swarm_spawn(self):
"""Wave Stage loop."""
i = self.swarm_next_monster
if self.swarm_count > 0:
dt = self.get_ticks() - self.swarm_launch_delay
if dt > Game.SWARM_STAGE_SPAWN_DELAY:
m = self.swarm_monster[i](self)
m.add(self.monsters)
m.add(self.swarm_monster_lst[i])
self.swarm_count -= 1
self.swarm_launch_delay = self.get_ticks()
if (self.swarm_count % 10) == 0:
self.swarm_snd[i].play()
if DEBUG:
print "WAVE SPAWN MONSTER"
# make sure next group of monsters in the list spawns
self.swarm_next_monster += 1
if self.swarm_next_monster >= len(self.swarm_monster_lst):
self.swarm_next_monster = 0
# wait until the last group of monsters are gone before
# ending the swarm stage.
n = len(self.swarm_monster) - 1
if (self.swarm_count <= 0) and (len(self.swarm_monster_lst[n]) == 0):
self.swarmfn = None
# time to regrow or kill some mushrooms...
self.birthanddeathfn = self.birth_and_death_init
if DEBUG:
print "WAVE STAGE END"
def swarm_score_up(self, x, y):
"""Increment score during a swarm stage."""
# any time a swarm stage monster is hit with a DDT
# swarm_points are triple scored to a max of 1000
if self.ddt_collisions:
tmp = self.swarm_points
self.swarm_points = self.swarm_points * 3
if self.swarm_points > Game.SWARM_MAX_BONUS_POINTS:
self.swarm_points = Game.SWARM_MAX_BONUS_POINTS
self.score += self.swarm_points
self.popups.add(x, y, self.swarm_points)
self.swarm_points += Game.SWARM_BONUS_INC
if self.swarm_points > Game.SWARM_MAX_BONUS_POINTS:
self.swarm_points = Game.SWARM_MAX_BONUS_POINTS
if self.ddt_collisions:
# restore points
self.swarm_points = tmp
def stop_ninth_millipede(self):
"""Stop scrolling mushroom field stage."""
self.the_ninth_millipede = False
self.scroll_mushroomfield_delay = 0
def birth_and_death_init(self):
"""Initialize the Mushroom Birth and Death loop."""
self.birth_and_death_ttl = self.get_ticks()
self.birthanddeathfn = self.birth_and_death
self.birth_and_death_trigger = self.get_ticks()
def birth_and_death(self):
"""Mushroom Birth and Death loop."""
dt = self.get_ticks() - self.birth_and_death_ttl
if dt > 1000:
self.birthanddeathfn = None
else:
dt = self.get_ticks() - self.birth_and_death_trigger
if dt > 250:
self.mushroom_field.birth_and_death()
self.birth_and_death_trigger = self.get_ticks()
def player_die_init(self):
"""Initliaze Player death sequence."""
"""
remove any monsters from the screen
and setup mushroom regrowth sequence.
"""
self.level_reset()
self.player_die_mushroom_count = len(self.damaged_mushrooms)
self.player_die_delay = 0
self.actionfn = self.player_die
def player_die(self):
"""Player death loop."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
self.prev_actionfn = self.actionfn
self.menu_delay = self.get_ticks()
self.actionfn = self.pause
pygame.mixer.pause()
elif event.key == pygame.K_ESCAPE:
sys.exit()
"""
Restore any damaged mushrooms--one at a time--to full strength.
Also, revert any flowers back to mushrooms.
"""