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
|
import pygame
import json
from ..command import Command
import logging
from ..input import Input
import os
logger = logging.getLogger(__name__)
class GameBase:
def __init__(self, screen, serial, instance):
self.screen = screen
self.serial = serial
self.instance = instance
self.font = pygame.font.Font(None, 36)
self.is_running = True
self.opponent_dead = False
self.my_score = 0
self.opponent_score = 0
self.waiting = False
self.score_display_time = 0
self.BLACK = (0, 0, 0)
self.WHITE = (255, 255, 255)
self.input_handler = Input(debug=os.getenv("DEVELOPMENT", "").lower() != "")
def handle_common_events(self, event):
action = self.input_handler.get_input(event)
if event.type == pygame.QUIT:
return {"command": Command.QUIT.value, "action": None, "value": None}
elif event.type == pygame.KEYDOWN and action == "QUIT":
return None
return None
def check_serial(self):
if self.serial.in_waiting() > 0:
data = self.serial.readline().decode("utf-8").strip()
logger.debug(f"received serial data during check_serial: {data}")
message = json.loads(data)
if message.get("command") == Command.SCORE.value:
self.opponent_dead = True
self.opponent_score = message.get("value", 0)
return True
elif message.get("command") == Command.QUIT.value:
return {"command": Command.QUIT.value, "action": None, "value": None}
return False
def end_game(self, score):
self.my_score = score
logger.debug(f"ending game, sending score: {self.my_score}")
self.serial.write(
json.dumps(
{"command": Command.SCORE.value, "action": None, "value": self.my_score}
).encode("utf-8")
)
if self.opponent_dead:
self.score_display_time = pygame.time.get_ticks()
logger.debug("opponent already dead, starting score display")
else:
self.waiting = True
logger.debug("waiting for opponent to finish")
def update(self):
if self.waiting:
self.screen.fill(self.BLACK)
text = self.font.render("Waiting for opponent ...", True, self.WHITE)
self.screen.blit(
text,
(
self.screen.get_width() // 2 - text.get_width() // 2,
self.screen.get_height() // 2,
),
)
pygame.display.flip()
if self.serial.in_waiting() > 0:
data = self.serial.readline().decode("utf-8").strip()
logger.debug(f"received serial data while waiting: {data}")
message = json.loads(data)
if message.get("command") == Command.SCORE.value:
self.opponent_score = message.get("value", 0)
self.waiting = False
self.score_display_time = pygame.time.get_ticks()
logger.debug("received opponent's score, starting score display")
elif message.get("command") == Command.QUIT.value:
logger.info("received quit command while waiting")
return {
"command": Command.QUIT.value,
"action": None,
"value": None,
}
elif self.score_display_time:
self.screen.fill(self.BLACK)
my_text = self.font.render(f"Your Score: {self.my_score}", True, self.WHITE)
opp_text = self.font.render(
f"Your Opponent's Score: {self.opponent_score}", True, self.WHITE
)
self.screen.blit(
my_text,
(
self.screen.get_width() // 2 - my_text.get_width() // 2,
self.screen.get_height() // 2 - 20,
),
)
self.screen.blit(
opp_text,
(
self.screen.get_width() // 2 - opp_text.get_width() // 2,
self.screen.get_height() // 2 + 20,
),
)
pygame.display.flip()
if pygame.time.get_ticks() - self.score_display_time > 3000:
logger.debug("score display timeout reached, exiting game")
self.is_running = False
return None
return None
def main_loop(self):
raise NotImplementedError
|