In the Beginning there was
the Cube
This commit is contained in:
commit
442cb227cb
13 files changed
+578
No files matched your search
@@ -0,0 +1,103 @@
|
||||
import pygame
|
||||
from game import Game
|
||||
import compute
|
||||
|
||||
BLACK = (0, 0, 0)
|
||||
GREEN = (0, 255, 0)
|
||||
RED = (255, 0, 0)
|
||||
|
||||
class CubeGame(Game):
|
||||
"""Der Mond kreist um die Erde."""
|
||||
def __init__(self,title: str = "Cube: The Game", fps: int = 60, size: tuple[int, int] = (1280, 800),) -> None:
|
||||
"""Legt Startposition, Drehwinkel und Rotationsmatrix fest."""
|
||||
super().__init__(title, fps, size)
|
||||
|
||||
self.cube_corner_points = [(-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.cube_edges = [(0, 1), (0, 3), (0, 4),
|
||||
(6, 2), (6, 7), (6, 5),
|
||||
(1, 2), (1, 5),
|
||||
(3, 7), (3, 2),
|
||||
(4, 5), (4, 7)]
|
||||
|
||||
self.screen_middle = (self.size[0]// 2, self.size[1] // 2)
|
||||
|
||||
self.spin = .05
|
||||
self.camera_scale_ortho = 150
|
||||
self.camera_scale_project = 400
|
||||
self.orthogonal = False
|
||||
self.direction = (0, 0)
|
||||
|
||||
|
||||
def handle_event(self, event):
|
||||
if not super().handle_event(event):
|
||||
return False
|
||||
if event.type == pygame.KEYDOWN:
|
||||
print(self.direction)
|
||||
if event.key == pygame.K_UP:
|
||||
self.direction = (max(-1, self.direction[0] - .1), self.direction[1])
|
||||
if event.key == pygame.K_DOWN:
|
||||
self.direction = (min(1, self.direction[0] + .1), self.direction[1])
|
||||
if event.key == pygame.K_RIGHT:
|
||||
self.direction = (self.direction[0], max(-1, self.direction[1] - .1))
|
||||
if event.key == pygame.K_LEFT:
|
||||
self.direction = ( self.direction[0], min(1, self.direction[1] + .1))
|
||||
if event.key == pygame.K_SPACE:
|
||||
self.orthogonal = not self.orthogonal
|
||||
return True
|
||||
|
||||
def rotate(self, axis, dir=1):
|
||||
for point in range(len(self.cube_corner_points)):
|
||||
point_matrix = [[self.cube_corner_points[point][0]],
|
||||
[self.cube_corner_points[point][1]],
|
||||
[self.cube_corner_points[point][2]]]
|
||||
point_temp = compute.matmul(compute.rot_3d(self.spin*dir, axis), point_matrix)
|
||||
self.cube_corner_points[point] = (point_temp[0][0], point_temp[1][0], point_temp[2][0])
|
||||
|
||||
def update_game(self) -> bool:
|
||||
"""Dreht den Mond ein Stück weiter."""
|
||||
if self.direction[0] != 0:
|
||||
self.rotate("x", self.direction[0])
|
||||
if self.direction[1] != 0:
|
||||
self.rotate("y", self.direction[1])
|
||||
self.direction = (round(self.direction[0], 4), round(self.direction[1], 4))
|
||||
|
||||
return True
|
||||
|
||||
def draw_game(self) -> None:
|
||||
"""Zeichnet Erde und Mond."""
|
||||
self.screen.fill(BLACK)
|
||||
|
||||
for edge in self.cube_edges:
|
||||
point2d_a = self.to_middle(self.perspective(self.cube_corner_points[edge[0]]))
|
||||
point2d_b = self.to_middle(self.perspective(self.cube_corner_points[edge[1]]))
|
||||
pygame.draw.line(self.screen, RED, point2d_a, point2d_b)
|
||||
|
||||
for point in self.cube_corner_points:
|
||||
point2d = self.to_middle(self.perspective(point))
|
||||
pygame.draw.circle(self.screen, GREEN, point2d, 5)
|
||||
|
||||
|
||||
pygame.display.flip()
|
||||
|
||||
def perspective(self, point):
|
||||
if self.orthogonal:
|
||||
return self.orthogonal_perspective(point)
|
||||
return self.project_perspective(point)
|
||||
|
||||
def orthogonal_perspective(self, point):
|
||||
return compute.project_ortho(point, scale=self.camera_scale_ortho)
|
||||
|
||||
def project_perspective(self, point):
|
||||
return compute.project_perspective(point, scale=self.camera_scale_project)
|
||||
|
||||
def to_middle(self, point):
|
||||
return point[0] + self.screen_middle[0], point[1] + self.screen_middle[1]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
cube = CubeGame()
|
||||
cube.run()
|
||||
Reference in new issue
Block a user