From 8e935fbbca5442aeb9113a5f8c448eeadff0d98b Mon Sep 17 00:00:00 2001 From: hoffmeisterda104361 Date: Mon, 9 Nov 2026 10:36:59 +0100 Subject: [PATCH] made rotation controls --- game/compute.py | 2 +- game/cube_game.py | 28 ++++++++++++++++++---------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/game/compute.py b/game/compute.py index 51c9f04..90f671a 100644 --- a/game/compute.py +++ b/game/compute.py @@ -170,7 +170,7 @@ def rot_3d(matrix: Matrix, angle: float, axis: str) -> Matrix: for vec in matrix: if any(not isinstance(v, (int, float)) for v in vec): raise ValueError("All coordinates must be numeric") - rotated = [sum(v * r for v, r in zip(vec, col)) for col in zip(*rot)] + rotated = [sum(v * r for v, r in zip(vec, row)) for row in rot] result.append([float(x) for x in rotated]) return result diff --git a/game/cube_game.py b/game/cube_game.py index 91928d9..633e9e9 100644 --- a/game/cube_game.py +++ b/game/cube_game.py @@ -38,19 +38,20 @@ class CubeGame(Game): (0, 4), (1, 5), (2, 6), (3, 7), ] self.rotated_corners: Matrix | None = None + self.hold_keys: set[int] = set() def update_game(self) -> bool: - """Rotate cube each frame and prepare projected coordinates. - - Returns ``True`` to continue the main loop. - """ - self.rotation["y"] += 1.0 - if self.rotation["y"] >= 360.0: - self.rotation["y"] -= 360.0 - + for key in self.hold_keys: + if key == pygame.K_UP: + self.rotation["x"] += 1 + elif key == pygame.K_DOWN: + self.rotation["x"] -= 1 + elif key == pygame.K_LEFT: + self.rotation["y"] -= 1 + elif key == pygame.K_RIGHT: + self.rotation["y"] += 1 + # Apply rotations to vertices each frame from game.compute import rot_3d - - # Clone original vertices and apply rotations points: Matrix = [c[:] for c in self.corners] for axis in ("x", "y", "z"): points = rot_3d(points, self.rotation[axis], axis) @@ -81,3 +82,10 @@ class CubeGame(Game): pygame.draw.line(self.screen, (255, 255, 255), a, b, 2) pygame.display.flip() + def handle_event(self, event: pygame.event.Event) -> bool: + if event.type == pygame.KEYDOWN: + self.hold_keys.add(event.key) + elif event.type == pygame.KEYUP: + self.hold_keys.discard(event.key) + return True +