Moon-Simulation hinzugefügt
This commit is contained in:
1 parent
658f1589ae
commit
17bf515f39
2 files changed
+166
No files matched your search
@@ -0,0 +1,68 @@
|
||||
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)
|
||||
|
||||
# Kleiner Drehwinkel im Bogenmaß und zugehörige Rotationsmatrix
|
||||
theta = 0.01
|
||||
self.rot_moon = rot_2D(theta)
|
||||
|
||||
# Startposition des Mondes als relativer Abstand zur Erde (z. B. 120 Pixel nach rechts)
|
||||
self.position_moon = (120.0, 0.0)
|
||||
|
||||
def update_game(self) -> bool:
|
||||
"""Dreht den Mond ein Stück weiter."""
|
||||
# 1. Position in Spaltenvektor [[x], [y]] umwandeln
|
||||
point = transpose([list(self.position_moon)])
|
||||
|
||||
# 2. Mit der Rotationsmatrix multiplizieren
|
||||
result = matmul(self.rot_moon, point)
|
||||
|
||||
# 3. Ergebnis zurück in ein Tupel konvertieren und speichern
|
||||
self.position_moon = (result[0][0], result[1][0])
|
||||
|
||||
return True
|
||||
|
||||
def draw_game(self) -> None:
|
||||
"""Zeichnet Erde und Mond."""
|
||||
# Bildschirm mit Schwarz füllen
|
||||
self.screen.fill(BLACK)
|
||||
|
||||
# Fenstermittelpunkt ermitteln, damit die Erde exakt in der Mitte sitzt
|
||||
center_x = self.size[0] // 2
|
||||
center_y = self.size[1] // 2
|
||||
|
||||
# Erde als blauen Kreis in der Mitte zeichnen
|
||||
pygame.draw.circle(self.screen, BLUE, (center_x, center_y), EARTH_RADIUS)
|
||||
|
||||
# Mondposition relativ zum Mittelpunkt berechnen und zeichnen
|
||||
moon_x = int(center_x + self.position_moon[0])
|
||||
moon_y = int(center_y + self.position_moon[1])
|
||||
pygame.draw.circle(self.screen, WHITE, (moon_x, moon_y), MOON_RADIUS)
|
||||
|
||||
# Anzeige aktualisieren
|
||||
pygame.display.flip()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = Moon()
|
||||
app.run()
|
||||
Reference in new issue
Block a user