compute und cube game modified

This commit is contained in:
englingvi103466 committed 2026-10-22 13:41:42 +02:00
1 parent ebd75fe26e
commit 7912045943
2 files changed
+26 -24

No files matched your search

+2
View File
@@ -2,6 +2,8 @@
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/wuerfel" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/wuerfel/spiel" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/.venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.14 (matrixmania)" jdkType="Python SDK" />
+24 -24
View File
@@ -1,11 +1,10 @@
import math
import pygame
from spiel.game import Game
from spiel.compute import rot_3D, project_perspective
from spiel.compute import rot_3D, project_ortho
class CubeGame(Game):
"""Ein interaktives 3D-Würfel-Spiel basierend auf der MatrixMania-Basisklasse."""
"""Ein interaktiver 3D-Würfel-Spiel basierend auf der MatrixMania-Aufgabenstellung."""
def __init__(self) -> None:
super().__init__(
@@ -14,16 +13,16 @@ class CubeGame(Game):
size=(800, 600)
)
# 8 Eckpunkte eines Würfels im 3D-Raum (x, y, z)
# Die 8 Eckpunkte des Würfels laut Aufgabenstellung (Mittelpunkt im Ursprung)
self.vertices = [
[-50, -50, -50],
[50, -50, -50],
[50, 50, -50],
[-50, 50, -50],
[-50, -50, 50],
[50, -50, 50],
[50, 50, 50],
[-50, 50, 50]
[-1, -1, -1],
[1, -1, -1],
[1, 1, -1],
[-1, 1, -1],
[-1, -1, 1],
[1, -1, 1],
[1, 1, 1],
[-1, 1, 1]
]
# Die 12 Kanten des Würfels (verbinden jeweils zwei Indizes)
@@ -33,6 +32,7 @@ class CubeGame(Game):
(0, 4), (1, 5), (2, 6), (3, 7) # Verbindungskanten
]
# Startwinkel für x- und y-Achse sind 0
self.angle_x = 0.0
self.angle_y = 0.0
@@ -51,7 +51,7 @@ class CubeGame(Game):
return True
def draw_game(self) -> None:
"""Zeichnet den 3D-Würfel auf den Bildschirm."""
"""Berechnet die Rotationen, projiziert orthogonal und zeichnet den Würfel."""
if self.screen is None:
return
@@ -62,9 +62,10 @@ class CubeGame(Game):
center_y = self.size[1] // 2
projected_points = []
scale_factor = 100.0 # Skalierungsfaktor, damit der Würfel groß genug ist
for vertex in self.vertices:
# 1. Rotation um die X-Achse
# 1. Rotationsmatrix für x-Achse generieren und anwenden
rx_mat = rot_3D(self.angle_x, 'x')
v_rx = [
rx_mat[0][0] * vertex[0] + rx_mat[0][1] * vertex[1] + rx_mat[0][2] * vertex[2],
@@ -72,7 +73,7 @@ class CubeGame(Game):
rx_mat[2][0] * vertex[0] + rx_mat[2][1] * vertex[1] + rx_mat[2][2] * vertex[2]
]
# 2. Rotation um die Y-Achse
# 2. Rotationsmatrix für y-Achse generieren und anwenden
ry_mat = rot_3D(self.angle_y, 'y')
v_rot = [
ry_mat[0][0] * v_rx[0] + ry_mat[0][1] * v_rx[1] + ry_mat[0][2] * v_rx[2],
@@ -80,15 +81,14 @@ class CubeGame(Game):
ry_mat[2][0] * v_rx[0] + ry_mat[2][1] * v_rx[1] + ry_mat[2][2] * v_rx[2]
]
# 3. Perspektivische Projektion (z + 300, damit der Würfel vor der Kamera liegt)
proj_x, proj_y = project_perspective(
(v_rot[0], v_rot[1], v_rot[2] + 300),
distance=300.0,
scale=1.5
)
# 3. Orthogonale Projektion auf die Bildschirmebene
proj_x, proj_y = project_ortho(v_rot)
# In Bildschirmkoordinaten umrechnen (Zentrum als Ursprung)
projected_points.append((center_x + proj_x, center_y + proj_y))
# 4. Skalieren und in Bildschirmkoordinaten zentrieren
screen_x = center_x + proj_x * scale_factor
screen_y = center_y + proj_y * scale_factor
projected_points.append((screen_x, screen_y))
# Kanten des Würfels zeichnen (Neon-Türkis)
for edge in self.edges:
@@ -100,5 +100,5 @@ class CubeGame(Game):
for p in projected_points:
pygame.draw.circle(self.screen, (255, 255, 255), (int(p[0]), int(p[1])), 4)
# Aufruf der Basisklasse, um das Display zu aktualisieren (flip)
# Aufruf der Basisklasse für das Display-Update (flip)
super().draw_game()