Files
2026-10-22 16:45:37 +02:00

86 lines
2.7 KiB
Python

import pygame
# Diese Funktionen führen die Matrixrechnung für die Drehung aus.
from compute import matmul, rot_2D, transpose
# Apollo erbt von Moon. Dadurch bewegt sich auch der Mond um die Erde.
from moon import Moon
RED = (255, 0, 0)
APOLLO_RADIUS = 5
APOLLO_ORBIT_RADIUS = 30.0
class Apollo(Moon):
"""Die rote Raumkapsel Apollo kreist um den Mond."""
def __init__(
self,
title: str = "Apollo",
fps: int = 60,
size: tuple[int, int] = (640, 400),
) -> None:
"""Bereitet die Mond- und Apollo-Animation vor."""
# Erst werden Erde und Mond eingerichtet.
super().__init__(title, fps, size)
# Apollo startet 30 Pixel unterhalb des Mondes.
# Das ist die Position relativ zum Mond.
self.position_apollo = (0.0, APOLLO_ORBIT_RADIUS)
# Apollo dreht sich doppelt so schnell wie der Mond,
# aber in die entgegengesetzte Richtung.
self.theta_apollo = -2 * self.theta
# Aus dem Winkel wird eine Drehmatrix erstellt.
self.rot_apollo = rot_2D(self.theta_apollo)
def update_game(self) -> bool:
"""Bewegt den Mond und Apollo um jeweils einen Schritt."""
# Die Moon-Methode bewegt zuerst den Mond um die Erde.
super().update_game()
# Eine Position als Spalte für die Matrixrechnung vorbereiten.
# Aus (x, y) wird [[x], [y]].
point = transpose([list(self.position_apollo)])
# Die Drehmatrix dreht Apollo ein kleines Stück weiter.
result = matmul(self.rot_apollo, point)
# Das Matrix-Ergebnis wieder als (x, y) speichern.
self.position_apollo = (result[0][0], result[1][0])
# True sagt dem Spiel, dass die Animation weiterlaufen soll.
return True
def draw_content(self) -> None:
"""Zeichnet Erde, Mond und Apollo auf den Bildschirm."""
# Die Moon-Methode zeichnet zuerst den Hintergrund, die Erde und den Mond.
super().draw_content()
# Die Bildschirmposition des Mondes:
# Fenstermitte plus Mondposition relativ zur Erde.
moon_x = self.center[0] + self.position_moon[0]
moon_y = self.center[1] + self.position_moon[1]
# Apollo-Position = Mondposition plus Apollo-Abstand zum Mond.
apollo_x = int(moon_x + self.position_apollo[0])
apollo_y = int(moon_y + self.position_apollo[1])
# Den Bildschirm holen und Apollo als roten Kreis zeichnen.
screen = pygame.display.get_surface()
if screen is not None:
pygame.draw.circle(
screen,
RED,
(apollo_x, apollo_y),
APOLLO_RADIUS,
)
if __name__ == "__main__":
Apollo().run()