From 731b7c2a1dbd91dadcf2281a946a546228ec9e36 Mon Sep 17 00:00:00 2001 From: AnnalenaTH Date: Mon, 19 Oct 2026 16:12:47 +0200 Subject: [PATCH] apollo Hausaufgabe --- Moon.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ apollo.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++ game.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 Moon.py create mode 100644 apollo.py create mode 100644 game.py diff --git a/Moon.py b/Moon.py new file mode 100644 index 0000000..563105b --- /dev/null +++ b/Moon.py @@ -0,0 +1,58 @@ +import pygame + +from compute import matmul, rot_2D, transpose +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.centre = (size[0] // 2, size[1] // 2) + + self.position_moon = [0.0, -150.0] + + theta = 0.01 + self.rot_moon = rot_2D(theta) + + def update_game(self) -> bool: + """Dreht den Mond ein Stück weiter.""" + self.update_moon_position() + return True + + def update_moon_position(self) -> None: + point = transpose([self.position_moon]) # [x, y] wird zur Spalte [[x], [y]] + result = matmul(self.rot_moon, point) # Ergebnis: [[x'], [y']] + self.position_moon = [result[0][0], result[1][0]] + + def draw_game(self) -> None: + self.screen.fill(BLACK) + self.draw_earth() + self.draw_moon() + pygame.display.flip() + + def draw_earth(self) -> None: + pygame.draw.circle(self.screen, BLUE, self.centre, EARTH_RADIUS) + + def draw_moon(self) -> None: + moon_x = int(self.centre[0] + self.position_moon[0]) + moon_y = int(self.centre[1] + self.position_moon[1]) + pygame.draw.circle(self.screen, WHITE, (moon_x, moon_y), MOON_RADIUS) + + +if __name__ == "__main__": + game = Moon() + game.run() \ No newline at end of file diff --git a/apollo.py b/apollo.py new file mode 100644 index 0000000..452f078 --- /dev/null +++ b/apollo.py @@ -0,0 +1,50 @@ +import pygame + +from compute import matmul, rot_2D, transpose +from Moon import Moon + +RED = (255, 0, 0) +BLACK = (0, 0, 0) +APOLLO_RADIUS = 3 + + +class Apollo(Moon): + + def __init__( + self, + title: str = "Apollo", + fps: int = 60, + size: tuple[int, int] = (640, 400), + ) -> None: + + super().__init__(title, fps, size) + + self.position_apollo = [0.0, -40.0] #Startposition von Apollo relativ zum Mond + theta_apollo = -0.02 #doppelte Geschwindigkeit, umgekehrte Drehrichtung + self.rot_apollo = rot_2D(theta_apollo) + + def update_game(self) -> bool: + super().update_game() + + point = transpose([self.position_apollo]) #dreht sich um eigene Position + result = matmul(self.rot_apollo, point) + self.position_apollo = [result[0][0], result[1][0]] #Matrix: erste Zeile: erstes Element, zweite Zeile: zweites Element + + return True + + def draw_game(self) -> None: + self.screen.fill(BLACK) + self.draw_earth() + self.draw_moon() + self.draw_apollo() + pygame.display.flip() + + def draw_apollo(self) -> None: + apollo_x = int(self.centre[0] + self.position_moon[0] + self.position_apollo[0]) #Absolute Position = Bildschirmmitte + Mond-Position + Apollo-Relativposition + apollo_y = int(self.centre[1] + self.position_moon[1] + self.position_apollo[1]) + pygame.draw.circle(self.screen, RED, (apollo_x, apollo_y), APOLLO_RADIUS) + + +if __name__ == "__main__": + game = Apollo() + game.run() \ No newline at end of file diff --git a/game.py b/game.py new file mode 100644 index 0000000..b245678 --- /dev/null +++ b/game.py @@ -0,0 +1,51 @@ +import pygame + +class Game: + + def __init__(self, title, fps=60, size=(640, 400)): + self.title = title + self.fps = fps + self.size = size + self.clock = pygame.time.Clock() + self.dt = 0 + self.screen = None + + def init_game(self): + pygame.init() + pygame.display.set_caption(self.title) + self.screen = pygame.display.set_mode(self.size) + + def game_loop(self): + while True: + # Berechnung der Zeitdifferenz seit dem letzten Frame + self.dt = self.clock.tick(self.fps) / 1000 + if self.event_handling() == False: + break + if self.update_game() == False: + break + self.draw_game() + + def exit_game(self): + pygame.quit() + + def event_handling(self): # bleibt in der Unterklasse unverändert + for event in pygame.event.get(): + if not self.handle_event(event): + return False + return True + + def handle_event(self, event): # wird in der Unterklasse überschrieben + if event.type == pygame.QUIT: + return False + return True + + def update_game(self): + return True + + def draw_game(self): + pygame.display.flip() + + def run(self): + self.init_game() + self.game_loop() + self.exit_game() \ No newline at end of file