diff options
Diffstat (limited to 'src/splitscreen_duo/games/game_base.py')
| -rw-r--r-- | src/splitscreen_duo/games/game_base.py | 55 |
1 files changed, 39 insertions, 16 deletions
diff --git a/src/splitscreen_duo/games/game_base.py b/src/splitscreen_duo/games/game_base.py index 547b27e..8ef02ec 100644 --- a/src/splitscreen_duo/games/game_base.py +++ b/src/splitscreen_duo/games/game_base.py @@ -9,10 +9,11 @@ logger = logging.getLogger(__name__) class GameBase: - def __init__(self, screen, serial, instance): + def __init__(self, screen, serial, instance, is_joint_mode=False): self.screen = screen self.serial = serial self.instance = instance + self.is_joint_mode = is_joint_mode self.font = pygame.font.Font(None, 36) self.is_running = True self.opponent_dead = False @@ -37,7 +38,6 @@ class GameBase: 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) @@ -53,26 +53,50 @@ class GameBase: return False def end_game(self, score): - self.my_score = score + if self.is_joint_mode: + self.is_running = False + + if self.instance == "primary": + self.serial.write( + json.dumps({"command": Command.GAME_ENDED.value}).encode("utf-8") + + b"\n" + ) + else: + 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") + + b"\n" + ) - 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() - 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("opponent already dead, starting score display") - else: - self.waiting = True + logger.debug("waiting for opponent to finish") - logger.debug("waiting for opponent to finish") + def send_stats(self, value): + if self.is_joint_mode and self.instance == "primary": + self.serial.write( + json.dumps({"command": Command.STATS.value, "value": value}).encode( + "utf-8" + ) + + b"\n" + ) def update(self): + if self.is_joint_mode: + return None + if self.waiting: self.screen.fill(self.BLACK) @@ -108,7 +132,6 @@ class GameBase: "action": None, "value": None, } - elif self.score_display_time: self.screen.fill(self.BLACK) |