-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
515 lines (410 loc) · 17.4 KB
/
client.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
import pygame
import pickle
import socket
import threading
import zlib
import os
import sys
from player import *
from settings import *
from game import Game
from assets import *
from button import *
from task import Task
class State:
def __init__(self):
self.matrix = None
self.players: list[Player] = []
self.player_index = -1
self.socket = None
self.file: socket.SocketIO = None
self.state = GameState.HOME_SCREEN
self.round = 1
self.countdown = 5
self.character = 1
self.name = lambda: f"Player {self.player_index}"
self.task_manager = Task()
def socket_thread(state: State):
global matrix, players
buffer = b""
while True:
try:
new_data = state.file.readline()
except Exception as e:
print(e)
print("Server probably disconnected")
os._exit(1)
if not new_data.endswith(b"$|$\n"):
buffer += new_data
continue
buffer += new_data
if buffer.startswith(b"pdata:"):
data = buffer.removesuffix(b"$|$\n")
data = data.split(b":", 1)[1]
state.players = pickle.loads(data)
elif buffer.startswith(b"matrix:"):
data = buffer.removesuffix(b"$|$\n")
_, y, x, data = data.split(b":", 3)
shape = int(y.decode()), int(x.decode())
try:
data = zlib.decompress(data)
state.matrix = np.frombuffer(
data, dtype=np.uint8).reshape(shape)
except zlib.error as e:
print(buffer, e)
elif buffer == b"explosion$|$\n":
explosion_sound().play(0)
elif buffer == b"ghost$|$\n":
ghost_sound().play(0)
elif buffer.startswith(b"round:"):
game_round = buffer.removesuffix(b"$|$\n").split(b":")[1].decode()
state.state = GameState.RANKING_PHASE
state.round = game_round
elif buffer.startswith(b"countdown:"):
countdown = buffer.removesuffix(b"$|$\n").split(b":")[1].decode()
state.countdown = countdown
elif buffer == b"go$|$\n":
state.state = GameState.GAME_PHASE
elif buffer == b"ranking$|$\n":
state.state = GameState.WINNER_PHASE
else:
print("Unknown message", buffer)
buffer = b""
def waiting_phase(state: State):
window = pygame.display.set_mode((500, 200))
clock = pygame.time.Clock()
waiting_text = text(15, 'Waiting for host to start...', True, "#d7fcd4")
waiting_text_rect = waiting_text.get_rect(
center=(window.get_width()//2, 10))
pname_text = text(15, 'Enter name:', True, "#d7fcd4")
player_name = InputBox(185, 45, 250, 25, 15, state.name())
select_text = text(15, 'Choose your character', True, "#d7fcd4")
select_text_rect = select_text.get_rect(
center=(window.get_width()//2, player_name.rect.bottom + 30))
tutorial_text = text(10, "Press 'h' for help", True, "#d7fcd4")
tutorial_text_rect = tutorial_text.get_rect(
bottom=window.get_height() - 5, right=window.get_width())
characters = [
Button(pygame.transform.scale(PlayerSprite(i).get(), (40, 40)), ((50 * i) + 120, select_text_rect.bottom + 30),
"", font(20), "#d7fcd4", "White")
for i in range(1, 5)
]
def sumbit_name(name):
state.name = lambda: name
pygame.display.set_caption(f"BomberPy - {name}")
state.file.write(f"name:{name}\n".encode())
while True:
mouse_position = pygame.mouse.get_pos()
window.blit(get_background(), (0, 0))
if state.state != GameState.WAITING_PHASE:
break
# Main screen
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
for id, character in enumerate(characters):
try:
if character.checkForInput(mouse_position):
state.file.write(f"character:{id + 1}\n".encode())
state.character = id + 1
except Exception as e:
continue
elif event.type == pygame.KEYDOWN and event.key == pygame.K_h and not player_name.active:
state.prevstate = state.state
state.state = GameState.TUTORIAL
if len(player_name.text) > 15 and event.type == pygame.KEYDOWN and event.key != pygame.K_BACKSPACE:
continue
player_name.handle_event(event, sumbit_name)
window.blit(waiting_text, waiting_text_rect)
window.blit(pname_text, (20, 50))
player_name.draw(window)
window.blit(select_text, select_text_rect)
for id, character in enumerate(characters):
character.update(window, mouse_position)
pygame.draw.rect(window, "#00ff00",
((state.character * 50) + 100, select_text_rect.bottom + 10, 40, 40), 2)
window.blit(tutorial_text, tutorial_text_rect)
pygame.display.update()
clock.tick(FPS)
def game_phase(state: State):
bgmusic().play(-1)
clock = pygame.time.Clock()
game_obj = Game(state.socket, state.file, state.matrix.shape)
height, width = get_height_width(state.matrix.shape)
stats_window = pygame.Surface((width, CELL_SIZE * 2))
window = pygame.display.set_mode(
(width, height + stats_window.get_height()))
while True:
if state.state != GameState.GAME_PHASE:
break
current_player = state.players[state.player_index]
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
state.file.write(b"bomber_man\n")
stats_window.fill("#ffffff")
for i in range(current_player.bombs):
stats_window.blit(bomb_frame(0), (i * CELL_SIZE, 0))
for i in range(current_player.lives):
stats_window.blit(heart_sprites(), (i * CELL_SIZE, CELL_SIZE))
countdown = text(20, f"{state.countdown}s", True, C_INACTIVE)
countdown_rect = countdown.get_rect(
top=0, right=stats_window.get_width())
stats_window.blit(countdown, countdown_rect)
pts_text = font(20).render(
f"{current_player.points}pts", True, color=C_INACTIVE)
pts_rect = pts_text.get_rect(
top=countdown_rect.height, right=stats_window.get_width())
stats_window.blit(pts_text, pts_rect)
window.blit(stats_window, (0, 0))
game_obj.update(state.players, state.matrix)
window.blit(game_obj.surface, (0, stats_window.get_height()))
pygame.display.update()
clock.tick(FPS)
bgmusic().stop()
def ranking_phase(state: State, with_coutdown):
bgmusic().stop()
window = pygame.display.set_mode((500, 200))
clock = pygame.time.Clock()
exit_btn = Button(None, (250, 100),
" Exit ", font(20), "#d7fcd4", "White")
run = True
while run:
if state.state not in [GameState.RANKING_PHASE, GameState.WINNER_PHASE]:
break
mouse_position = pygame.mouse.get_pos()
window.blit(get_background(), (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if exit_btn.checkForInput(mouse_position):
run = False
ranked_players = sorted(
state.players, key=lambda x: x.points, reverse=True)
msg = f"Round {state.round} will start in {state.countdown}" if with_coutdown else f"{ranked_players[0].name} wins!!!"
countdown = text(15, msg, True, "#d7fcd4")
countdown_rect = countdown.get_rect(center=(window.get_width()//2, 10))
window.blit(countdown, countdown_rect)
if len(ranked_players) > 0:
curr_rank = text(15, f"-- Player Ranks --", True, "#d7fcd4")
curr_rank_rect = curr_rank.get_rect(
center=(window.get_width()//2, countdown_rect.bottom + 50))
window.blit(curr_rank, curr_rank_rect)
offset = curr_rank_rect.bottom + 10
for ranked_player in ranked_players:
ranking = text(
15, f"{ranked_player.name} - {ranked_player.points} pts", True, "#d7fcd4")
rect = ranking.get_rect(center=(window.get_width()//2, offset))
window.blit(ranking, rect)
offset += rect.height + 10
# window.blit(winner, (0, 50))
# exit_btn.update(window, mouse_position)
pygame.display.update()
clock.tick(FPS)
def tutorial(state):
help_msgs = f"""
Point System:
* Clear Box +{PTS_BOX}pts
* Kill opponent +{PTS_KILL}pts
* Suicide {PTS_SUICIDE}pts
Note:
* Whoever has the highest score will
be the winner
* Each round has a maximum of 3 minutes
* After each round, life will be
converted to pts. 1 life = {LIFE_PTS_CONVERTION} pts
"""
window = pygame.display.set_mode((650, 650))
clock = pygame.time.Clock()
exit_text = text(10, "Press 'esc' to exit", True, "#d7fcd4")
exit_text_rect = exit_text.get_rect(
bottom=window.get_height() - 5, right=window.get_width())
run = True
while run:
window.blit(get_background(), (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
state.state = state.prevstate
run = False
texts = text(20, "Tutorial", True, "#d7fcd4")
rect = texts.get_rect(centerx=window.get_width()//2, top=10)
window.blit(texts, rect)
# ================
# Arrow
# ================
img = pygame.transform.scale(kbd_arrow(), (100, 60))
img_rect = img.get_rect(left=0, top=rect.bottom + 20)
window.blit(img, img_rect)
texts = text(15, "Move the player", True, "#d7fcd4")
rect = texts.get_rect(
left=img_rect.right + 10, centery=img_rect.centery)
window.blit(texts, rect)
# ================
# Space
# ================
img = pygame.transform.scale(kbd_space(), (100, 40))
img_rect = img.get_rect(left=0, top=img_rect.bottom)
window.blit(img, img_rect)
texts = text(15, "Place the bomb", True, "#d7fcd4")
rect = texts.get_rect(
left=img_rect.right + 10, centery=img_rect.centery)
window.blit(texts, rect)
# ================
# Power up
# ================
texts = text(15, "Power Ups:", True, "#d7fcd4")
rect = texts.get_rect(left=0, top=img_rect.bottom + 30)
window.blit(texts, rect)
msg = [(heart_sprites(), "Increase life"),
(bomb_add_sprite(), "Increase bomb capacity"),
(movement_speed_sprite(), "Increase movement speed"),
(expl_range_add_sprite(), "Increase explosion range")]
for sprite, message in msg:
img = pygame.transform.scale(sprite, (30, 30))
img_rect = img.get_rect(left=30, top=rect.bottom + 10)
window.blit(img, img_rect)
texts = text(15, message, True, "#d7fcd4")
rect = texts.get_rect(
left=img_rect.right + 10, centery=img_rect.centery)
window.blit(texts, rect)
# ================
# Curse
# ================
texts = text(15, "Curse:", True, "#d7fcd4")
rect = texts.get_rect(left=0, top=img_rect.bottom + 10)
window.blit(texts, rect)
# ================
# Skull
# ================
img = pygame.transform.scale(skull_sprite(), (30, 30))
img_rect = img.get_rect(left=30, top=rect.bottom + 10)
window.blit(img, img_rect)
texts = text(15, "Decrease life without dying",
True, "#d7fcd4", )
rect = texts.get_rect(
left=img_rect.right + 10, centery=img_rect.centery)
window.blit(texts, rect)
# ================
# Note
# ================
for msg in help_msgs.split("\n"):
texts = text(15, msg, True, "#d7fcd4")
rect = texts.get_rect(left=0, top=rect.bottom + 5)
window.blit(texts, rect)
# exit text
window.blit(exit_text, exit_text_rect)
pygame.display.update()
clock.tick(FPS)
def home_screen(state: State):
window = pygame.display.set_mode((350, 200))
clock = pygame.time.Clock()
bomberman_text = text(30, 'Bomberman', True, "#d7fcd4")
waiting_text_rect = bomberman_text.get_rect(
center=(window.get_width()//2, 20))
host_text_pos = (10, waiting_text_rect.bottom + 30)
host_text = text(15, 'Host: ', True, "#d7fcd4")
host_input = InputBox(host_text.get_width(),
host_text_pos[1] - 5, 250, 25, 15, "localhost")
port_text_pos = (10, host_text_pos[1] + 30)
port_text = text(15, 'Port: ', True, "#d7fcd4")
port_input = InputBox(port_text.get_width(),
port_text_pos[1] - 5, 250, 25, 15, "8888")
connect_btn = Button(None, (window.get_width()//2, window.get_height() - 40),
" Connect ", font(20), "#d7fcd4", "White")
connect_btn.border = True
data = {
"msg": None,
"host": "localhost",
"port": 8888
}
def connect():
data["msg"] = "Failed to server"
try:
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((data["host"], data["port"]))
client_file = socket.SocketIO(client_socket, "rwb")
state.file = client_file
state.socket = client_socket
except Exception as e:
data["msg"] = "Failed to connect to server"
def reset_msg():
data["msg"] = None
state.task_manager.add(3000, reset_msg)
return
# the server will sent the player index once we connect
player_index = client_file.readline().rstrip()
player_index = int.from_bytes(player_index, "big")
state.player_index = player_index
pygame.display.set_caption(f"BomberPy - Player {player_index}")
threading.Thread(target=socket_thread, args=(
state,), daemon=True).start()
state.state = GameState.WAITING_PHASE
def set_host(x):
data["host"] = x
def set_port(x):
data["port"] = int(x)
need_connect = False
while True:
mouse_position = pygame.mouse.get_pos()
window.blit(get_background(), (0, 0))
if state.state != GameState.HOME_SCREEN:
break
# Main screen
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if connect_btn.checkForInput(mouse_position):
# force update message
# connecting to server is blocking the thread
# thats why we need to put it in end, so we can output msg
data["msg"] = "Connecting..."
need_connect = True
host_input.handle_event(event, set_host)
port_input.handle_event(event, set_port)
window.blit(bomberman_text, waiting_text_rect)
window.blit(host_text, host_text_pos)
host_input.draw(window)
window.blit(port_text, port_text_pos)
port_input.draw(window)
connect_btn.update(window, mouse_position)
if data["msg"]:
message = text(10, data["msg"], True, "#d7fcd4")
message_rect = message.get_rect(bottomright=(
window.get_width(), window.get_height()))
window.blit(message, message_rect)
pygame.display.update()
state.task_manager.tick()
clock.tick(FPS)
if need_connect:
connect()
need_connect = False
def main():
pygame.init()
state = State()
# start the app
while True:
if state.state == GameState.HOME_SCREEN:
home_screen(state)
elif state.state == GameState.WAITING_PHASE:
waiting_phase(state)
elif state.state == GameState.RANKING_PHASE:
ranking_phase(state, True)
elif state.state == GameState.GAME_PHASE:
game_phase(state)
elif state.state == GameState.WINNER_PHASE:
ranking_phase(state, False)
elif state.state == GameState.TUTORIAL:
tutorial(state)
if __name__ == "__main__":
main()