diff options
Diffstat (limited to 'src/splitscreen_duo/games/breakout.py')
| -rw-r--r-- | src/splitscreen_duo/games/breakout.py | 193 |
1 files changed, 57 insertions, 136 deletions
diff --git a/src/splitscreen_duo/games/breakout.py b/src/splitscreen_duo/games/breakout.py index bb42fa8..950adf5 100644 --- a/src/splitscreen_duo/games/breakout.py +++ b/src/splitscreen_duo/games/breakout.py @@ -1,8 +1,8 @@ import pygame import random from ..command import Command +from .game_base import GameBase import logging -import json BLACK = (0, 0, 0) WHITE = (255, 255, 255) @@ -20,10 +20,11 @@ SPEED = 5 logger = logging.getLogger(__name__) -class Breakout: - def __init__(self, screen_width, screen_height): - self.screen_width = screen_width - self.screen_height = screen_height +class Breakout(GameBase): + def __init__(self, screen, serial, instance): + super().__init__(screen, serial, instance) + self.screen_width = screen.get_width() + self.screen_height = screen.get_height() self.reset() @@ -98,146 +99,66 @@ class Breakout: def check_game_over(self): if self.ball[1] >= self.screen_height: return "lose" + if not self.bricks: return "win" return None + def main_loop(self): + clock = pygame.time.Clock() -def main_loop(screen, serial, instance): - clock = pygame.time.Clock() - breakout = Breakout(screen.get_width(), screen.get_height()) - font = pygame.font.Font(None, 36) - is_running = True - opponent_dead = False - my_score = 0 - opponent_score = 0 - waiting = False - score_display_time = 0 - - while is_running: - if waiting: - screen.fill(BLACK) - - text = font.render("waiting for opponent ...", True, WHITE) - - screen.blit( - text, - ( - screen.get_width() // 2 - text.get_width() // 2, - screen.get_height() // 2, - ), - ) - pygame.display.flip() - - if serial.in_waiting() > 0: - data = serial.readline().decode("utf-8").strip() - message = json.loads(data) - - if message.get("command") == Command.SCORE.value: - opponent_score = message.get("value", 0) - waiting = False - score_display_time = pygame.time.get_ticks() - elif message.get("command") == Command.QUIT.value: - return { - "command": Command.QUIT.value, - "action": None, - "value": None, - } - elif score_display_time: - screen.fill(BLACK) - - my_text = font.render(f"Your Score: {my_score}", True, WHITE) - opp_text = font.render( - f"Your Opponent's Score: {opponent_score}", True, WHITE - ) - - screen.blit( - my_text, - ( - screen.get_width() // 2 - my_text.get_width() // 2, - screen.get_height() // 2 - 20, - ), - ) - screen.blit( - opp_text, - ( - screen.get_width() // 2 - opp_text.get_width() // 2, - screen.get_height() // 2 + 20, - ), - ) - pygame.display.flip() - - if pygame.time.get_ticks() - score_display_time > 3000: - return None - else: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - return { - "command": Command.QUIT.value, - "action": None, - "value": None, - } - elif event.type == pygame.KEYDOWN: - if event.key == pygame.K_LEFT: - breakout.move_paddle(-20) - elif event.key == pygame.K_RIGHT: - breakout.move_paddle(20) - elif event.key == pygame.K_ESCAPE: - return None - - breakout.paddle[0] = pygame.mouse.get_pos()[0] - PADDLE_WIDTH // 2 - - breakout.move_paddle(0) - - if serial.in_waiting() > 0: - data = serial.readline().decode("utf-8").strip() - message = json.loads(data) - - if message.get("command") == Command.SCORE.value: - opponent_dead = True - opponent_score = message.get("value", 0) - - breakout.move_ball() - - game_over = breakout.check_game_over() - - if game_over: - my_score = breakout.score - - serial.write( - json.dumps( - { - "command": Command.SCORE.value, - "action": None, - "value": my_score, - } - ).encode("utf-8") - ) + while self.is_running: + result = self.update() + + if result is not None: + return result + + if not (self.waiting or self.score_display_time): + for event in pygame.event.get(): + result = self.handle_common_events(event) + + if result is not None: + return result + elif event.type == pygame.KEYDOWN: + if event.key == pygame.K_LEFT: + self.move_paddle(-20) + elif event.key == pygame.K_RIGHT: + self.move_paddle(20) + + self.paddle[0] = pygame.mouse.get_pos()[0] - PADDLE_WIDTH // 2 - if opponent_dead: - score_display_time = pygame.time.get_ticks() - else: - waiting = True - continue + self.move_paddle(0) + self.check_serial() + self.move_ball() - screen.fill(BLACK) - pygame.draw.rect( - screen, - BLUE, - [breakout.paddle[0], breakout.paddle[1], PADDLE_WIDTH, PADDLE_HEIGHT], - ) - pygame.draw.circle( - screen, WHITE, [int(breakout.ball[0]), int(breakout.ball[1])], BALL_SIZE - ) + game_over = self.check_game_over() - for brick in breakout.bricks: - pygame.draw.rect(screen, RED, brick) + if game_over: + self.end_game(self.score) - score_text = font.render(f"Score: {breakout.score}", True, WHITE) + continue - screen.blit(score_text, (10, 10)) - pygame.display.flip() - clock.tick(60) + self.screen.fill(BLACK) + pygame.draw.rect( + self.screen, + BLUE, + [self.paddle[0], self.paddle[1], PADDLE_WIDTH, PADDLE_HEIGHT], + ) + pygame.draw.circle( + self.screen, + WHITE, + [int(self.ball[0]), int(self.ball[1])], + BALL_SIZE, + ) + + for brick in self.bricks: + pygame.draw.rect(self.screen, RED, brick) - return None + score_text = self.font.render(f"Score: {self.score}", True, WHITE) + + self.screen.blit(score_text, (10, 10)) + pygame.display.flip() + clock.tick(60) + + return None |