made rotation controls
This commit is contained in:
1 parent
cf566b38d4
commit
8e935fbbca
2 files changed
+19
-11
No files matched your search
+1
-1
@@ -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
|
||||
|
||||
|
||||
+18
-10
@@ -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
|
||||
|
||||
Reference in new issue
Block a user