aboutsummaryrefslogtreecommitdiff
path: root/src/splitscreen_duo/games/game_base.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/splitscreen_duo/games/game_base.py')
-rw-r--r--src/splitscreen_duo/games/game_base.py120
1 files changed, 83 insertions, 37 deletions
diff --git a/src/splitscreen_duo/games/game_base.py b/src/splitscreen_duo/games/game_base.py
index a539ba0..20387c2 100644
--- a/src/splitscreen_duo/games/game_base.py
+++ b/src/splitscreen_duo/games/game_base.py
@@ -7,6 +7,8 @@ import os
logger = logging.getLogger(__name__)
+ROTATION_ENABLED = os.getenv("DEVELOPMENT", "") in ["", "0"]
+
class GameBase:
def __init__(self, screen, serial, instance, is_joint_mode=False):
@@ -23,20 +25,29 @@ class GameBase:
self.score_display_time = 0
self.BLACK = (0, 0, 0)
self.WHITE = (255, 255, 255)
- self.input_handler = Input(debug=os.getenv("DEVELOPMENT", "").lower() != "")
self.screen_width = screen.get_width()
self.screen_height = screen.get_height()
self.is_vertical = is_joint_mode
+ self.rotation_angle = 0
+ self.rotated_surface = None
+
+ if ROTATION_ENABLED and not is_joint_mode:
+ if instance == "primary":
+ self.rotation_angle = 90 # Right is down
+ elif instance == "secondary":
+ self.rotation_angle = -90 # Left is down
+
+ self.input_handler = Input(debug=os.getenv("DEVELOPMENT", "").lower() != "")
def handle_common_events(self, event):
- action = self.input_handler.get_input(event)
+ action = self.input_handler.get_input(event, self.rotation_angle, self.is_joint_mode)
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
+ return action
def check_serial(self):
if self.serial.in_waiting() > 0:
@@ -103,10 +114,56 @@ class GameBase:
if (current_width != self.screen_width or current_height != self.screen_height):
self.screen_width = current_width
self.screen_height = current_height
- self.is_vertical = current_height > current_width * 1.5
+ self.is_vertical = self.is_joint_mode or current_height > current_width * 1.5
+
+ if ROTATION_ENABLED and not self.is_joint_mode:
+ if self.instance == "primary":
+ self.rotation_angle = 90
+ elif self.instance == "secondary":
+ self.rotation_angle = -90
+ else:
+ self.rotation_angle = 0
self.reset()
- logger.debug(f"screen dimensions updated: {self.screen_width}x{self.screen_height}, vertical: {self.is_vertical}")
+ logger.debug(f"screen dimensions updated: {self.screen_width}x{self.screen_height}, vertical: {self.is_vertical}, rotation: {self.rotation_angle}°")
+
+ def draw_waiting_screen(self, surface):
+ surface.fill(self.BLACK)
+
+ text = self.font.render("Waiting for opponent ...", True, self.WHITE)
+
+ surface.blit(
+ text,
+ (
+ self.screen_width // 2 - text.get_width() // 2,
+ self.screen_height // 2,
+ ),
+ )
+
+ def draw_score_display(self, surface):
+ surface.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
+ )
+
+ spacing = 30 if self.is_vertical else 20
+
+ surface.blit(
+ my_text,
+ (
+ self.screen_width // 2 - my_text.get_width() // 2,
+ self.screen_height // 2 - spacing,
+ ),
+ )
+ surface.blit(
+ opp_text,
+ (
+ self.screen_width // 2 - opp_text.get_width() // 2,
+ self.screen_height // 2 + spacing,
+ ),
+ )
def update(self):
self.update_screen_dimensions()
@@ -115,17 +172,7 @@ class GameBase:
return None
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,
- ),
- )
+ self.render_rotated(self.draw_waiting_screen)
pygame.display.flip()
if self.serial.in_waiting() > 0:
@@ -150,27 +197,7 @@ class GameBase:
"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,
- ),
- )
+ self.render_rotated(self.draw_score_display)
pygame.display.flip()
if pygame.time.get_ticks() - self.score_display_time > 3000:
@@ -182,5 +209,24 @@ class GameBase:
return None
+ def render_rotated(self, draw_function):
+ if ROTATION_ENABLED and not self.is_joint_mode and self.rotation_angle != 0:
+ render_surface = pygame.Surface((self.screen_width, self.screen_height))
+
+ render_surface.fill(self.BLACK)
+
+ draw_function(render_surface)
+
+ rotated = pygame.transform.rotate(render_surface, self.rotation_angle)
+
+ self.screen.fill(self.BLACK)
+
+ pos_x = (self.screen_width - rotated.get_width()) // 2
+ pos_y = (self.screen_height - rotated.get_height()) // 2
+
+ self.screen.blit(rotated, (pos_x, pos_y))
+ else:
+ draw_function(self.screen)
+
def main_loop(self):
raise NotImplementedError