MoonGame/moon.py
2025-10-30 20:05:43 +01:00

58 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# moon.py
import math
import pygame
from compute import rotation_matrix, apply_mat_to_vec, add
from game import Game # <- euer Framework aus Semester 1
class Moon(Game):
"""
Animation: Blauer Kreis (Erde) im Ursprung, weißer Kreis (Mond) kreist darum.
Mathe (Rotation etc.) liegt komplett in compute.py.
"""
def __init__(self, width: int = 800, height: int = 600, fps: int = 60):
super().__init__(width, height, fps, title="Moon Orbit Demo")
# „Ursprung“ = Bildschirmmitte (damit Erde bei (0,0) in unserer Welt liegt)
self.center = (width // 2, height // 2)
# Darstellung
self.earth_radius = 32
self.moon_radius = 12
self.orbit_radius = 160 # Abstand ErdeMond in Pixeln (optisch ansprechend)
# Bewegung
self.angular_speed = 0.8 # rad/s (Geschwindigkeit der Umlaufbahn)
self.angle = 0.0 # aktueller Winkel in rad
# Farben
self.bg_color = (10, 10, 25)
self.earth_color = (60, 120, 255) # blau
self.moon_color = (245, 245, 245) # weiß
def update(self, dt: float) -> None:
"""Zeitentwicklung: Winkel erhöhen (dt in Sekunden)."""
self.angle = (self.angle + self.angular_speed * dt) % (2 * math.pi)
def draw(self, screen: pygame.Surface) -> None:
"""Szene rendern."""
screen.fill(self.bg_color)
# Erde im „Ursprung“ (hier: Bildschirmmitte)
pygame.draw.circle(screen, self.earth_color, self.center, self.earth_radius)
# Startvektor des Mondes (auf x-Achse, Länge = orbit_radius)
v0 = (float(self.orbit_radius), 0.0)
# Rotierter Vektor: v = R(angle) * v0
R = rotation_matrix(self.angle)
v_rot = apply_mat_to_vec(R, v0)
# In Bildschirmkoordinaten verschieben: (0,0) Welt -> center Screen
pos = add(self.center, v_rot)
# Mond zeichnen
pygame.draw.circle(screen, self.moon_color, (int(pos[0]), int(pos[1])), self.moon_radius)
if __name__ == "__main__":
Moon().run()