Initial: compute + game + moon
This commit is contained in:
commit
7d6bb28e08
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
__pycache__/
|
||||
*.pyc
|
||||
.venv/
|
||||
venv/
|
||||
.idea/
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@ -0,0 +1 @@
|
||||
pygame
|
||||
0
src/__init__.py
Normal file
0
src/__init__.py
Normal file
59
src/compute.py
Normal file
59
src/compute.py
Normal file
@ -0,0 +1,59 @@
|
||||
from typing import List, Sequence
|
||||
import math
|
||||
|
||||
Matrix = List[List[float]]
|
||||
|
||||
def matmul(matrix_a: Matrix, matrix_b: Matrix) -> Matrix:
|
||||
rows_a = len(matrix_a)
|
||||
cols_a = len(matrix_a[0])
|
||||
|
||||
rows_b = len(matrix_b)
|
||||
cols_b = len(matrix_b[0])
|
||||
|
||||
if cols_a != rows_b:
|
||||
raise ValueError("the width of the first matrix ({cols_a}) must be equal to the width of the second matrix ({rows_b})")
|
||||
|
||||
matrix_c = [[0.0] * cols_b for _ in range(rows_a)]
|
||||
for i in range(rows_a):
|
||||
for j in range(cols_b):
|
||||
for k in range(cols_a):
|
||||
matrix_c[i][j] += matrix_a[i][k] * matrix_b[k][j]
|
||||
return matrix_c
|
||||
|
||||
def transpose(matrix: Matrix) -> Matrix:
|
||||
rows = len(matrix)
|
||||
cols = len(matrix[0])
|
||||
return [[matrix[i][j] for i in range(rows)] for j in range(cols)]
|
||||
|
||||
def rot_2D(theta: float) -> Matrix:
|
||||
import math
|
||||
c = math.cos(theta)
|
||||
s = math.sin(theta)
|
||||
return [[c, -s], [s, c]]
|
||||
|
||||
if __name__ == "__main__":
|
||||
matrix_a = [ [ 3, 4, -1, 4 ],
|
||||
[ -2, 2, 5, 1 ]
|
||||
]
|
||||
matrix_b = [ [ 1, 3, -2 ],
|
||||
[ 2, 5, 1 ],
|
||||
[ -1, 4, -4 ],
|
||||
[ 2, 3, 6 ]
|
||||
]
|
||||
matrix_c = matmul(matrix_a, matrix_b)
|
||||
print("Ergebnis C = A * B:")
|
||||
for row in matrix_c:
|
||||
print(row)
|
||||
|
||||
m = [
|
||||
[1, 2, 3],
|
||||
[4, 5, 6]
|
||||
]
|
||||
print("\nTranspose(m):")
|
||||
for row in transpose(m):
|
||||
print(row)
|
||||
|
||||
print("\nRotationsmatrix 90gard (pi/2):")
|
||||
R = rot_2D(math.pi / 2)
|
||||
for row in R:
|
||||
print(row)
|
||||
53
src/game.py
Normal file
53
src/game.py
Normal file
@ -0,0 +1,53 @@
|
||||
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()
|
||||
|
||||
44
src/moon.py
Normal file
44
src/moon.py
Normal file
@ -0,0 +1,44 @@
|
||||
import pygame
|
||||
from .game import Game
|
||||
from .compute import rot_2D
|
||||
|
||||
class Moon(Game):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Moon", fps=60, size=(960, 720))
|
||||
# Mittelpunkt
|
||||
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
|
||||
|
||||
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
|
||||
mx, my = self._to_screen(x, y)
|
||||
pygame.draw.circle(self.screen, (240, 240, 240), (mx, my), self.moon_radius)
|
||||
|
||||
pygame.display.flip()
|
||||
|
||||
if __name__ == "__main__":
|
||||
Moon().run()
|
||||
Loading…
x
Reference in New Issue
Block a user