Files

67 lines
2.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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()