Moon und Apollo Animation ergänzt
This commit is contained in:
1 parent
527df2eb49
commit
87e5b1facc
3 files changed
+169
No files matched your search
@@ -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()
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in new issue
Block a user