-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplayer.py
executable file
·311 lines (270 loc) · 9.38 KB
/
player.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
#! /usr/bin/env python
#coding=utf-8
import pygame
import sys
import os.path
import time
import random
from consts import *
from msgbox import Msgbox
from floor import Floor
from npcs import UpStair, DownStair
from snakes import KingSnake
from character import load_images, Character
class Player(Character):
images = load_images(["worior.png", "worior2.png"])
def __init__(self, game, speed, location, gameboard):
super(Player, self).__init__(location, (CELL_SIZE,CELL_SIZE))
self.game = game
self.root = game.root
self.gameboard = gameboard
self.speed = speed
self.flyable = False
self.condition = "NORMAL"
self.on_power = False
self.get_top = False
self.health = 500
self.magic = 200
self.feature = "NONE"
self.defence = 0
self.ykeynum = 0
self.bkeynum = 0
self.rkeynum = 0
self.gkeynum = 0
self.swordkeynum = 0
self.snakedp = 0
self.snakerocknum = 0
self.money = 0
self.exp = 0
self.level = 1
STARTFLOOR = 1
self.currentfloor = Floor(STARTFLOOR)
self.visited_floors = {STARTFLOOR: self.currentfloor}
self.quests = []
def is_out(self):
if self.rect.left < self.gameboard.get_rect().left:
return True
if self.rect.right > self.gameboard.get_rect().right:
return True
if self.rect.top < self.gameboard.get_rect().top:
return True
if self.rect.bottom > self.gameboard.get_rect().bottom:
return True
return False
def update(self):
super(Player, self).update()
self.oldpos = self.rect.copy()
"""if self.speed != [0, 0]:
for i in range(CELL_SIZE/STEP):
self.rect.move_ip(self.speed)
self.image = self.next_image()
self.gameboard.blit(self.image, self.rect)
pygame.display.update()
pygame.time.delay(500)
"""
self.rect.move_ip(self.speed)
if self.is_out():
self.rect = self.oldpos
for npc in pygame.sprite.spritecollide(self, self.currentfloor.group, dokill=False):
for quest in self.quests:
if npc in quest.targets:
quest.execute(player, npc)
npc.do_collide(self)
if self.condition == "POISON":
if time.time() > self.condition_endtime:
self.condition = "NORMAL"
else:
if time.time() > self.condition_time + 1:
self.health -= 10
self.condition_time = time.time()
elif self.condition == "DIZZY":
if time.time() > self.condition_endtime:
self.condition = "NORMAL"
else:
if self.speed != [0, 0]:
hurt = random.randint(1, 2)
if hurt == 1:
self.health -= 100 if self.health > 0 else 0
self.magic -= 80 if self.magic > 0 else 0
self.feature = random.choice(["FIRE", "GRASS", "WATER", "SOIL", "SKY", "LIGHT", "SNOW"])
self.defence -= 60
self.speed = [0, 0]
if self.is_dead():
self.suicide()
def hurt(self, damage):
if damage - self.defence >= 0:
self.health -= damage - self.defence
def weaken(self, damage):
self.magic -= damage
if self.magic < 0:
self.hurt(damage - self.magic * 3)
self.magic = 0
def undo(self):
self.rect = self.oldpos
def is_dead(self):
if self.health <= 0:
return True
def suicide(self):
self.health = 0
pygame.mixer.music.stop()
pygame.mixer.music.load(os.path.join("sound","dead.aif"))
pygame.mixer.music.play(1)
msgbox = Msgbox("Game over!!!", (100,100))
msgbox.show()
self.kill()
sys.exit()
def goto_floor(self, floornum):
""" 跳转至指定楼层。
floornum: 去往楼层号
"""
if floornum == self.currentfloor.floornum:
return
f = self.visited_floors.get(floornum)
if f is None:
f = Floor(floornum)
self.visited_floors[floornum] = f
self.root.remove(self.currentfloor.group)
oldfloornum = self.currentfloor.floornum
self.currentfloor = f
self.root.add(self.currentfloor.group)
if floornum > oldfloornum:
for npc in self.currentfloor.group.sprites():
if isinstance(npc, DownStair):
self.rect.left = npc.rect.left + CELL_SIZE
self.rect.top = npc.rect.top
if self.currentfloor.floornum == 10:
if KingSnake.first_ften == True:
Msgbox("You get here, now. But you can't continue.").show()
KingSnake.first_ften = False
else:
for npc in self.currentfloor.group.sprites():
if isinstance(npc, UpStair):
if self.currentfloor.floornum == 7:
self.rect.left = npc.rect.left + CELL_SIZE
self.rect.top = npc.rect.top
elif self.currentfloor.floornum in [8, 12]:
self.rect.left = npc.rect.left
self.rect.top = npc.rect.top - CELL_SIZE
elif self.currentfloor.floornum == 9:
self.rect.left = npc.rect.left
self.rect.top = npc.rect.top + CELL_SIZE
else:
self.rect.left = npc.rect.left - CELL_SIZE
self.rect.top = npc.rect.top
def go_upstair(self):
self.goto_floor(self.currentfloor.floornum+1)
def go_downstair(self):
self.goto_floor(self.currentfloor.floornum-1)
def poison(self):
self.condition = "POISON"
Msgbox("Ouch! You've got POISON condition to hurt health!").show()
self.condition_time = time.time()
self.condition_endtime = time.time() + 30
def dizzy(self):
self.condition = "DIZZY"
Msgbox("Oops! You've got DIZZY condition to have a scaring time!").show()
self.condition_time = time.time()
self.condition_endtime = time.time() + 30
def buy_y(self):
if self.money >= 20:
self.money -= 20
self.ykeynum += 1
def buy_y2(self):
if self.money >= 80:
self.money -= 80
self.ykeynum += 5
def buy_b(self):
if self.money >= 30:
self.money -= 30
self.bkeynum += 1
def buy_b2(self):
if self.money >= 130:
self.money -= 130
self.bkeynum += 5
def buy_r(self):
if self.money >= 50:
self.money -= 50
self.rkeynum += 1
def buy_r2(self):
if self.money >= 220:
self.money -= 220
self.rkeynum += 5
def buy_g(self):
if self.money >= 80:
self.money -= 80
self.gkeynum += 1
def buy_g2(self):
if self.money >= 360:
self.money -= 360
self.gkeynum += 5
def buy_smalldr(self):
if self.exp >= 20:
self.exp -= 20
self.health += 130
def buy_largedr(self):
if self.exp >= 70:
self.exp -= 70
self.health += 500
def buy_defrk(self):
if self.exp >= 20:
self.exp -= 20
self.defence += 8
def buy_largedef(self):
if self.exp >= 150:
self.exp -= 150
self.defence += 60
def buy_snakerk(self):
if self.exp >= 20:
self.exp -= 20
self.snakerocknum += 1
def buy_magic(self):
if self.exp >= 80:
self.exp -= 80
self.magic += 90
def buy_level(self):
if self.exp >= 100:
self.exp -= 100
self.level += 1
self.health += 300
self.defence += 30
def buy_level2(self):
if self.exp >= 270:
self.exp -= 270
self.level += 3
self.health += 900
self.defence += 90
def pick_key(self, key):
attr = key + 'keynum'
setattr(self, attr, getattr(self, attr) + 1)
def use_key(self, key):
attr = key + 'keynum'
keynum = getattr(self, attr)
if keynum > 0:
setattr(self, attr, keynum - 1)
return True
return False
class CheatPlayer(Player):
def __init__(self, game, speed, location, gameboard):
super(CheatPlayer, self).__init__(game, speed, location, gameboard)
self.condition = "NORMAL"
self.flyable = True
self.on_power = False
self.get_top = False
self.health = 500000
self.magic = 200000
self.feature = "NONE"
self.defence = 100000
self.ykeynum = 100
self.bkeynum = 100
self.rkeynum = 100
self.gkeynum = 100
self.swordkeynum = 100
self.snakedp = 0
self.snakerocknum = 0
self.money = 1000
self.exp = 1000
self.level = 1
STARTFLOOR = 20
self.currentfloor = Floor(STARTFLOOR)
self.visited_floors = {STARTFLOOR: self.currentfloor}
self.quests = []