Apollo + .env + config

This commit is contained in:
Sladoje 2025-11-03 21:50:09 +01:00
parent 7d6bb28e08
commit 31a6950ef8
5 changed files with 91 additions and 12 deletions

12
.env Normal file
View File

@ -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

View File

@ -1 +1,2 @@
pygame
python-dotenv

41
src/apollo.py Normal file
View File

@ -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()

29
src/config.py Normal file
View File

@ -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)

View File

@ -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