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:
|
for vec in matrix:
|
||||||
if any(not isinstance(v, (int, float)) for v in vec):
|
if any(not isinstance(v, (int, float)) for v in vec):
|
||||||
raise ValueError("All coordinates must be numeric")
|
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])
|
result.append([float(x) for x in rotated])
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|||||||
+18
-10
@@ -38,19 +38,20 @@ class CubeGame(Game):
|
|||||||
(0, 4), (1, 5), (2, 6), (3, 7),
|
(0, 4), (1, 5), (2, 6), (3, 7),
|
||||||
]
|
]
|
||||||
self.rotated_corners: Matrix | None = None
|
self.rotated_corners: Matrix | None = None
|
||||||
|
self.hold_keys: set[int] = set()
|
||||||
|
|
||||||
def update_game(self) -> bool:
|
def update_game(self) -> bool:
|
||||||
"""Rotate cube each frame and prepare projected coordinates.
|
for key in self.hold_keys:
|
||||||
|
if key == pygame.K_UP:
|
||||||
Returns ``True`` to continue the main loop.
|
self.rotation["x"] += 1
|
||||||
"""
|
elif key == pygame.K_DOWN:
|
||||||
self.rotation["y"] += 1.0
|
self.rotation["x"] -= 1
|
||||||
if self.rotation["y"] >= 360.0:
|
elif key == pygame.K_LEFT:
|
||||||
self.rotation["y"] -= 360.0
|
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
|
from game.compute import rot_3d
|
||||||
|
|
||||||
# Clone original vertices and apply rotations
|
|
||||||
points: Matrix = [c[:] for c in self.corners]
|
points: Matrix = [c[:] for c in self.corners]
|
||||||
for axis in ("x", "y", "z"):
|
for axis in ("x", "y", "z"):
|
||||||
points = rot_3d(points, self.rotation[axis], axis)
|
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.draw.line(self.screen, (255, 255, 255), a, b, 2)
|
||||||
pygame.display.flip()
|
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