Würfel
This commit is contained in:
commit
e957a37aec
8 files changed
+476
No files matched your search
@@ -0,0 +1,105 @@
|
||||
import pygame
|
||||
from Spiel.game import Game
|
||||
from Spiel.compute import matmul, rot_3d, project_ortho
|
||||
|
||||
|
||||
class CubeGame(Game):
|
||||
def __init__(self, title="3D Cube", fps=60, size=(500, 500)):
|
||||
super().__init__(title=title, fps=fps, size=size)
|
||||
|
||||
# Die 8 Eckpunkte des Würfels mit Mittelpunkt im Ursprung
|
||||
self.vertices: list[list[float]] = [
|
||||
[-1.0, -1.0, -1.0],
|
||||
[ 1.0, -1.0, -1.0],
|
||||
[ 1.0, 1.0, -1.0],
|
||||
[-1.0, 1.0, -1.0],
|
||||
[-1.0, -1.0, 1.0],
|
||||
[ 1.0, -1.0, 1.0],
|
||||
[ 1.0, 1.0, 1.0],
|
||||
[-1.0, 1.0, 1.0],
|
||||
]
|
||||
|
||||
# Die 12 Kanten des Würfels (Paare von Punkt-Indizes)
|
||||
self.edges: list[tuple[int, int]] = [
|
||||
# Hintere Fläche (z = -1)
|
||||
(0, 1), (1, 2), (2, 3), (3, 0),
|
||||
# Vordere Fläche (z = 1)
|
||||
(4, 5), (5, 6), (6, 7), (7, 4),
|
||||
# Verbindungen zwischen beiden Flächen
|
||||
(0, 4), (1, 5), (2, 6), (3, 7)
|
||||
]
|
||||
|
||||
# Rotationswinkel um x- und y-Achse
|
||||
self.angle_x: float = 0.0
|
||||
self.angle_y: float = 0.0
|
||||
|
||||
# Skalierungsfaktor zur Darstellung auf dem Bildschirm
|
||||
self.scale: float = 90.0
|
||||
|
||||
# Liste der aktuell projizierten 2D-Bildschirmpunkte
|
||||
self.projected_points: list[tuple[float, float]] = []
|
||||
|
||||
def event_handling(self) -> bool:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
return False
|
||||
|
||||
# Tastensteuerung für gezielte manuelle Drehung (Pfeiltasten oder WASD)
|
||||
keys = pygame.key.get_pressed()
|
||||
speed = 0.04
|
||||
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
|
||||
self.angle_y -= speed
|
||||
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
|
||||
self.angle_y += speed
|
||||
if keys[pygame.K_UP] or keys[pygame.K_w]:
|
||||
self.angle_x -= speed
|
||||
if keys[pygame.K_DOWN] or keys[pygame.K_s]:
|
||||
self.angle_x += speed
|
||||
|
||||
return True
|
||||
|
||||
def update_game(self) -> bool:
|
||||
# Stetige Grundrotation für die Animation
|
||||
self.angle_x += 0.015
|
||||
self.angle_y += 0.02
|
||||
|
||||
# Rotationsmatrizen für x- und y-Achse erzeugen
|
||||
rx = rot_3D(self.angle_x, "x")
|
||||
ry = rot_3D(self.angle_y, "y")
|
||||
|
||||
# Gesamtrotationsmatrix kombinieren: R = Ry * Rx
|
||||
r_combined = matmul(ry, rx)
|
||||
|
||||
# Bildschirm-Mittelpunkt
|
||||
center_x = self.size[0] / 2
|
||||
center_y = self.size[1] / 2
|
||||
|
||||
self.projected_points = []
|
||||
for vertex in self.vertices:
|
||||
# 3D-Vektor als Spaltenmatrix (3x1) für die Matrizenmultiplikation
|
||||
v_col = [[vertex[0]], [vertex[1]], [vertex[2]]]
|
||||
rotated_col = matmul(r_combined, v_col)
|
||||
rotated_point = [rotated_col[0][0], rotated_col[1][0], rotated_col[2][0]]
|
||||
|
||||
# Orthogonal projizieren und hochskalieren
|
||||
proj_x, proj_y = project_ortho(rotated_point, scale=self.scale)
|
||||
|
||||
# In die Fenstermitte verschieben
|
||||
screen_x = center_x + proj_x
|
||||
screen_y = center_y + proj_y
|
||||
self.projected_points.append((screen_x, screen_y))
|
||||
|
||||
return True
|
||||
|
||||
def draw_game(self):
|
||||
# Hintergrund schwarz färben
|
||||
self.screen.fill((0, 0, 0))
|
||||
|
||||
# Die 12 Kanten in Grün zeichnen
|
||||
green = (0, 255, 0)
|
||||
for start_idx, end_idx in self.edges:
|
||||
p1 = self.projected_points[start_idx]
|
||||
p2 = self.projected_points[end_idx]
|
||||
pygame.draw.line(self.screen, green, p1, p2, 2)
|
||||
|
||||
pygame.display.flip()
|
||||
Reference in new issue
Block a user