From 12931cfb5cfd9758c9a82cf789f4495270828605 Mon Sep 17 00:00:00 2001 From: Lena Date: Wed, 28 Oct 2026 23:59:21 +0100 Subject: [PATCH] =?UTF-8?q?W=C3=BCrfel=20sichtbar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spiel/cube_game.py | 49 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/spiel/cube_game.py b/spiel/cube_game.py index c79c06e..4a56f3f 100644 --- a/spiel/cube_game.py +++ b/spiel/cube_game.py @@ -1,13 +1,29 @@ +import pygame +from typing import List, Tuple + from spiel.game import Game +from spiel.compute import rot_3D, matmul, project_ortho + class CubeGame(Game): """A simple 3D cube game demo.""" def __init__(self): super().__init__("3D Cube", 60, (640, 480)) - self.corners = [[-1, -1, -1], [ 1, -1, -1], [ 1, 1, -1], [-1, 1, -1],[-1, -1, 1], [ 1, -1, 1], [ 1, 1, 1], [-1, 1, 1]] - self.angle_x = 0 - self.angle_y = 0 + # cube corners + self.corners: List[Tuple[float, float, float]] = [ + (-1, -1, -1), + (1, -1, -1), + (1, 1, -1), + (-1, 1, -1), + (-1, -1, 1), + (1, -1, 1), + (1, 1, 1), + (-1, 1, 1), + ] + self.angle_x = 0.0 + self.angle_y = 0.0 + # edges between corners that differ in exactly one coordinate self.edges = [ (0, 1), (1, 2), @@ -22,11 +38,34 @@ class CubeGame(Game): (2, 6), (3, 7), ] + self.projected: List[Tuple[float, float]] = [] def update_game(self) -> bool: - # No logic yet, just keep running + """Rotate and project all corners each frame.""" + # compute rotation matrices + rot_x = rot_3D(self.angle_x, "x") + rot_y = rot_3D(self.angle_y, "y") + rot = matmul(rot_y, rot_x) + projected: List[Tuple[float, float]] = [] + + center_x = self.size[0] / 2 + center_y = self.size[1] / 2 + + for corner in self.corners: + vec = [[corner[0]], [corner[1]], [corner[2]]] + # apply rotation + rotated = matmul(rot, vec) + x, y, z = rotated[0][0], rotated[1][0], rotated[2][0] + # orthographic projection + proj = project_ortho((x, y, z), scale=100) + projected.append((proj[0] + center_x, proj[1] + center_y)) + + self.projected = projected return True def draw_game(self) -> None: - # Clear screen to black + """Render all edges as green lines.""" self.screen.fill((0, 0, 0)) + for i, j in self.edges: + pygame.draw.line(self.screen, (0, 255, 0), self.projected[i], self.projected[j], 1) +