70 lines
1.8 KiB
Python
70 lines
1.8 KiB
Python
import pygame
|
|
|
|
from compute import matmul, rot_2D
|
|
from game import Game
|
|
|
|
EARTH_RADIUS = 30
|
|
MOON_RADIUS = 10
|
|
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.position_moon: tuple[float, float] = (
|
|
self.size[0] // 2 + 100,
|
|
self.size[1] // 2,
|
|
)
|
|
self.theta = 0.05
|
|
self.rot_moon = rot_2D(self.theta)
|
|
|
|
def update_game(self) -> bool:
|
|
"""Dreht den Mond ein Stück weiter."""
|
|
self.move_moon()
|
|
return True
|
|
|
|
def move_moon(self) -> None:
|
|
"""Update moon position, rotating around Earth center."""
|
|
earth_center = (self.size[0] // 2, self.size[1] // 2)
|
|
# vector from earth to moon
|
|
dx = self.position_moon[0] - earth_center[0]
|
|
dy = self.position_moon[1] - earth_center[1]
|
|
point = [[dx], [dy]]
|
|
result = matmul(self.rot_moon, point)
|
|
# new absolute position
|
|
self.position_moon = (
|
|
earth_center[0] + result[0][0],
|
|
earth_center[1] + result[1][0],
|
|
)
|
|
|
|
def draw_game(self) -> None:
|
|
"""Zeichnet Erde und Mond."""
|
|
self.screen.fill(BLACK)
|
|
earth_pos = (self.size[0] // 2, self.size[1] // 2)
|
|
pygame.draw.circle(self.screen, BLUE, earth_pos, EARTH_RADIUS)
|
|
pygame.draw.circle(
|
|
self.screen,
|
|
WHITE,
|
|
(int(self.position_moon[0]), int(self.position_moon[1])),
|
|
MOON_RADIUS,
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
moon = Moon()
|
|
moon.run()
|
|
|
|
|
|
# source .venv/bin/activate
|
|
# python moon.py
|