-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinter.py
175 lines (161 loc) · 7.05 KB
/
inter.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
# 包含属于entity player monster等
# 因为这部分代码需要访问scene所以不在entity.py里
# import asyncio
import pygame
import constants as c
from entity import *
from scene import *
#第一个全局变量
thisMap=Mapper(1,1)
dialoger=dialog()
class player(creature):
money:int
readToInteract:bool
def keyboard(self, keys:pygame.key.ScancodeWrapper): # 捕捉键盘信息
allowF=thisMap.moveRequest
if c.alwaysAllow:allowF=lambda x,y,entity : True
#python有for-else语句但没有 elfor 有什么让这段代码美观的方案吗??
for i in c.KeyboardLeft:
if keys[i]: self.tryMove(-1,0,allowF);break
else:
for i in c.KeyboardRight:
if keys[i]: self.tryMove(1,0,allowF);break
else:
for i in c.KeyboardUp:
if keys[i]: self.tryMove(0,-1,allowF);break
else:
for i in c.KeyboardDown:
if keys[i]: self.tryMove(0,1,allowF);break
# 外挂 加速 加炸弹 穿墙 获得金钱 加血 报告属性并崩溃
if c.AllowCheat:
for i in c.KeyboardSpeedUp:
if keys[i]: self.speed += 1
for i in c.KeyboardSpeedDown:
if keys[i]: self.speed -= 1
for i in c.KeyboardBombUp:
if keys[i]:
self.bombRange += 1
self.bombSum += 1
for i in c.KeyboardBombDown:
if keys[i]:
self.bombRange -= 1
self.bombSum -= 1
for i in c.KeyboardCrossWall:
if keys[i]: c.alwaysAllow = not c.alwaysAllow
for i in c.KeyboardMoneyUp:
if keys[i]: self.money += 10
for i in c.KeyboardMoneyDown:
if keys[i]: self.money -= 10
for i in c.KeyboardHealth:
if keys[i]: self.hpPlus()
for i in c.KeyboardCrash:
if keys[i]:
print(f"speed:{self.speed},\r\nbombRange:{self.bombRange},\r\nbombSum:{self.bombSum},\r\nmoney:{self.money},\r\nhp:{self.hp}")
raise Exception("Crash")
for i in c.KeyboardBomb:
if keys[i]:self.putBomb(thisMap.addEntity);break
def reRegister(self, gx:int, gy:int, initInMap:Callable, force:bool=True):
return super().reRegister(gx,gy,initInMap,force)
def __init__(self, id:int, gx:int, gy:int, imagesdir:str, initInMap:Callable|None =None, speed:int=c.IntialSpeed, hp:int=c.IntialHp, layer:int=9):
if initInMap==None : initInMap=thisMap.addEntity# player切换地图的时候不要忘了重新在地图注册
assert initInMap is not None
super().__init__(id,gx,gy,imagesdir,initInMap,speed,hp,layer)
self.money=0
self.cankick=False
self.hp+=c.IntialPlayerHp-c.IntialHp
def pickup(self, w:List[List[dict[str,Any]]]):# 捡东西
if w[self.gx][self.gy]["type"]=="object":
match w[self.gx][self.gy]["content"]: # content 对应掉落物类型
case 0:print('???a empty object???')
case 1:self.bombSum+=1
case 2:self.hpPlus()
case 3:self.speed=c.IncreasedSpeed
case 4:self.bombRange+=1
case 5:self.money+=1
case 6:self.cankick=True
w[self.gx][self.gy]["type"]="field"
w[self.gx][self.gy]["render"]=None# Warning
def clock(self, mapper:Mapper):
self.pickup(mapper.mp)
super().clock(mapper.moveUpdate)
if mapper.mp[self.gx][self.gy].get("teleportTo") :
tmp=mapper.mp[self.gx][self.gy]["content"]
# 如果 content为-1必须按f交互才能切换 ; 传送的两种形式:交互(按F)传送和直接传送
if tmp!=-1 or tmp==-1 and self.readToInteract:
changeMap(*mapper.mp[self.gx][self.gy]["teleportTo"])# 参数为传到哪里
# print(self.readToInteract)
# 处理对话交互
if self.readToInteract and mapper.mp[self.gx][self.gy].get("interact") and dialoger.content==None: # 只有在没有绘制对话框时才可交互
t=thisMap.mp[self.gx][self.gy]["interact"]
if 'function'in str(type(t[0])) : dialoger(t[0](self,mapper),t[1]) # 这种应该没用到,协程需要访问地图的备选方案
else : dialoger(*t) # 将协程iter传进去
def overlap(self, other:entityLike):
super().overlap(other)
if (self.gx,self.gy)==(other.gx,other.gy):
if "monster" in str(type(other)) :# 只有与monster接触时扣血
self.hpMinus()
def delete(self):
super().delete()
raise Exception("GAMEOVER") # 通过异常处理死亡和结局
maps:List[Mapper]=[]
def changeMap(mapid:int, gx:int, gy:int):
global thisMap
mee=thisMap.me
thisMap.mp[thisMap.me.gx][thisMap.me.gy]["entity"].remove(mee) # 在原地图删除
thisMap.me=None
thisMap=maps[mapid]
mee.reRegister(gx,gy,thisMap.addEntity) # player切换地图的时候重新在地图注册
thisMap.me=mee
changeMusic(mapid)
# thisMapId = mapid # 表示当前地图的id 用于changeMusic
def catchKeyboard(nowplayer:player, nowdialog:dialog): # 处理所有键盘输入的函数,集合player.keyboard() dialog.keyboard()
keys = pygame.key.get_pressed()
if nowdialog.content==None: # 没有对话时才接收其他输入
nowplayer.keyboard(keys)
for i in c.KeyboardInteract: # readToInteract 的设置
if keys[i] : nowplayer.readToInteract=True;break
else : nowplayer.readToInteract=False
nowdialog.keyboard(keys)
def changeMusic(mapid):
'''
global thisMapId
# 更换背景音乐
# if mapid == 0:
# for i in range(0, len(backgroundMusic)):
# stop_music(backgroundMusic[i])
# play_music(backgroundMusic[1])
# elif mapid == 1:
# for i in range(0, len(backgroundMusic)):
# stop_music(backgroundMusic[i])
# play_music(backgroundMusic[1])
if mapid == 2:
for i in range(0, len(backgroundMusic)):
stop_music(backgroundMusic[i])
play_music(backgroundMusic[2])
if thisMapId == 2:
for i in range(0, len(backgroundMusic)):
stop_music(backgroundMusic[i])
play_music(backgroundMusic[1])
'''
for i in range(0, len(backgroundMusic)):
stop_music(backgroundMusic[i])
play_music(backgroundMusic[mapid+1])
def play_music(music:pygame.mixer.Sound):
music.play(-1)
def stop_music(music:pygame.mixer.Sound, time=1000):
music.fadeout(time)
def play_sound(sound:pygame.mixer.Sound):
sound.play(1)
"""
音乐及音效定义
直接在此处加入即可
"""
pygame.mixer.init()
backgroundMusic = [
pygame.mixer.Sound('./assets/music/Home_Screen.ogg'),
pygame.mixer.Sound('./assets/music/Queen-Gardens.flac'),
pygame.mixer.Sound('./assets/music/outside.ogg'),
pygame.mixer.Sound('./assets/music/shop.ogg'),
pygame.mixer.Sound('./assets/music/City-of-Tears.flac')
]
backgroundSound = []