initialer commit
This commit is contained in:
1 parent
e926d75119
commit
74776ef485
23 files changed
+445
-199
No files matched your search
@@ -0,0 +1,76 @@
|
||||
"""Animation: Der Mond kreist um die Erde."""
|
||||
|
||||
import pygame
|
||||
|
||||
from compute import matmul, rot_2D, transpose
|
||||
from game import Game
|
||||
|
||||
EARTH_RADIUS = 30
|
||||
MOON_RADIUS = 10
|
||||
MOON_DISTANCE = 150
|
||||
BLACK = (0, 0, 0)
|
||||
WHITE = (255, 255, 255)
|
||||
BLUE = (0, 0, 255)
|
||||
|
||||
|
||||
class Moon(Game):
|
||||
"""Der Mond kreist um die Erde."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
title: str = "Moon",
|
||||
fps: int = 60,
|
||||
size: tuple[int, int] = (640, 400),
|
||||
) -> None:
|
||||
"""Legt Startposition, Drehwinkel und Rotationsmatrix fest."""
|
||||
super().__init__(title, fps, size)
|
||||
self.center: tuple[float, float] = (size[0] / 2, size[1] / 2)
|
||||
self.position_moon: tuple[float, float] = (MOON_DISTANCE, 0)
|
||||
self.theta: float = 0.01
|
||||
self.rot_moon: list[list[float]] = rot_2D(self.theta)
|
||||
|
||||
def update_game(self) -> bool:
|
||||
"""Dreht den Mond ein Stück weiter."""
|
||||
self.position_moon = self.rotate(self.position_moon, self.rot_moon)
|
||||
return True
|
||||
|
||||
def draw_game(self) -> None:
|
||||
"""Löscht den Bildschirm, zeichnet alle Körper und zeigt das Bild an."""
|
||||
self.screen.fill(BLACK)
|
||||
self.draw_bodies()
|
||||
pygame.display.flip()
|
||||
|
||||
def draw_bodies(self) -> None:
|
||||
"""Zeichnet Erde und Mond."""
|
||||
pygame.draw.circle(self.screen, BLUE, self.to_screen((0, 0)), EARTH_RADIUS)
|
||||
pygame.draw.circle(
|
||||
self.screen, WHITE, self.to_screen(self.position_moon), MOON_RADIUS
|
||||
)
|
||||
|
||||
def rotate(
|
||||
self, position: tuple[float, float], rotation: list[list[float]]
|
||||
) -> tuple[float, float]:
|
||||
"""
|
||||
Dreht einen Punkt mit einer Rotationsmatrix um den Ursprung.
|
||||
|
||||
:param position: Punkt (x, y) relativ zum Drehzentrum
|
||||
:param rotation: 2x2-Rotationsmatrix
|
||||
:return: Gedrehter Punkt (x', y')
|
||||
"""
|
||||
point = transpose([list(position)])
|
||||
result = matmul(rotation, point)
|
||||
return (result[0][0], result[1][0])
|
||||
|
||||
def to_screen(self, position: tuple[float, float]) -> tuple[float, float]:
|
||||
"""
|
||||
Rechnet eine Position relativ zur Erde in Fensterkoordinaten um.
|
||||
|
||||
:param position: Punkt (x, y) relativ zur Erde
|
||||
:return: Punkt in Fensterkoordinaten
|
||||
"""
|
||||
return (self.center[0] + position[0], self.center[1] + position[1])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
moon = Moon()
|
||||
moon.run()
|
||||
Reference in new issue
Block a user