-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
2077 lines (1953 loc) · 121 KB
/
model.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
import sys
import math
from random import *
import pygame
from pygame.locals import *
import enemy
import mapManager
import myHero
import plotManager
"""有3个透明画布(surface)在所有元素之上,一是用于画自然元素(如雪,雨);第二个是画全屏效果的;第三个是用于画击中时的血的溅射效果"""
# =============================================================================================================
# -------------------------------------------------------------------------------------------------------------
# ------------------------------------ stage running class ----------------------------------------------------
# -------------------------------------------------------------------------------------------------------------
# =============================================================================================================
class AdventureModel():
allElements = None # a big Group for all sprites in this stage
bg_size = () # 屏幕的宽高(二元组)
towerD = 10 # 单人模式为10(默认),双人模式为11
towerH = 160
blockSize = 72
ctrY = -130 # 右上角控件的水平偏移位置(像素)
language = 0 # 初始默认为英文,可在构造函数中设定
fntSet = []
remindedArea = []
win = False # 标记最终结果
stg = 1
curArea = 0
scrnSpd = 4 # 屏幕上下移动的速度,单位像素
delay = 120 # 延时变量,用于在不影响游戏正常运行的情况下给图片切换增加延迟
msgList = [] # 用于存储消息的列表(列表包含列表):[ [heroName, incident, cntDown (,sticker)], ... ]
subsList = [] # The buffer containing those newly opened chests' substances.
vibration = 0 # Cnt to indicate the vibration of the screen.
# 双人模式下的特殊变量
frontier = 0 # 两者中的较高像素值
frnLayer = 0 # 两者中的较高层数
heroes = [] # 保存hero对象的引用;可能为1个或2个
tomb = []
tower = None
screen = None # 保存屏幕对象的引用
clock = None
towerBG = None # 当前关卡的背景jpg
towerBGRect = None
nature = None # 自然元素的画布
spurtCanvas = None # 击中反馈溅血的画布(比你想象中的更万能!不只是能画血噢😄嘻嘻)
haloCanvas = None # boss出现时的全屏阴影画布
plotManager = None # 管理剧情信息
msgStick = {}
hostage = None
natON = True # 自然装饰效果是否开启
music = None # bgm (Sound对象)
controller = [] # 右上角控件
controllerOn = []
paused = True
musicOn = True
setBG = False
gameOn = True # 游戏循环标志,默认为True,玩家点击退出或游戏结束时变为False
# 本model构造函数说明:
# heroInfoList 是一个列表,列表的每一项是一个hero的信息,每一项信息包括heroNo和该英雄的keyDic。即形如:[ (heroNo1, keyDic1), (heroNo2, keyDic2) ]。可为1-2个。
def __init__(self, stg, areas, heroList, bg_size, screen, language, fntSet, musicObj, diffi, natON, monsDic, VHostage):
self.allElements = pygame.sprite.Group()
self.stg = stg
self.screen = screen
self.bg_size = bg_size
self.language = language
self.fntSet = fntSet
self.natON = natON
self.monsAcc = monsDic # 当前关卡的所有怪物名及其VMons对象组成的字典
# 右上角的控件图片 及其他控制器
self.controller = [pygame.image.load("image/quit.png").convert_alpha(),
pygame.image.load("image/BGMusic.png").convert_alpha(), pygame.image.load("image/mute.png").convert_alpha()]
self.controllerOn = [pygame.image.load("image/quitOn.png").convert_alpha(),
pygame.image.load("image/BGMusicOn.png").convert_alpha(), pygame.image.load("image/muteOn.png").convert_alpha()]
self.aimImg = pygame.image.load("image/aim.png").convert_alpha()
self.clock = pygame.time.Clock()
self.music = musicObj
self.msgStick = { "msg":pygame.image.load("image/tip.png").convert_alpha(),
"dlg":pygame.image.load("image/stg"+str(self.stg)+"/preFig.png").convert_alpha() }
# Initialize game canvas.
self.gameOn = True
self.paused = True
self.setBG = False
self.nature = None
if self.natON:
if self.stg == 1:
self.nature = mapManager.Nature(self.bg_size, self.stg, 8, 1)
elif self.stg == 2:
self.nature = mapManager.Nature(self.bg_size, self.stg, 1, 0)
elif self.stg == 3:
self.nature = mapManager.Nature(self.bg_size, self.stg, 8, 1)
elif self.stg == 4 or self.stg == 7:
self.nature = mapManager.Nature(self.bg_size, self.stg, 18, 0)
elif self.stg == 5:
self.nature = mapManager.Nature(self.bg_size, self.stg, 10, -1)
elif self.stg == 6:
self.nature = mapManager.Nature(self.bg_size, self.stg, 8, 1)
self.spurtCanvas = mapManager.SpurtCanvas( self.bg_size )
self.haloCanvas = mapManager.HaloCanvas( self.bg_size )
# Select and overlap the moveMons() method.
if self.stg == 1:
self.moveMons = moveMonsStg1
elif self.stg == 2:
self.moveMons = moveMonsStg2
elif self.stg == 3:
self.moveMons = moveMonsStg3
elif self.stg == 4:
self.moveMons = moveMonsStg4
elif self.stg == 5:
self.moveMons = moveMonsStg5
elif self.stg == 6:
self.moveMons = moveMonsStg6
elif self.stg == 7:
self.moveMons = moveMonsStg7
# Plot Manager & Effect Manager.
self.plotManager = plotManager.Dialogue( stg )
self.effecter = plotManager.Controller(bg_size)
self.remindedArea = [0] # 存储已加载过关卡对话的区域。
for msg in self.plotManager.getPre(self.curArea):
self.msgList.append( [ "message", msg, 150, self.msgStick["dlg"] ] )
# tower背景图片 及其移动速度
self.towerBG = pygame.image.load( "image/stg"+ str(self.stg) +"/towerBG.jpg" ).convert()
self.towerBGRect = self.towerBG.get_rect()
self.towerBGRect.left = (self.bg_size[0]-self.towerBGRect.width) // 2
self.towerBGRect.top = (self.bg_size[1]-self.towerBGRect.height) // 2
if diffi == 0:
dmgReduction = 0.6 # 伤害减轻
enemy.Monster.healthBonus = 0.8
doubleP = 0.4 # 宝箱爆率翻倍的概率
self.towerH = 40 # 每区域层数
elif diffi == 1:
dmgReduction = 1
enemy.Monster.healthBonus = 1
doubleP = 0.2
self.towerH = 60
elif diffi == 2:
dmgReduction = 1.2
enemy.Monster.healthBonus = 1.5
doubleP = 0
self.towerH = 80
# create the map (note: for good influency of the screen, please no more than 240 layers) --------------- 🏯
if len(heroList)>1:
self.towerD = 11
oriPos = ( (self.bg_size[0] - self.towerD*self.blockSize) // 2, self.bg_size[1]-self.blockSize )
if self.stg==1 or self.stg==6:
specialOn = (False, True, True)
else:
specialOn = (True, True, True)
# Build 3 areas and link them as one big tower.
tower0 = mapManager.AdventureTower(oriPos, self.blockSize, self.towerD, self.towerH, self.stg, 0, specialOn[0], doubleP)
oriPos = tower0.generateMap()
tower1 = mapManager.AdventureTower(oriPos, self.blockSize, self.towerD, self.towerH, self.stg, 1, specialOn[1], doubleP)
oriPos = tower1.generateMap()
tower2 = mapManager.AdventureTower(oriPos, self.blockSize, self.towerD, self.towerH, self.stg, 2, specialOn[2], doubleP)
tower2.generateMap()
self.areaList = [tower0, tower1, tower2]
self.curArea = 0
self.tower = self.areaList[self.curArea]
self.hostage = None
# create the hero -----------------🐷
self.tomb = []
self.heroes = []
for each in heroList: # 根据传入的VHero参数信息生成hero
if each[2] == "p1":
propPos = { "slot":(-300,290), "ammo":(-335,268), "bag":(-230,268), "health":(115,660) }
initPos = (tower0.boundaries[0]-self.blockSize-10, bg_size[1]-2*self.blockSize)
elif each[2] == "p2":
propPos = { "slot":(180,290), "ammo":(145,268), "bag":(250,268), "health":(595,660) }
initPos = (tower0.boundaries[0]-self.blockSize+10, bg_size[1]-2*self.blockSize)
hero = myHero.Hero(initPos, self.blockSize, each[0], propPos, dmgReduction )
hero.keyDic = each[1]
hero.haloCanvas = self.haloCanvas # 受伤反馈画布
self.heroes.append(hero)
self.allElements.add(hero)
# Initialize towers and heroes.
for tower in self.areaList:
# add elems of each area to the allElements and hero's checkList.
for key in tower.groupList:
for brick in tower.groupList[key]:
self.allElements.add( brick ) # 加入walls
if brick.category == "sideWall" or (brick.category == "baseWall" and brick.coord[1]==0):
for hero in self.heroes:
hero.checkList.add( brick )
for sup in tower.chestList:
if sup.category == "hostage": # 选出hostage挂在self.hostage上,并设置其VHero所需的信息。
self.hostage = sup
self.hostage.hp = VHostage.hp
self.hostage.pw = VHostage.pw
self.hostage.dmg = VHostage.dmg
self.hostage.no = VHostage.no
self.hostage.lvl = VHostage.lvl
self.allElements.add(sup) # 加入supply
for hero in self.heroes:
hero.checkList.add(sup)
for elem in tower.elemList:
self.allElements.add(elem)
for hero in self.heroes:
hero.checkList.add(elem)
# create natural impediments and monsters for each area.
if (self.stg==1):
if tower.area == 0:
makeMons( 0, tower.layer, 0.7, 1, tower )
elif tower.area == 1:
makeMons( 0, tower.layer, 0.65, 1, tower )
makeMons( 2, tower.layer, 0.45, 2, tower )
elif tower.area == 2:
makeMons( 0, tower.layer, 0.55, 1, tower )
makeMons( 2, tower.layer, 0.4, 2, tower )
makeMons( 20, 22, 1, 3, tower ) # boss必须出现,所以概率给1;层数给指定的层数21(20~22之间)
for i in range(2):
f = enemy.InfernoFire(self.bg_size)
self.allElements.add(f)
elif (self.stg==2):
if tower.area == 0:
makeMons( 0, tower.layer, 0.6, 1, tower )
else:
makeMons( 0, tower.layer, 0.6, 1, tower )
makeMons( 2, tower.layer, 0.4, 2, tower )
if tower.area == 2:
makeMons( 20, 22, 1, 3, tower )
elif (self.stg==3):
if tower.area == 0:
makeMons( 2, tower.layer, 0.8, 3, tower )
makeMons( 2, tower.layer, 0.6, 2, tower )
elif tower.area >= 1:
makeMons( 0, tower.layer, 0.6, 1, tower )
makeMons( 2, tower.layer, 0.5, 2, tower )
makeMons( 2, tower.layer, 0.6, 3, tower )
if tower.area == 2:
makeMons( 20, 22, 1, 4, tower )
elif (self.stg==4):
if tower.area == 0:
makeMons( 0, tower.layer, 0.65, 1, tower )
makeMons( 2, tower.layer, 0.55, 2, tower )
#makeMons( 4, 6, 1, 4, tower )
if tower.area >= 1:
makeMons( 0, tower.layer, 0.6, 1, tower )
makeMons( 2, tower.layer, 0.45, 2, tower )
makeMons( 4, tower.layer, 0.5, 3, tower )
elif (self.stg==5):
makeMons( 0, tower.layer, 0.7, 1, tower )
makeMons( 2, tower.layer, 0.6, 2, tower )
#makeMons( 6, 8, 1, 3, tower )
elif (self.stg==6):
makeMons( 0, tower.layer, 0.7, 1, tower )
makeMons( 2, tower.layer, 0.7, 2, tower )
if tower.area == 2:
makeMons( 20, 22, 1, 3, tower )
elif (self.stg==7):
makeMons( 0, tower.layer, 0.7, 1, tower )
# 随机指定任意个区域封锁者
i = 0
for minion in tower.monsters:
i += 1
self.allElements.add(minion)
if random()<0.1 or ( i==len(tower.monsters) and len(tower.goalieList)==0 ):
tower.goalieList.add( minion )
def go(self, themeColor, horns, heroBook, stgManager, diffi):
self.music.play(-1)
self.screen.fill( (0, 0, 0) )
self.screen.blit( self.towerBG, self.towerBGRect )
if self.stg==2:
c = enemy.Column(self.bg_size, self.tower.groupList)
self.allElements.add(c)
elif self.stg==3:
mist = enemy.Mist(self.bg_size)
elif self.stg==4:
ooze = enemy.Ooze(self.bg_size, self.blockSize, self.fntSet[0][1]) # 中文
elif self.stg==5:
blizzard = enemy.blizzardGenerator(self.bg_size)
for item in self.allElements:
self.screen.blit( item.image, item.rect )
while self.gameOn:
if not self.paused: # 若未 pause.
# respond to the player's ongoing keydown event.
# get the list including the boolean status of all keys:
key_pressed = pygame.key.get_pressed()
for hero in self.heroes:
if hero.category == "follower":
continue
if key_pressed[ hero.keyDic["leftKey"] ]:
hero.moveX( self.delay, "left" )
elif key_pressed[ hero.keyDic["rightKey"] ]:
hero.moveX( self.delay, "right" )
self.frontier = self.bg_size[1]
self.frnLayer = 0
for hero in self.heroes:
self.frontier = min(self.frontier, hero.rect.bottom)
self.frnLayer = max(self.frnLayer, hero.onlayer)
# move all if the screen need to be adjusted.
gap = ( self.bg_size[0] - (self.tower.boundaries[0]+self.tower.boundaries[1]) ) //2
if gap:
lvl = min(gap, 10) if gap>0 else max(gap, -10)
for tower in self.areaList:
tower.level( lvl )
for elem in self.allElements:
elem.level( lvl )
if ( self.tower.getTop(self.frnLayer) < self.bg_size[1]*0.42 ):
for elem in self.allElements:
elem.lift(self.scrnSpd)
for tower in self.areaList:
tower.lift(self.scrnSpd)
if self.stg==4:
ooze.lift(self.scrnSpd)
elif ( self.tower.getTop(self.frnLayer)+self.blockSize > self.bg_size[1]*0.7 ) and ( self.tower.getTop(-1)+2*self.blockSize>=self.bg_size[1] ) :
for elem in self.allElements:
elem.lift(-self.scrnSpd)
for tower in self.areaList:
tower.lift(-self.scrnSpd)
if self.stg==4:
ooze.lift(-self.scrnSpd)
# check hero's jump and fall:
for hero in self.heroes:
# 若处于跳跃状态,则执行跳跃函数
if hero.k1 > 0:
hero.jump( self.tower.getTop(hero.onlayer+1) )
# 否则,执行掉落函数
else:
fallChecks = self.tower.groupList[str(hero.onlayer-1)]
if self.stg==2 and hero.rect.bottom <= (c.rect.top+2): # 英雄的位置比石柱高时,在调用hero.fall进行掉落检查时加入石柱,调用完成后会自动清除
hero.checkList.add( c )
hero.fall(self.tower.getTop(hero.onlayer-1), fallChecks, self.stg, self.tower.heightList)
# repaint all elements
self.screen.blit( self.towerBG, self.towerBGRect )
for item in self.allElements:
self.moveMons( self, item, self.heroes ) # 分关卡处理所有的敌人(自然阻碍和怪兽)。由于是覆盖的函数,需要给self参数。
if item.category=="sideWall" or item.category=="lineWall" or item.category=="baseWall" or item.category=="specialWall" or \
item.category == "supAmmo" or item.category == "fruit" or item.category == "torch" or item.category == "medicine":
item.paint(self.screen)
elif item.category == "lineDecor":
item.alter()
self.screen.blit(item.image, item.rect)
elif item.category == "hostage" or item.category == "door" or item.category == "notice" or item.category == "exit":
item.paint(self.screen, self.spurtCanvas, self.fntSet[0], self.language)
# 处理投掷物:投掷物的move函数将返回三种情况:1.返回False,表示未命中;2.返回包含两个元素的元组,含义分别为投掷物的方向“right”或“left”,以及投掷物击中的坐标(x,y);
# 3.返回包含三个元素的元组,第三个元组为标志命中目标是否死亡。
elif item.category=="bullet" or item.category=="bulletPlus":
arrCheck = self.tower.monsters
if self.stg==2: # 第二关加上障碍物大石头、蛛网
for elem in self.tower.elemList:
#if elem.category=="webWall" and not elem.valid:
# continue
arrCheck.add(elem)
if item.category=="bullet":
item.move(arrCheck, self.spurtCanvas, self.bg_size)
else:
item.move(item, self.delay, arrCheck, self.spurtCanvas, self.bg_size) # 这里还要传入投掷物本身
self.screen.blit(item.image, item.rect)
elif item.category == "follower":
win = item.decideAction(self.delay, self.tower.heightList, self.tower.monsters, self.tower.porter)
if win:
self.music.stop()
self.gameOn = False
self.win = True
#return [True, self.heroes+self.tomb] # win: return True and exp
drawHealth( self.screen, item.rect.left+item.rect.width//2, item.rect.top-8, 0, 8, item.health, item.full, 1 )
item.paint(self.screen)
for hero in self.heroes:
# decide the image of Hero
hero.checkImg( self.delay, self.spurtCanvas, self.tower.monsters )
hero.paint( self.screen )
if len(hero.jmpInfo)>0: # 绘画跳跃烟尘效果
self.screen.blit( hero.jmpInfo[0], hero.jmpInfo[1] )
# 从hero的preyList信息列表中取击中信息。
for hitInfo in hero.preyList:
self.spurtCanvas.addSpatters( hitInfo[3], [2, 3, 4], [10, 11, 12], hitInfo[2], hitInfo[1] )
if hitInfo[4] and (hitInfo[4] in self.monsAcc): # 击中的目标死亡
self.spurtCanvas.addSouls( hitInfo[5], [5, 6], [15, 18, 20], (210,210,255,250), hitInfo[1], hero )
if self.monsAcc[ hitInfo[4] ].collec(): # 尝试搜集该怪物。若已收集,则返回False;否则收集成功,返回True。
self.msgList.append( ["message", ("New monster collected to Collection!","新的怪物已收集至图鉴!"), 150, self.msgStick["msg"] ] )
hero.preyList = [] # 每次刷新读取所有信息后,将list重置为空表
# 从hero的eventList事件列表中取事件信息,并将these newly opened chests加入self.subsList中。
for item in hero.eventList:
if item=="exp":
self.msgList.append( [hero, item, 60] )
else:
self.msgList.append( [hero, item, 60] )
self.subsList.append(item)
hero.eventList = []
# 第三关增加游戏难度 ---
if ( self.stg==3 ):
sprites = [self.tower.porter]
for hero in self.heroes:
sprites.append(hero)
mist.renew( self.delay, sprites )
self.screen.blit( mist.canvas, mist.canvasRect )
if self.curArea >= 1:
mist.pervade = True
if not ( self.delay % 60 ): # 每隔一段时间在屏幕范围内生成一波骷髅兵
for line in range( self.frnLayer-3, self.frnLayer+3, 2 ): # 起点,终点,跨度,变hero的偶数为groupList的奇数(hero.onlayer +- 4 - 1)
if ( 0 < line < self.towerH-1 ) and len(self.tower.monsters) < 120 and ( random() < 0.1 ):
skeleton = enemy.Skeleton(self.tower.groupList[str(line)], self.blockSize, line)
self.tower.monsters.add(skeleton)
self.allElements.add(skeleton)
elif (self.stg==4):
sprites = []
for hero in self.heroes:
sprites.append(hero)
for each in self.tower.monsters:
sprites.append(each)
ooze.rise( self.delay, self.screen, sprites, self.spurtCanvas )
if ( self.frnLayer>=30 ) and ( ooze.speed<=self.curArea+2 ): # 每个area的速度上限,如area0上限为2,area1上限为3.
ooze.speed += 1
self.msgList.append( [ "message", ("The Ooze speeds up rising!","泥沼上涨速度加快!"), 150, self.msgStick["msg"] ] )
elif (self.stg==5):
blizzard.storm(self.heroes, self.nature.wind, self.spurtCanvas)
if self.vibration > 0:
if (self.vibration % 2 == 0):
for elem in self.allElements:
elem.lift(4)
elem.level(4)
elif (self.vibration % 2 == 1):
for elem in self.allElements:
elem.lift(-4)
elem.level(-4)
self.vibration -= 1
# 绘制三层画布
if self.spurtCanvas:
self.spurtCanvas.update(self.screen)
if self.haloCanvas:
self.haloCanvas.update( self.delay, self.screen )
if self.nature:
self.nature.update(self.screen)
# 目标怪物
if self.delay <= 120:
for each in self.tower.goalieList:
self.addSymm( self.aimImg, enemy.getPos(each,0.5,0.5)[0]-480, enemy.getPos(each,0.5,0.5)[1]-360)
# 在迷雾/画布之后再画获得的补给品,否则会被迷雾/画布盖掉
for item in self.subsList:
if not item.reached:
item.subsMove()
self.screen.blit(item.substance, item.subsRect)
else:
self.subsList.remove(item)
# draw hero status info
for hero in self.heroes:
if hero.category == "follower":
continue
self.addSymm( hero.slot, hero.propPos["slot"][0], hero.propPos["slot"][1] )
self.addSymm( hero.brand, hero.propPos["slot"][0]-110, hero.propPos["slot"][1] )
self.addTXT( ("Level "+str(hero.lvl), "等级"+str(hero.lvl)), 0, (255,255,255), hero.propPos["slot"][0]-100, hero.propPos["slot"][1]+24 )
self.addSymm( hero.ammoImg, hero.propPos["ammo"][0], hero.propPos["ammo"][1] )
self.addTXT( (str(hero.arrow), str(hero.arrow)), 1, (255,255,255), hero.propPos["ammo"][0]+36, hero.propPos["ammo"][1] )
# 画背包物品
if len(self.effecter.SSList)==0:
if len(hero.bagBuf)>1:
nxtItem = hero.bagBuf[(hero.bagPt + 1) % len(hero.bagBuf)]
nxtRect = hero.bagImgList[nxtItem].get_rect()
nxtImg = pygame.transform.smoothscale(hero.bagImgList[nxtItem], ( int(nxtRect.width*0.7), int(nxtRect.height*0.7) ) )
nxtRect = self.addSymm( nxtImg, hero.propPos["bag"][0]-20, hero.propPos["bag"][1]+10 )
item = hero.bagBuf[hero.bagPt]
bagRect = self.addSymm( hero.bagImgList[item], hero.propPos["bag"][0], hero.propPos["bag"][1] )
self.addTXT( (str(hero.bag[item]),str(hero.bag[item])), 1, (255,255,255), hero.propPos["bag"][0]+36, hero.propPos["bag"][1] )
drawHealth( self.screen, hero.propPos["health"][0], hero.propPos["health"][1], 20, 18, hero.health, hero.full, 2 )
pos = pygame.mouse.get_pos()
bannerTuple = self.renderBanner(pos)
self.showMsg(bannerTuple[2]) # 将第3个section(rect)传递给showMsg函数。
menu = bannerTuple[3]
# check big events.
# 因为有的怪物(如戈仑石人)存在死亡延迟,故在杀死怪物的瞬间判断会不准确。故放在外面一直侦听。
if self.tower.area<=2 and len(self.tower.goalieList)==0 and self.tower.porter.locked: #(限于前三个区域)
self.tower.porter.unlock()
self.msgList.append( [ "message", ("The Area is unblocked!","区域封锁已解除!"), 150, self.msgStick["msg"] ] )
for hero in self.heroes:
if ( hero.rect.top >= self.bg_size[1] ):
hero.hitted(2, 0)
if ( hero.health <= 0 ):
self.heroes.remove(hero)
self.tomb.append(hero)
# 有一名英雄死亡,检查其死亡后heroes列表中的英雄情况。
if hero.category=="follower": # 失败情况1:要营救的对象死亡
self.music.stop()
self.gameOn = False
self.win = False
return [False, self.heroes+self.tomb]
else:
if self.hostage: # 失败情况2:当营救对象未加入时,所有玩家死亡
if (len(self.heroes)<=0): # 死亡的不是营救对象,但所有玩家死亡
self.music.stop()
self.gameOn = False
self.win = False
#return [False, self.heroes+self.tomb] # died: return False
else:
if (len(self.heroes)<=1): # 失败情况3:营救对象在队列中,除ta以外的所有玩家死亡
self.music.stop()
self.gameOn = False
self.win = False
#return [False, self.heroes+self.tomb] # died: return False
# 进入下一tower。
if hero.category == "follower":
continue
if hero.onlayer>=self.tower.layer and enemy.getPos(hero, 0.5, 0.5)[0]>self.tower.boundaries[1]+2*self.blockSize:
self.curArea += 1
self.tower = self.areaList[self.curArea]
hero.onlayer = 0
# 若进入的是新区域,则将区域对话加入消息列表。
if self.curArea not in self.remindedArea:
self.remindedArea.append(self.curArea)
for msg in self.plotManager.getPre(self.curArea):
self.msgList.append( [ "message", msg, 150, self.msgStick["dlg"] ] )
# 返回上一tower。
elif hero.onlayer<=0 and enemy.getPos(hero, 0.5, 0.5)[0]<self.tower.boundaries[0]-2*self.blockSize:
self.curArea -= 1
self.tower = self.areaList[self.curArea]
hero.onlayer = self.tower.layer
else:
# 透明灰色打底
if not self.setBG:
self.setBG = True
drawRect( 0, 0, self.bg_size[0], self.bg_size[1], (0,0,0,180), self.screen )
tip = choice( self.plotManager.tips )
# 加Board to represent some tips.
self.addSymm( pygame.image.load("image/cardBoard.png"), 0, 0 )
self.addSymm( pygame.image.load("image/Enter.png").convert_alpha(), 0, -60 )
self.addTXT( ["continue/pause","继续/暂停"], 0, (20,20,20), 0, -30)
# tip area.
drawRect( self.bg_size[0]//2-240, self.bg_size[1]//2+20, 480, 100, (210,180,120,120), self.screen )
topAlign = 50
for line in tip:
self.addTXT( line, 0, (0,0,0), 0, topAlign )
topAlign += 20
# handle controllers images and click events -----------------------------------
home = self.addSymm( self.controller[0], -260, self.ctrY)
BGMusic = self.addSymm( self.controller[1], -200, self.ctrY) if self.musicOn else self.addSymm( self.controller[2], -200, self.ctrY)
pos = pygame.mouse.get_pos()
if ( home.left < pos[0] < home.right ) and ( home.top < pos[1] < home.bottom ): # 退出(放弃)当前关卡
home = self.addSymm( self.controllerOn[0], -260, self.ctrY)
self.addTXT( ("quit","放弃"), 0, (60,60,60), -260, self.ctrY+40 )
elif ( BGMusic.left < pos[0] < BGMusic.right ) and (BGMusic.top < pos[1] < BGMusic.bottom ):
if self.musicOn:
BGMusic = self.addSymm( self.controllerOn[1], -200, self.ctrY)
self.addTXT( ("music off","关闭音乐"), 0, (60,60,60), -200, self.ctrY+40 )
else:
BGMusic = self.addSymm( self.controllerOn[2], -200, self.ctrY)
self.addTXT( ("music on","开启音乐"), 0, (60,60,60), -200, self.ctrY+40 )
# 一次性的鼠标点击或按键事件
for event in pygame.event.get():
if ( event.type == QUIT ):
pygame.quit()
sys.exit()
elif ( event.type == KEYDOWN ):
if not self.paused:
for hero in self.heroes:
if hero.category == "follower":
continue
if ( event.key == hero.keyDic["wrestleKey"] ): # 挥刀
if (hero.onlayer == 0) and self.hostage and ( pygame.sprite.collide_mask(hero, self.hostage) ):
# 将hostage变为一个hero并加入heroes队列。
pos = (self.hostage.rect.left, self.hostage.rect.bottom)
follower = myHero.Follower(self.bg_size, self.blockSize, self.hostage, hero, pos)
for tower in self.areaList:
for brick in tower.groupList["-1"]:
follower.checkList.add( brick )
for elem in tower.elemList:
follower.checkList.add(elem)
self.heroes.append(follower)
self.allElements.add(follower)
# 已经被带起,将原来的hostage删除。
self.hostage.kill()
self.hostage = None
else:
hero.whip()
elif ( event.key == hero.keyDic["jumpKey"] ): # 跳跃
if ( hero.k1 > 0 ) and ( hero.k2 == 0 ):
hero.k2 = 1
if not hero.trapper and hero.aground and ( hero.k1 == 0 ):
hero.k1 = 1
elif ( event.key == hero.keyDic["shootKey"] ) and hero.affected<0: # 射箭
hero.shoot( self.allElements, self.delay )
elif ( event.key == hero.keyDic["itemKey"] ): # 使用背包物品
hero.useItem( self.spurtCanvas )
elif ( event.key == hero.keyDic["downKey"] ): # 下跳
hero.shiftLayer(-2, self.tower.heightList)
elif ( event.key == hero.keyDic["bagKey"] ) and len(self.effecter.SSList)==0 and len(hero.bagBuf)>1: # 切换背包物品
self.effecter.addSmoothSwitch(hero.bagImgList[item], bagRect, 0.7, -20, -10, 10)
self.effecter.addSmoothSwitch(nxtImg, nxtRect, 1.4, 20, -10, 10)
hero.bagPt = (hero.bagPt + 1) % len(hero.bagBuf)
# elif ( event.key == pygame.K_u ): # 特殊技能
# self.hero.special()
if ( event.key == pygame.K_RETURN ): # 暂停/解除暂停
self.paused = not self.paused
self.setBG = False # 用以指示可以铺一层透明背景
elif event.type == pygame.MOUSEBUTTONUP: # 鼠标事件
if self.paused:
if ( home.left < pos[0] < home.right ) and ( home.top < pos[1] < home.bottom ): # 退出(放弃)当前关卡
self.music.stop()
self.gameOn = False
self.win = False
#return [False, self.heroes+self.tomb]
elif ( BGMusic.left < pos[0] < BGMusic.right ) and (BGMusic.top < pos[1] < BGMusic.bottom ):
if self.musicOn:
self.music.stop()
self.musicOn = False
else:
self.music.play(-1)
self.musicOn = True
else:
if ( menu.left < pos[0] < menu.right ) and ( menu.top < pos[1] < menu.bottom ):
self.paused = not self.paused
pygame.display.flip() # from buffer area load the pic to the screen
self.delay -= 1
if not self.delay:
self.delay = 240
self.clock.tick(60)
# ===================================================================
# Game Loop 结束,渲染 Stage Over 界面。
drawRect( 0, 0, self.bg_size[0], self.bg_size[1], themeColor, self.screen )
# 播放音效。
if self.win:
horns[0].play(0)
self.addTXT( ("Successful Rescue!","营救成功!"), 3, (255,255,255), 0, -120)
if self.stg<len(heroBook.accList) and not heroBook.accList[self.stg]:
heroBook.accList[self.stg] = True # 当前关卡通过,下一关的英雄角色解锁 ✔herobook
heroBook.heroList[self.stg].acc = True
hName = heroBook.heroList[self.stg].name
self.addTXT( ("New hero "+hName[0]+" is now accessible.","新英雄 "+hName[1]+" 已解锁。"), 1, (255,255,255), 0, -120)
# 修改关卡通过信息
line = plotManager.readFile( "r", 1, None )
info = stgManager.renewRec( line, self.stg-1, diffi, 0 )
plotManager.readFile( "w", 1, info)
else:
horns[1].play(0)
self.addTXT( ("Mission Failed.","营救失败。"), 3, (255,255,255), 0, -120)
# 不论胜负与否,都显示参与本关的英雄的经验值信息,并计算经验值获得。
y1 = 20
for hero in (self.heroes + self.tomb):
if not hero.category=="follower":
heroBook.heroList[int(hero.heroNo)].increaseExp(hero.expInc)
vHero = heroBook.heroList[hero.heroNo] # 从heroBook的列表中取VHero类型
# brand of the hero.
brd = self.addSymm(hero.brand, -120, y1)
# level.
bar = heroBook.drawExp( self.screen, brd.right+6, brd.bottom-26, int(vHero.exp), int(vHero.nxtLvl), 2 )
expTXT = ( "Level"+str(vHero.lvl)+" (EXP+"+str(hero.expInc)+")","等级"+str(vHero.lvl)+"(经验+"+str(hero.expInc)+")" )
self.addTXT( expTXT, 1, (240,230,230), bar.left+bar.width//2-self.bg_size[0]//2, (bar.top-40)-self.bg_size[1]//2 )
y1 += 90
while True:
# two Basic Buttons.
restart = drawRect( self.bg_size[0]//2-220, 560, 200, 60, themeColor, self.screen )
retreat = drawRect( self.bg_size[0]//2+20, 560, 200, 60, themeColor,self.screen )
pos = pygame.mouse.get_pos()
if ( restart.left < pos[0] < restart.right ) and ( restart.top < pos[1] < restart.bottom ):
drawRect( restart.left+5, restart.top+5, restart.width-10, restart.height-10, (210,210,210,60), self.screen )
if pygame.mouse.get_pressed()[0]:
horns[2].play(0)
return True # 返回True,则main中的循环继续。
elif ( retreat.left < pos[0] < retreat.right ) and ( retreat.top < pos[1] < retreat.bottom ):
drawRect( retreat.left+5, retreat.top+5, retreat.width-10, retreat.height-10, (210,210,210,60), self.screen )
if pygame.mouse.get_pressed()[0]:
horns[2].play(0)
return False # 返回False,则结束main中的循环。
self.addTXT( ("Retry","重试"), 2, (255,255,255), -120, 230)
self.addTXT( ("Home","主菜单"), 2, (255,255,255), 120, 230)
for event in pygame.event.get(): # 必不可少的部分,否则事件响应会崩溃
if ( event.type == QUIT ):
pygame.quit()
sys.exit()
pygame.display.flip() # from buffer area load the pic to the screen
self.clock.tick(60)
# ---- clear all elements in the model ----
def clearAll(self):
for each in self.allElements:
each.kill()
del each
for tower in self.areaList:
del tower
# ---- show feedback of hero motion ----
def showMsg(self, msgSect):
vaccant = True
for msg in self.msgList:
if msg[2] == 0: # 倒计时减为0时从列表删除
self.msgList.remove(msg)
continue
# 判断消息的类型。首先检查第一个信息是否为“message”,是则要在banner的消息框中显示。
if msg[0]=="message":
if vaccant:
ctr = (msgSect.left+msgSect.width//2-self.bg_size[0]//2, msgSect.top+msgSect.height//2-self.bg_size[1]//2 )
self.addSymm( msg[3], msgSect.left+30-self.bg_size[0]//2, ctr[1] ) # 消息栏左侧显示消息图标。
self.addTXT( msg[1], 1, (255,255,255), ctr[0], ctr[1])
msg[2] -= 1
vaccant = False
#若不是message字段,则表示是宝箱获得补给物品:
else:
if msg[1]=="exp":
txt = ( msg[0].itemDic[msg[1]][0]+ "+1", msg[0].itemDic[msg[1]][1]+ "+1" )
else:
txt = ( msg[0].itemDic[msg[1].category][0]+ "+" + str(msg[1].contains), msg[0].itemDic[msg[1].category][1]+ "+" + str(msg[1].contains) )
ctr = ( msg[0].rect.left+msg[0].rect.width//2-self.bg_size[0]//2, msg[0].rect.top-self.bg_size[1]//2-(60-msg[2]) )
self.addTXT( txt, 0, (255,255,255), ctr[0], ctr[1])
msg[2] -= 1 # 消息显示倒计时-1
# --- paint upper banner (contains 4 sections) ---
def renderBanner(self, pos):
# paint 4 background sections and get their rect.
sect1 = drawRect(20, 10, 60, 40, (0,0,0,120), self.screen) # Goalie Information.
sect2 = drawRect(100, 10, 80, 40, (0,0,0,120), self.screen) # Area Number.
sect3 = drawRect(200, 10, 660, 40, (0,0,0,120), self.screen) # Message Bar.
sect4 = drawRect(880, 10, 60, 40, (0,0,0,120), self.screen) # Menu Option.
# give banner info.
ctr = (sect1.left+sect1.width//2-self.bg_size[0]//2, sect1.top+sect1.height//2-self.bg_size[1]//2) # 更改为中心坐标系统的中心点参数
self.addSymm( pygame.image.load("image/goalie.png").convert_alpha(), ctr[0], ctr[1] )
self.addTXT( ( str(len(self.tower.goalieList)), str(len(self.tower.goalieList)) ), 1, (255,255,255), ctr[0], ctr[1])
ctr = (sect2.left+sect2.width//2-self.bg_size[0]//2, sect2.top+sect2.height//2-self.bg_size[1]//2)
self.addTXT( ("Area%d" % (self.curArea+1), "区域%d" % (self.curArea+1)), 1, (255,255,255), ctr[0], ctr[1] )
ctr = (sect4.left+sect4.width//2-self.bg_size[0]//2, sect4.top+sect4.height//2-self.bg_size[1]//2)
menu = self.addSymm( pygame.image.load("image/menu.png").convert_alpha(), ctr[0], ctr[1] )
if ( menu.left < pos[0] < menu.right ) and ( menu.top < pos[1] < menu.bottom ): #
menu = self.addSymm( pygame.image.load("image/menuOn.png").convert_alpha(), ctr[0], ctr[1] )
self.addTXT( ("menu","菜单"), 0, (255,255,255), ctr[0], ctr[1] )
return (sect1, sect2, sect3, sect4)
# ========================================================================
# ================= Game Loop 结束后的Stage总结界面 =======================
#def renderConclusion(self, heroBook):
# pass
# ========================================================================
# ================= 用于检测item为敌人的情况下,并移动该敌人 ================
# 将这一部分从go()分离出来的目的是不让主函数显得过于庞大,单独成为一个辅助函数有助于之后的维护和修改
# ========================================================================
def moveMons(self, item, heroes):
# This function should be overlapped in the initial constrction according to the stage.
pass
# ===========================================
# Surface对象; x,y为正负(偏离屏幕中心点)像素值,确定了图像的中点坐标
def addSymm(self, surface, x, y):
rect = surface.get_rect()
rect.left = (self.bg_size[0] - rect.width) // 2 + x
rect.top = (self.bg_size[1] - rect.height) // 2 + y
self.screen.blit( surface, rect )
return rect # 返回图片的位置信息以供更多操作
# ===========================================
# x,y为正负(偏离屏幕中心点)像素值,y则确定了文字行矩形的中点坐标(居中对齐)。
# 这样改动是为了和addSymm()函数保持一个相对统一的系统。
def addTXT(self, txtList, fntSize, color, x, y):
txt = self.fntSet[fntSize][self.language].render( txtList[self.language], True, color )
rect = txt.get_rect()
rect.left = (self.bg_size[0] - rect.width) //2 + x
rect.top = (self.bg_size[1] - rect.height) //2 + y
self.screen.blit( txt, rect )
return rect
# =============================================================================================================
# -------------------------------------------------------------------------------------------------------------
# ------------------------------------ stage running class ----------------------------------------------------
# -------------------------------------------------------------------------------------------------------------
# =============================================================================================================
class EndlessModel():
allElements = None # a big Group for all sprites in this stage
bg_size = () # 屏幕的宽高(二元组)
towerD = 10
blockSize = 72
ctrY = -130 # 右上角控件的水平偏移位置(像素)
language = 0 # 初始默认为英文,可在构造函数中设定
fntSet = []
stg = 1
scrnSpd = 4 # 屏幕上下移动的速度,单位像素
delay = 120 # 延时变量,用于在不影响游戏正常运行的情况下给图片切换增加延迟
msgList = [] # 用于存储消息的列表(列表包含列表):[[incident, cntDown]]
subsList = []
vibration = 0 # 用于震动全屏的参数
keyDic = []
monsters = None
hero = None
tower = None
screen = None # 保存屏幕对象的引用
clock = None
towerBG = None # 当前关卡的背景jpg
towerBGRect = None
nature = None
plotManager = None
haloCanvas = None # boss出现时的全屏阴影画布
msgStick = None
music = None # bgm (Sound对象)
controller = [] # 右上角控件
controllerOn = []
paused = True
musicOn = True
setBG = False
gameOn = True # model循环标志,默认为True,player点击退出或gameover时变为False
monsBroc = [ (), # mons生成手册:记录每个stg有哪些mons,比例分别为多少.当前是stg0,不记录任何东西。
( (1,0.8), (2,0.6) ), # stg1
( (1,0.75), (2,0.55) ),
( (1,0.9), (2,0.7), (3,0.7) ),
( (1,0.8), (2,0.6), (3,0.5) ), # stg4
( (1,0.85), (2,0.75) ),
( (1,0.85), (2,0.85) ),
( (1,0.7), (1, 0.4) ) ]
def __init__(self, stg, keyDic, bg_size, screen, language, fntSet, musicObj, natON, VHero):
self.allElements = pygame.sprite.Group()
self.stg = stg
self.screen = screen
self.bg_size = bg_size
self.language = language
self.keyDic = keyDic
self.fntSet = fntSet
self.plotManager = plotManager.Dialogue(self.stg)
self.msgStick = pygame.image.load("image/tip.png").convert_alpha()
self.nature = None
if natON:
if self.stg == 1:
self.nature = mapManager.Nature(self.bg_size, self.stg, 8, 1)
elif self.stg == 2:
self.nature = mapManager.Nature(self.bg_size, self.stg, 1, 0)
elif self.stg == 3:
self.nature = mapManager.Nature(self.bg_size, self.stg, 8, 1)
elif self.stg == 4 or self.stg == 7:
self.nature = mapManager.Nature(self.bg_size, self.stg, 18, 0)
elif self.stg == 5:
self.nature = mapManager.Nature(self.bg_size, self.stg, 10, -1)
elif self.stg == 6:
self.nature = mapManager.Nature(self.bg_size, self.stg, 8, 1)
self.spurtCanvas = mapManager.SpurtCanvas( self.bg_size)
self.haloCanvas = mapManager.HaloCanvas( self.bg_size)
self.effecter = plotManager.Controller( bg_size )
# create the hero
propPos = { "slot":(-300,290), "ammo":(-335,268), "bag":(-230,268), "health":(115,660) }
initPos = (self.bg_size[0]//2, self.bg_size[1]-self.blockSize)
self.hero = myHero.Hero(initPos, self.blockSize, VHero, propPos, 1)
self.allElements.add(self.hero)
self.hero.haloCanvas = self.haloCanvas
# Select and overlap the moveMons() method.
if self.stg == 1:
self.moveMons = moveMonsStg1
elif self.stg == 2:
self.moveMons = moveMonsStg2
elif self.stg == 3:
self.moveMons = moveMonsStg3
elif self.stg == 4:
self.moveMons = moveMonsStg4
elif self.stg == 5:
self.moveMons = moveMonsStg5
elif self.stg == 6:
self.moveMons = moveMonsStg6
elif self.stg == 7:
self.moveMons = moveMonsStg7
enemy.Monster.healthBonus = 1
# create the map (note: for good influency of the screen, please no more than 240 layers)
self.tower = mapManager.EndlessTower(bg_size, self.blockSize, self.towerD, stg)
self.tower.generateIni(self.screen)
for key in self.tower.groupList:
for brick in self.tower.groupList[key]:
self.allElements.add( brick ) # 加入walls
if brick.category == "sideWall" or brick.category == "baseWall":
self.hero.checkList.add( brick )
for sup in self.tower.chestList:
self.allElements.add(sup) # 加入supply
self.hero.checkList.add(sup)
for elem in self.tower.elemList:
self.allElements.add(elem)
self.hero.checkList.add(elem)
# tower背景图片
self.towerBG = pygame.image.load( "image/stg"+ str(stg) +"/towerBG.jpg" ).convert()
self.towerBGRect = self.towerBG.get_rect()
self.towerBGRect.left = (self.bg_size[0]-self.towerBGRect.width) // 2
self.towerBGRect.top = (self.bg_size[1]-self.towerBGRect.height) // 2
# 右上角的控件图片
self.controller = [pygame.image.load("image/quit.png").convert_alpha(), pygame.image.load("image/BGMusic.png").convert_alpha(), pygame.image.load("image/mute.png").convert_alpha()]
self.controllerOn = [pygame.image.load("image/quitOn.png").convert_alpha(), pygame.image.load("image/BGMusicOn.png").convert_alpha(), pygame.image.load("image/muteOn.png").convert_alpha(),]
self.clock = pygame.time.Clock()
self.music = musicObj
def go(self, themeColor, horns, stgManager):
self.music.play(-1)
# create natural impediments and monsters
if (self.stg==1):
infernoFires = pygame.sprite.Group()
for i in range(2):
f = enemy.InfernoFire(self.bg_size)
infernoFires.add(f)
self.allElements.add(f)
makeMons( 0, self.tower.curLayer, 0.65, 1, self.tower )
makeMons( 60, self.tower.curLayer, 0.45, 2, self.tower )
elif (self.stg==2):
c = enemy.Column(self.bg_size, self.tower.groupList)
self.allElements.add(c)
makeMons( 0, self.tower.curLayer, 0.6, 1, self.tower )
makeMons( 60, self.tower.curLayer, 0.4, 2, self.tower )
elif (self.stg==3):
mist = enemy.Mist(self.bg_size)
makeMons( 0, self.tower.curLayer, 0.8, 1, self.tower )
makeMons( 10, self.tower.curLayer, 0.6, 2, self.tower )
elif (self.stg==4):
ooze = enemy.Ooze(self.bg_size, self.blockSize//2, self.fntSet[0][1]) # 中文
#self.allElements.add(ooze)
makeMons( 0, self.tower.curLayer, 0.65, 1, self.tower )
makeMons( 2, self.tower.curLayer, 0.55, 2, self.tower )
elif (self.stg==5):
blizzard = enemy.blizzardGenerator(self.bg_size)
makeMons( 0, self.tower.curLayer, 0.7, 1, self.tower )
makeMons( 12, self.tower.curLayer, 0.6, 2, self.tower )
elif (self.stg==6):
makeMons( 0, self.tower.curLayer, 0.7, 1, self.tower )
makeMons( 2, self.tower.curLayer, 0.7, 2, self.tower )
elif (self.stg==7):
makeMons( 0, self.tower.curLayer, 0.7, 1, self.tower )
for minion in self.tower.monsters:
self.allElements.add(minion)
self.screen.fill( (0, 0, 0) )
self.screen.blit( self.towerBG, self.towerBGRect )
for item in self.allElements:
self.screen.blit( item.image, item.rect )
while self.gameOn:
if not self.paused: # 若未暂停
# respond to the player's ongoing keydown event
# get the list including the boolean status of all keys:
key_pressed = pygame.key.get_pressed()
if key_pressed[ self.keyDic["leftKey"] ]:
self.hero.moveX( self.delay, "left" )
elif key_pressed[ self.keyDic["rightKey"] ]:
self.hero.moveX( self.delay, "right" )
# move all if the screen need to be adjusted
if ( self.tower.getTop(self.hero.onlayer) < self.bg_size[1]*0.45 ):
for elem in self.allElements:
elem.lift(self.scrnSpd)
for each in self.tower.heightList:
self.tower.heightList[each] += self.scrnSpd
if self.stg==4:
ooze.lift(self.scrnSpd)
# check if it's needed to add More Layers
if ( self.hero.onlayer >= (self.tower.curLayer-10) ):
self.tower.addMore()
# 添加至allElements
for key in self.tower.groupList:
for brick in self.tower.groupList[key]:
if not brick in self.allElements:
self.allElements.add( brick )
if brick.category == "sideWall" or brick.category == "baseWall":
self.hero.checkList.add( brick )
for sup in self.tower.chestList:
if not sup in self.allElements:
self.allElements.add(sup)
self.hero.checkList.add(sup)
for elem in self.tower.elemList:
if not elem in self.allElements:
self.allElements.add(elem)
self.hero.checkList.add(elem)
# 增加新层的怪物
for each in self.monsBroc[self.stg]:
makeMons( self.tower.curLayer-10, self.tower.curLayer, each[1], each[0], self.tower )
for mons in self.tower.monsters:
if mons not in self.allElements:
self.allElements.add(mons)
# 删除低层的怪物(这里不好严格用layer来操作,因为飞行的monster没有特定的onlayer。因此用屏幕的高度为判断范围,超出两倍则判定删除)
for mons in self.tower.monsters:
if mons.rect.top >= 2*self.bg_size[1]:
mons.erase()
# check jump and fall:
if self.hero.k1 > 0:
self.hero.jump( self.tower.getTop( self.hero.onlayer+1 ) )