From 808c19d1ff99922c7a875942cb6b61142972424c Mon Sep 17 00:00:00 2001 From: hoffmeisterda104361 Date: Mon, 2 Nov 2026 16:14:48 +0100 Subject: [PATCH] project structure --- .idea/.gitignore | 5 + .idea/Cube.iml | 10 + .../inspectionProfiles/profiles_settings.xml | 6 + .idea/misc.xml | 7 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + game/__init__.py | 1 + game/compute.py | 177 ++++++++++++++++++ game/cube_game.py | 13 ++ game/game.py | 98 ++++++++++ main.py | 1 + pyproject.toml | 0 12 files changed, 332 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/Cube.iml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 game/__init__.py create mode 100644 game/compute.py create mode 100644 game/cube_game.py create mode 100644 game/game.py create mode 100644 main.py create mode 100644 pyproject.toml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..b58b603 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/Cube.iml b/.idea/Cube.iml new file mode 100644 index 0000000..dd6587f --- /dev/null +++ b/.idea/Cube.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..34ca4a6 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..60dc314 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/game/__init__.py b/game/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/game/__init__.py @@ -0,0 +1 @@ + diff --git a/game/compute.py b/game/compute.py new file mode 100644 index 0000000..51c9f04 --- /dev/null +++ b/game/compute.py @@ -0,0 +1,177 @@ +"""Utility module providing basic matrix operations. + +Functions +--------- +- :func:`matmul` – Multiply two matrices. +- :func:`transpose` – Transpose a matrix. +- :func:`rotate` – Rotate a matrix 90° clockwise. + +All functions work on nested Python lists and perform basic +validation before operation. +""" + +from __future__ import annotations + +Matrix = list[list[float]] + +def matmul(a: Matrix, b: Matrix) -> Matrix: + """Return the product of two matrices. + + Parameters + ---------- + a: Matrix + Left‑hand matrix (shape ``m × n``). + b: Matrix + Right‑hand matrix (shape ``n × p``). + + Returns + ------- + Matrix + Resulting matrix of shape ``m × p``. + + Raises + ------ + ValueError + If the inner dimensions of the matrices do not match, or if + either argument is empty. + """ + if not a or not a[0] or not b or not b[0]: + raise ValueError("Input matrices must be non‑empty") + + n_cols_a = len(a[0]) + n_rows_b = len(b) + if n_cols_a != n_rows_b: + raise ValueError( + f"Inner dimensions must agree for matrix multiplication: " + f"{len(a)}×{n_cols_a} and {n_rows_b}×{len(b[0])}" + ) + + m = len(a) + p = len(b[0]) + result: Matrix = [[0.0 for _ in range(p)] for _ in range(m)] + + for i in range(m): + for j in range(p): + total = 0.0 + for k in range(n_cols_a): + total += a[i][k] * b[k][j] + result[i][j] = total + return result + + +def transpose(matrix: Matrix) -> Matrix: + """Return the transpose of a matrix. + + Parameters + ---------- + matrix: Matrix + A matrix represented as a list of row lists. + + Returns + ------- + Matrix + The transposed matrix. + + Raises + ------ + ValueError + If the matrix is empty or rows are not of equal length. + """ + if not matrix or not matrix[0]: + raise ValueError("Input matrix must be non‑empty") + + row_length = len(matrix[0]) + for row in matrix: + if len(row) != row_length: + raise ValueError("All rows in the matrix must have the same length") + + return [list(col) for col in zip(*matrix)] + + +def rot_2d(matrix: Matrix) -> Matrix: + """Return the matrix rotated 90 degrees clockwise. + + This is equivalent to transposing the matrix and then reversing + each row. + + Parameters + ---------- + matrix: Matrix + A matrix represented as a list of row lists. + + Returns + ------- + Matrix + The rotated matrix. + + Raises + ------ + ValueError + If the matrix is empty or rows are not of equal length. + """ + transposed = transpose(matrix) + return [list(reversed(row)) for row in transposed] + +def rot_3d(matrix: Matrix, angle: float, axis: str) -> Matrix: + """Return a new matrix rotated around the specified axis. + + Parameters + ---------- + matrix: Matrix + Matrix of 3‑element vectors to be rotated. + angle: float + Rotation angle in degrees. + axis: str + Axis to rotate around. Must be ``'x'``, ``'y'`` or ``'z'``. + + Returns + ------- + Matrix + Rotated matrix of 3‑element vectors. + + Raises + ------ + ValueError + If the input matrix is empty, rows are not of length 3, or the axis is invalid. + """ + import math + + if not isinstance(angle, (int, float)): + raise ValueError("angle must be a numeric value") + if not isinstance(axis, str): + raise ValueError("axis must be a string") + + rad = math.radians(angle) + c = math.cos(rad) + s = math.sin(rad) + + axis = axis.lower() + if axis == "x": + rot = [[1, 0, 0], + [0, c, -s], + [0, s, c]] + elif axis == "y": + rot = [[c, 0, s], + [0, 1, 0], + [-s, 0, c]] + elif axis == "z": + rot = [[c, -s, 0], + [s, c, 0], + [0, 0, 1]] + else: + raise ValueError("axis must be one of 'x', 'y', or 'z'") + + if not matrix or not all(isinstance(row, list) for row in matrix): + raise ValueError("Input must be a list of rows") + if not all(len(row) == 3 for row in matrix): + raise ValueError("Each row must contain exactly 3 elements") + + result: Matrix = [] + for vec in matrix: + if any(not isinstance(v, (int, float)) for v in vec): + raise ValueError("All coordinates must be numeric") + rotated = [sum(v * r for v, r in zip(vec, col)) for col in zip(*rot)] + result.append([float(x) for x in rotated]) + return result + +"""End of module""" diff --git a/game/cube_game.py b/game/cube_game.py new file mode 100644 index 0000000..3126611 --- /dev/null +++ b/game/cube_game.py @@ -0,0 +1,13 @@ +from game.game import Game + +class CubeGame(Game): + def __init__(self): + super().__init__("Cube Game") + self.centre = [0,0,0] + self.corners = [[-1.0,-1.0,-1.0], [1.0,-1.0,-1.0], [1.0,1.0,-1.0], [-1.0,1.0,-1.0], + [-1.0,-1.0,1.0], [1.0,-1.0,1.0], [1.0,1.0,1.0], [-1.0,1.0,1.0]] + def update_game(self): + pass + + def draw_game(self): + pass diff --git a/game/game.py b/game/game.py new file mode 100644 index 0000000..06f6d1f --- /dev/null +++ b/game/game.py @@ -0,0 +1,98 @@ +"""Basisklasse ``Game`` für einfache pygame-Anwendungen.""" + +import pygame + + +class Game: + """Basisklasse für pygame-Spiele. + + Unterklassen passen das Verhalten durch Überschreiben von + :meth:`handle_event`, :meth:`update_game` und :meth:`draw_game` an. + """ + + def __init__( + self, + title: str, + fps: int = 60, + size: tuple[int, int] = (640, 400), + ) -> None: + """Legt Fenstertitel, Bildrate und Fenstergröße fest. + + Args: + title: Titel des Fensters. + fps: Maximale Bilder pro Sekunde. + size: Fenstergröße als (Breite, Höhe) in Pixeln. + """ + self.title: str = title + self.fps: int = fps + self.size: tuple[int, int] = size + self.clock: pygame.time.Clock = pygame.time.Clock() + self.dt: float = 0 + self.screen: pygame.Surface | None = None + + def init_game(self) -> None: + """Initialisiert pygame und erzeugt das Fenster.""" + pygame.init() + pygame.display.set_caption(self.title) + self.screen = pygame.display.set_mode(self.size) + + def game_loop(self) -> None: + """Führt die Hauptschleife aus, bis Events oder Update ``False`` liefern.""" + 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) -> None: + """Beendet pygame.""" + pygame.quit() + + def event_handling(self) -> bool: + """Verteilt alle anstehenden Events an :meth:`handle_event`. + + Bleibt in der Unterklasse unverändert. + + Returns: + ``False``, sobald ein Event das Spiel beenden soll, sonst ``True``. + """ + for event in pygame.event.get(): + if not self.handle_event(event): + return False + return True + + def handle_event(self, event: pygame.event.Event) -> bool: + """Verarbeitet ein einzelnes Event. + + Wird in der Unterklasse überschrieben. + + Args: + event: Das zu verarbeitende pygame-Event. + + Returns: + ``False`` bei ``pygame.QUIT``, sonst ``True``. + """ + if event.type == pygame.QUIT: + return False + return True + + def update_game(self) -> bool: + """Aktualisiert den Spielzustand um einen Frame. + + Returns: + ``False``, wenn das Spiel beendet werden soll, sonst ``True``. + """ + return True + + def draw_game(self) -> None: + """Zeichnet den aktuellen Frame und bringt ihn zur Anzeige.""" + pygame.display.flip() + + def run(self) -> None: + """Startet das Spiel: Initialisierung, Hauptschleife, Beenden.""" + self.init_game() + self.game_loop() + self.exit_game() diff --git a/main.py b/main.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/main.py @@ -0,0 +1 @@ + diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..e69de29