From 31a6950ef8d9f84bf93e8cbfd02e4090a83d81b7 Mon Sep 17 00:00:00 2001 From: Sladoje Date: Mon, 3 Nov 2025 21:50:09 +0100 Subject: [PATCH] Apollo + .env + config --- .env | 12 ++++++++++++ requirements.txt | 1 + src/apollo.py | 41 +++++++++++++++++++++++++++++++++++++++++ src/config.py | 29 +++++++++++++++++++++++++++++ src/moon.py | 20 ++++++++------------ 5 files changed, 91 insertions(+), 12 deletions(-) create mode 100644 .env create mode 100644 src/apollo.py create mode 100644 src/config.py diff --git a/.env b/.env new file mode 100644 index 0000000..790c0b0 --- /dev/null +++ b/.env @@ -0,0 +1,12 @@ +WINDOW_WIDTH=960 +WINDOW_HEIGHT=720 +FPS=60 + +EARTH_RADIUS=24 +MOON_RADIUS=10 +MOON_ORBIT=180 +MOON_ANGULAR_SPEED=0.7 + +APOLLO_RADIUS=7 +APOLLO_ORBIT=40 +APOLLO_REL_SPEED=-1.4 diff --git a/requirements.txt b/requirements.txt index 0cb7ff1..eefac5d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ pygame +python-dotenv \ No newline at end of file diff --git a/src/apollo.py b/src/apollo.py new file mode 100644 index 0000000..7a29090 --- /dev/null +++ b/src/apollo.py @@ -0,0 +1,41 @@ +import pygame +from .moon import Moon +from .compute import rot_2D +from . import config + +class Apollo(Moon): + def __init__(self) -> None: + super().__init__() + self.apollo_angle = 0.0 + self.apollo_speed = config.APOLLO_REL_SPEED + self.apollo_orbit = config.APOLLO_ORBIT + self.apollo_radius = config.APOLLO_RADIUS + + def update_game(self) -> bool: + super().update_game() + self.apollo_angle += self.apollo_speed * self.dt + return True + + def draw_game(self) -> None: + assert self.screen is not None + self.screen.fill((0, 0, 0)) + + pygame.draw.circle(self.screen, (40, 120, 255), (self.cx, self.cy), self.earth_radius) + + R_moon = rot_2D(self.angle) + mx = R_moon[0][0] * self.orbit + R_moon[0][1] * 0.0 + my = R_moon[1][0] * self.orbit + R_moon[1][1] * 0.0 + smx, smy = self._to_screen(mx, my) + pygame.draw.circle(self.screen, (240, 240, 240), (smx, smy), self.moon_radius) + + R_ap = rot_2D(self.apollo_angle) + ax = R_ap[0][0] * self.apollo_orbit + R_ap[0][1] * 0.0 + ay = R_ap[1][0] * self.apollo_orbit + R_ap[1][1] * 0.0 + + apx, apy = self._to_screen(mx + ax, my + ay) + pygame.draw.circle(self.screen, (220, 50, 50), (apx, apy), self.apollo_radius) + + pygame.display.flip() + +if __name__ == "__main__": + Apollo().run() diff --git a/src/config.py b/src/config.py new file mode 100644 index 0000000..80afcb0 --- /dev/null +++ b/src/config.py @@ -0,0 +1,29 @@ +from dotenv import load_dotenv +import os + +load_dotenv() + +def _as_int(name: str, default: int) -> int: + try: + return int(os.environ.get(name, default)) + except (TypeError, ValueError): + return default + +def _as_float(name: str, default: float) -> float: + try: + return float(os.environ.get(name, default)) + except (TypeError, ValueError): + return default + +WINDOW_WIDTH = _as_int("WINDOW_WIDTH", 960) +WINDOW_HEIGHT = _as_int("WINDOW_HEIGHT", 720) +FPS = _as_int("FPS", 60) + +EARTH_RADIUS = _as_int("EARTH_RADIUS", 24) +MOON_RADIUS = _as_int("MOON_RADIUS", 10) +MOON_ORBIT = _as_int("MOON_ORBIT", 180) +MOON_ANGULAR_SPEED = _as_float("MOON_ANGULAR_SPEED", 0.7) + +APOLLO_RADIUS = _as_int("APOLLO_RADIUS", 7) +APOLLO_ORBIT = _as_int("APOLLO_ORBIT", 40) +APOLLO_REL_SPEED = _as_float("APOLLO_REL_SPEED", -1.4) diff --git a/src/moon.py b/src/moon.py index aa4f002..eb8d756 100644 --- a/src/moon.py +++ b/src/moon.py @@ -1,37 +1,33 @@ import pygame from .game import Game from .compute import rot_2D +from . import config class Moon(Game): def __init__(self) -> None: - super().__init__("Moon", fps=60, size=(960, 720)) - # Mittelpunkt + super().__init__("Moon", fps=config.FPS, size=(config.WINDOW_WIDTH, config.WINDOW_HEIGHT)) self.cx = self.size[0] // 2 self.cy = self.size[1] // 2 - # Zustand - self.angle = 0.0 # aktueller Winkel (Radiant) - self.angular_speed = 0.7 # Winkelgeschwindigkeit (rad/s) - self.orbit = 180 # Bahnradius in Pixeln - self.earth_radius = 24 - self.moon_radius = 10 + + self.angle = 0.0 + self.angular_speed = config.MOON_ANGULAR_SPEED + self.orbit = config.MOON_ORBIT + self.earth_radius = config.EARTH_RADIUS + self.moon_radius = config.MOON_RADIUS def update_game(self) -> bool: - # pro Frame Winkel erhöhen: dt = Sekunden seit letztem Frame self.angle += self.angular_speed * self.dt return True def _to_screen(self, x: float, y: float) -> tuple[int, int]: - # Mathe-y nach oben -> Bildschirm-y nach unten return int(self.cx + x), int(self.cy - y) def draw_game(self) -> None: assert self.screen is not None self.screen.fill((0, 0, 0)) - # Erde in der Mitte pygame.draw.circle(self.screen, (40, 120, 255), (self.cx, self.cy), self.earth_radius) - # Rotationsmatrix und Mondposition: R(angle) * (orbit, 0) R = rot_2D(self.angle) x = R[0][0] * self.orbit + R[0][1] * 0.0 y = R[1][0] * self.orbit + R[1][1] * 0.0