Apollo, Game hinzugefügt und Moon überarbeitet
This commit is contained in:
1 parent
090453d4aa
commit
9d1ebda660
3 files changed
+157
No files matched your search
@@ -0,0 +1,64 @@
|
|||||||
|
import pygame
|
||||||
|
|
||||||
|
from compute import matmul, rot_2D
|
||||||
|
from game import Game
|
||||||
|
|
||||||
|
EARTH_RADIUS = 30
|
||||||
|
MOON_RADIUS = 10
|
||||||
|
MOON_DISTANCE = 100.0
|
||||||
|
MOON_ANGLE = 0.01
|
||||||
|
|
||||||
|
BLACK = (0, 0, 0)
|
||||||
|
BLUE = (0, 0, 255)
|
||||||
|
GRAY = (200, 200, 200)
|
||||||
|
|
||||||
|
|
||||||
|
class Moon(Game):
|
||||||
|
"""Der Mond kreist um die Erde."""
|
||||||
|
|
||||||
|
def __init__(self, title="Moon", fps=60, size=(640, 400)):
|
||||||
|
super().__init__(title, fps, size)
|
||||||
|
self.center_x = size[0] // 2
|
||||||
|
self.center_y = size[1] // 2
|
||||||
|
self.init_positions()
|
||||||
|
|
||||||
|
def init_positions(self):
|
||||||
|
self.position_moon = (MOON_DISTANCE, 0.0)
|
||||||
|
self.rot_moon = rot_2D(MOON_ANGLE)
|
||||||
|
|
||||||
|
def rotate_point(self, position, rotation):
|
||||||
|
result = matmul(rotation, [[position[0]], [position[1]]])
|
||||||
|
return (result[0][0], result[1][0])
|
||||||
|
|
||||||
|
def update_game(self):
|
||||||
|
self.update_positions()
|
||||||
|
return True
|
||||||
|
|
||||||
|
def update_positions(self):
|
||||||
|
self.position_moon = self.rotate_point(self.position_moon, self.rot_moon)
|
||||||
|
|
||||||
|
def to_screen(self, position):
|
||||||
|
return (int(self.center_x + position[0]), int(self.center_y + position[1]))
|
||||||
|
|
||||||
|
def draw_game(self):
|
||||||
|
self.screen.fill(BLACK)
|
||||||
|
self.draw_scene()
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
def draw_scene(self):
|
||||||
|
self.draw_earth()
|
||||||
|
self.draw_moon()
|
||||||
|
|
||||||
|
def draw_earth(self):
|
||||||
|
pygame.draw.circle(
|
||||||
|
self.screen, BLUE, (self.center_x, self.center_y), EARTH_RADIUS
|
||||||
|
)
|
||||||
|
|
||||||
|
def draw_moon(self):
|
||||||
|
pygame.draw.circle(
|
||||||
|
self.screen, GRAY, self.to_screen(self.position_moon), MOON_RADIUS
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
Moon().run()
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
import pygame
|
||||||
|
|
||||||
|
from Moon import MOON_ANGLE, Moon
|
||||||
|
from compute import rot_2D
|
||||||
|
|
||||||
|
APOLLO_DISTANCE = 40.0
|
||||||
|
APOLLO_RADIUS = 5
|
||||||
|
APOLLO_ANGLE = -2 * MOON_ANGLE
|
||||||
|
RED = (255, 0, 0)
|
||||||
|
|
||||||
|
|
||||||
|
class Apollo(Moon):
|
||||||
|
"""Apollo kreist gegenläufig mit doppelter Winkelgeschwindigkeit um den Mond."""
|
||||||
|
|
||||||
|
def __init__(self, title="Apollo", fps=60, size=(640, 400)):
|
||||||
|
super().__init__(title, fps, size)
|
||||||
|
|
||||||
|
def init_positions(self):
|
||||||
|
super().init_positions()
|
||||||
|
self.position_apollo = (APOLLO_DISTANCE, 0.0)
|
||||||
|
self.rot_apollo = rot_2D(APOLLO_ANGLE)
|
||||||
|
|
||||||
|
def update_positions(self):
|
||||||
|
super().update_positions()
|
||||||
|
self.position_apollo = self.rotate_point(self.position_apollo, self.rot_apollo)
|
||||||
|
|
||||||
|
def draw_scene(self):
|
||||||
|
super().draw_scene()
|
||||||
|
self.draw_apollo()
|
||||||
|
|
||||||
|
def draw_apollo(self):
|
||||||
|
position = (
|
||||||
|
self.position_moon[0] + self.position_apollo[0],
|
||||||
|
self.position_moon[1] + self.position_apollo[1],
|
||||||
|
)
|
||||||
|
pygame.draw.circle(
|
||||||
|
self.screen, RED, self.to_screen(position), APOLLO_RADIUS
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
Apollo().run()
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import pygame
|
||||||
|
|
||||||
|
class Game:
|
||||||
|
|
||||||
|
def __init__(self, title, fps=60, size=(640, 400)):
|
||||||
|
self.title = title
|
||||||
|
self.fps = fps
|
||||||
|
self.size = size
|
||||||
|
self.clock = pygame.time.Clock()
|
||||||
|
self.dt = 0
|
||||||
|
self.screen = None
|
||||||
|
|
||||||
|
def init_game(self):
|
||||||
|
pygame.init()
|
||||||
|
pygame.display.set_caption(self.title)
|
||||||
|
self.screen = pygame.display.set_mode(self.size)
|
||||||
|
|
||||||
|
def game_loop(self):
|
||||||
|
while True:
|
||||||
|
# Berechnung der Zeitdifferenz seit dem letzten Frame
|
||||||
|
self.dt = self.clock.tick(self.fps) / 1000
|
||||||
|
if self.event_handling() == False:
|
||||||
|
break
|
||||||
|
if self.update_game() == False:
|
||||||
|
break
|
||||||
|
self.draw_game()
|
||||||
|
|
||||||
|
def exit_game(self):
|
||||||
|
pygame.quit()
|
||||||
|
|
||||||
|
def event_handling(self): # bleibt in der Unterklasse unverändert
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if not self.handle_event(event):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def handle_event(self, event): # wird in der Unterklasse überschrieben
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def update_game(self):
|
||||||
|
return True
|
||||||
|
|
||||||
|
def draw_game(self):
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
self.init_game()
|
||||||
|
self.game_loop()
|
||||||
|
self.exit_game()
|
||||||
Reference in new issue
Block a user