From 87e5b1facc41776b4c157cb6c227f94818063022 Mon Sep 17 00:00:00 2001 From: Lena Date: Wed, 28 Oct 2026 14:11:05 +0100 Subject: [PATCH] =?UTF-8?q?Moon=20und=20Apollo=20Animation=20erg=C3=A4nzt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apollo.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ game.py | 37 +++++++++++++++++++++++++++++++ moon.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 apollo.py create mode 100644 game.py create mode 100644 moon.py diff --git a/apollo.py b/apollo.py new file mode 100644 index 0000000..a6bf96f --- /dev/null +++ b/apollo.py @@ -0,0 +1,66 @@ +import pygame +from moon import Moon +from compute import matmul, rot_2D + +EARTH_RADIUS = 30 +MOON_RADIUS = 10 +APOLLO_RADIUS = 5 +BLACK = (0, 0, 0) +WHITE = (255, 255, 255) +BLUE = (0, 0, 255) +RED = (255, 0, 0) + + +class Apollo(Moon): + """The Apollo capsule orbits the moon. + + It inherits the Moon class and extends it with + – a faster, opposite‑direction orbit around the moon + – a red circular representation. + """ + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + # Initial position relative to the moon – 100 pixels along the x‑axis + self.position_apollo_rel: tuple[float, float] = (100.0, 0.0) + # Apollo's angular speed: double that of the moon, opposite sign + self.theta_apollo = -2.0 * self.theta + self.rot_apollo = rot_2D(self.theta_apollo) + self.position_apollo_abs: tuple[float, float] = ( + self.position_moon[0] + self.position_apollo_rel[0], + self.position_moon[1] + self.position_apollo_rel[1], + ) + + def update_game(self) -> bool: + """Update moon and then Apollo's orbit.""" + # First let the moon move according to its own logic + super().update_game() + + # Rotate Apollo around the moon (relative position) + point = [[self.position_apollo_rel[0]], [self.position_apollo_rel[1]]] + result = matmul(self.rot_apollo, point) + self.position_apollo_rel = (result[0][0], result[1][0]) + + # Absolute position is moon position + relative offset + self.position_apollo_abs = ( + self.position_moon[0] + self.position_apollo_rel[0], + self.position_moon[1] + self.position_apollo_rel[1], + ) + return True + + def draw_game(self) -> None: + """Draw Earth, moon and finally the Apollo capsule.""" + super().draw_game() + # Draw Apollo as a red circle + pygame.draw.circle( + self.screen, + RED, + (int(self.position_apollo_abs[0]), int(self.position_apollo_abs[1])), + APOLLO_RADIUS, + ) + + +if __name__ == "__main__": + # Launch the Apollo animation + apo = Apollo() + apo.run() diff --git a/game.py b/game.py new file mode 100644 index 0000000..c3987a2 --- /dev/null +++ b/game.py @@ -0,0 +1,37 @@ + +import pygame + + +class Game: + """Basic Pygame game loop. + + Subclasses **must** implement: + * ``update_game`` – update state, return ``False`` to quit. + * ``draw_game`` – render to the screen. + """ + + def __init__(self, title: str, fps: int, size: tuple[int, int]): + pygame.init() + self.screen = pygame.display.set_mode(size) + pygame.display.set_caption(title) + self.clock = pygame.time.Clock() + self.fps = fps + self.size = size + + def run(self) -> None: + running = True + while running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + running = self.update_game() + self.draw_game() + pygame.display.flip() + self.clock.tick(self.fps) + pygame.quit() + + def update_game(self) -> bool: + raise NotImplementedError + + def draw_game(self) -> None: + raise NotImplementedError diff --git a/moon.py b/moon.py new file mode 100644 index 0000000..156e1da --- /dev/null +++ b/moon.py @@ -0,0 +1,66 @@ + +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 \ No newline at end of file