forked from griffy/IronTrotter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
339 lines (294 loc) · 12.5 KB
/
handler.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
from twisted.internet import reactor, protocol
import sys
from stats import Stats
import pickle
from netclient import TrotterSubFactory
from twisted.internet.task import LoopingCall
import random
from random import choice
import pygame
import colors
import map
import entity
from entity import Entity
from entity import is_boss, is_living, is_player, is_item, is_terrain, is_solid_terrain
import font
import sound
import viewport
import scores
import hud
import sprite
class Handler:
def __init__(self, screen):
self.title = pygame.image.load("images/titleScreen.png")
self.titlerect = self.title.get_rect()
self.screen = screen
self.f = TrotterSubFactory(self)
self.fontDrawer = font.Font("font/youmurdererbb_reg.ttf", 100, colors.RED)
self.counter = 5
self.drawText = False
pygame.mixer.init()
pygame.mixer.pre_init(44100, -16, 2, 2048)
self.titleMusic = sound.Sound("music/severedfifth_endofdays.ogg")
self.titleMusic.play()
musics = [
"severedfifth_drilldown.ogg",
"severedfifth_fightphilosophy.ogg",
"severedfifth_machinesofwar.ogg",
"severedfifth_repent.ogg",
"severedfifth_endofdays.ogg",
"severedfifth_foretoldrevelation.ogg",
"severedfifth_theblackening.ogg",
"severedfifth_fallout.ogg",
"severedfifth_forgottenheroes.ogg",
"severedfifth_politicold.ogg",
"severedfifth_theyprey.ogg"
]
self.gameMusic = pygame.mixer.Sound("music/"+choice(musics))
self.attackSound = pygame.mixer.Sound("sounds/erdie__sword01.wav")
self.ghastlySound = pygame.mixer.Sound("sounds/johnc__moan.wav")
self.hurtSound = pygame.mixer.Sound("sounds/wolfsinger__weirdbreath.wav")
self.lc = LoopingCall(self.titleevent)
self.lc.start(0.1)
self.hud = None
# create an empty map for the server to fill in at the lobby
self.map = map.Map(map.map_width, map.map_height)
self.player = None
def titleevent(self):
global counter
for event in pygame.event.get():
if event.type == pygame.QUIT:
reactor.stop()
elif event.type == pygame.KEYDOWN:
# --- KEY handlers go HERE ---
if event.key == pygame.K_m:
print "c"
elif event.key == pygame.K_RETURN:
self.titleMusic.stop()
self.gameMusic.play(-1)
print "DO A THING"
self.lc.stop()
self.lc = LoopingCall(self.lobbyevent)
self.lc.start(0.1)
elif event.key == pygame.K_x:
pygame.event.post(pygame.event.Event(pygame.QUIT))
self.screen.fill(colors.BLACK)
self.screen.blit(self.title, self.titlerect)
if self.counter == 0:
self.drawText = not self.drawText
self.counter = 5
self.counter -= 1
if self.drawText:
self.fontDrawer.draw(400,450, "PRESS ENTER TO START")
pygame.display.flip()
def lobbyevent(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
reactor.stop()
elif event.type == pygame.KEYDOWN:
# --- KEY handlers go HERE ---
if event.key == pygame.K_RETURN:
self.lc.stop()
self.lc = LoopingCall(self.gameevent)
self.lc.start(0.1)
elif event.key == pygame.K_x:
pygame.event.post(pygame.event.Event(pygame.QUIT))
self.screen.fill(colors.BLACK)
self.screen.blit(self.title, self.titlerect)
if self.counter == 0:
self.drawText = not self.drawText
self.counter = 5
self.counter -= 1
if self.drawText:
self.fontDrawer.draw(400,450, "PRESS ENTER TO START")
pygame.display.flip()
def gameevent(self):
# get user input and send to server
moved = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.player_quit_game()
reactor.stop()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_w or event.key == pygame.K_k:
moved = self.player_move_up()
elif event.key == pygame.K_a or event.key == pygame.K_h:
moved = self.player_move_left()
elif event.key == pygame.K_s or event.key == pygame.K_j:
moved = self.player_move_down()
elif event.key == pygame.K_d or event.key == pygame.K_l:
moved = self.player_move_right()
elif event.key == pygame.K_SPACE:
self.player_attack()
elif event.key == pygame.K_ESCAPE:
pygame.event.post(pygame.event.Event(pygame.QUIT))
if moved:
self.viewport.update_view()
self.pickup_item()
self.map.update(self.viewport)
if self.map.is_cleared() or self.player.stats.hp <= 0:
self.lc.stop()
self.map = None
self.lc = LoopingCall(self.gameoverevent)
self.lc.start(0.1)
else:
# draw updated
self.screen.fill(colors.BLACK)
self.map.draw_within(self.viewport)
self.hud.draw()
pygame.display.flip()
def gameoverevent(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
reactor.stop()
self.screen.fill(colors.BLACK)
self.fontDrawer.draw(400, 280, "GAME OVER")
self.hud.draw()
pygame.display.flip()
# called when we receive Updates from the server
def handleUpdate(self, update):
if update.idnum == 0 or (self.map is None and self.player.stats.hp <= 0):
return
entity = None
if is_living(update.enttype):
entity = self.map.layers[2].getById(update.idnum)
if entity is None:
entity = Entity(update.stats, update.enttype,
True, update.name, update.idnum)
self.map.layers[2].add(entity)
# the monster's not dead, so it's attacking. arrrgh
if update.stats.hp > 0 and self.player:
self.monster_attack(entity)
elif is_player(update.enttype):
entity = self.map.layers[2].getById(update.idnum)
if entity is None:
entity = Entity(update.stats, update.enttype,
True, update.name, update.idnum)
if self.player is None:
self.player = entity
x = pygame.display.get_surface().get_width()
y = pygame.display.get_surface().get_height()
self.hud = hud.HUD(self.player, x, y)
self.viewport = viewport.Viewport(self.player, int(x/32), int(y/32))
self.map.layers[2].add(entity)
elif is_item(update.enttype):
entity = self.map.layers[1].getById(update.idnum)
if entity is None:
entity = Entity(update.stats, update.enttype,
False, update.name, update.idnum)
self.map.layers[1].add(entity)
elif is_terrain(update.enttype):
entity = self.map.layers[0].getById(update.idnum)
if entity is None:
entity = Entity(update.stats, update.enttype,
is_solid_terrain(update.enttype),
update.name, update.idnum)
self.map.layers[0].add(entity)
entity.stats = update.stats
def score_kill(self, entity):
if is_boss(entity):
self.player.stats.score += scores.BOSS
elif is_player(entity):
self.player.stats.score += scores.PLAYER
else:
self.player.stats.score += scores.GHOST
def monster_attack(self, entity):
entity_x = entity.stats.x
entity_y = entity.stats.y
player_x = self.player.stats.x
player_y = self.player.stats.y
dist = abs(entity_x - player_x) + abs(entity_y - player_y)
if dist < 2:
self.hurtSound.play()
self.player.stats.hp -= 10
self.f.transport.sendLine(pickle.dumps(self.player.getUpdate(),2))
elif dist < 3:
self.ghastlySound.play()
def pickup_item(self):
item = self.map.item_under_entity(self.player)
if item:
# set the hp to 0 to remove it in next map update
item.stats.hp = 0
self.player.stats.score += scores.POTION
self.f.transport.sendLine(pickle.dumps(item.getUpdate(),2))
# give the player a hitpoint boost
self.player.stats.hp += random.randint(5, 25)
self.f.transport.sendLine(pickle.dumps(self.player.getUpdate(),2))
def player_attack(self):
self.player.sprite.do_animate()
direction = self.player.sprite.direction
x = self.player.stats.x
y = self.player.stats.y
if direction == sprite.LEFT:
if self.map.is_entity_blocked_left(self.player):
entity = self.map.layers[2].get(x-1, y)
if entity:
self.attackSound.play()
entity.stats.hp -= 25
if entity.stats.hp <= 0:
self.score_kill(entity)
self.f.transport.sendLine(pickle.dumps(entity.getUpdate(),2))
elif direction == sprite.RIGHT:
if self.map.is_entity_blocked_right(self.player):
entity = self.map.layers[2].get(x+1, y)
if entity:
self.attackSound.play()
entity.stats.hp -= 25
if entity.stats.hp <= 0:
self.score_kill(entity)
self.f.transport.sendLine(pickle.dumps(entity.getUpdate(),2))
elif direction == sprite.UP:
if self.map.is_entity_blocked_up(self.player):
entity = self.map.layers[2].get(x, y-1)
if entity:
self.attackSound.play()
entity.stats.hp -= 25
if entity.stats.hp <= 0:
self.score_kill(entity)
self.f.transport.sendLine(pickle.dumps(entity.getUpdate(),2))
elif direction == sprite.DOWN:
if self.map.is_entity_blocked_down(self.player):
entity = self.map.layers[2].get(x, y+1)
if entity:
self.attackSound.play()
entity.stats.hp -= 25
if entity.stats.hp <= 0:
self.score_kill(entity)
self.f.transport.sendLine(pickle.dumps(entity.getUpdate(),2))
def player_quit_game(self):
# set the player's health to 0 "killing" it
# update the server
self.player.stats.hp = 0
self.f.transport.sendLine(pickle.dumps(self.player.getUpdate(),2))
def player_move_up(self):
self.player.sprite.set_direction(sprite.UP)
if not self.map.is_entity_blocked_up(self.player):
# update player, send to server
self.player.stats.y -= 1
self.f.transport.sendLine(pickle.dumps(self.player.getUpdate(),2))
return True
return False
def player_move_left(self):
self.player.sprite.set_direction(sprite.LEFT)
if not self.map.is_entity_blocked_left(self.player):
# update player, send to server
self.player.stats.x -= 1
self.f.transport.sendLine(pickle.dumps(self.player.getUpdate(),2))
return True
return False
def player_move_right(self):
self.player.sprite.set_direction(sprite.RIGHT)
if not self.map.is_entity_blocked_right(self.player):
# update player, send to server
self.player.stats.x += 1
self.f.transport.sendLine(pickle.dumps(self.player.getUpdate(),2))
return True
return False
def player_move_down(self):
self.player.sprite.set_direction(sprite.DOWN)
if not self.map.is_entity_blocked_down(self.player):
# update player, send to server
self.player.stats.y += 1
self.f.transport.sendLine(pickle.dumps(self.player.getUpdate(),2))
return True
return False