From 5bccd0df779d0dac65487b7b87f91fd8f718e7f2 Mon Sep 17 00:00:00 2001 From: tovjemam Date: Wed, 22 Nov 2023 21:03:55 +0100 Subject: [PATCH] 1 --- game.py | 26 +++++++++++++++++ gameobject.py | 17 +++++++++++ gui.py | 35 ++++++++++++++++++++++ main.py | 23 +++++++++++++++ tests/game_test.py | 61 ++++++++++++++++++++++++++++++++++++++ tests/gameobject_test.py | 44 ++++++++++++++++++++++++++++ tests/gui_test.py | 63 ++++++++++++++++++++++++++++++++++++++++ tests/vector2_test.py | 41 ++++++++++++++++++++++++++ tests/world_test.py | 62 +++++++++++++++++++++++++++++++++++++++ vector2.py | 25 ++++++++++++++++ world.py | 25 ++++++++++++++++ 11 files changed, 422 insertions(+) create mode 100644 game.py create mode 100644 gameobject.py create mode 100644 gui.py create mode 100644 main.py create mode 100644 tests/game_test.py create mode 100644 tests/gameobject_test.py create mode 100644 tests/gui_test.py create mode 100644 tests/vector2_test.py create mode 100644 tests/world_test.py create mode 100644 vector2.py create mode 100644 world.py diff --git a/game.py b/game.py new file mode 100644 index 0000000..bbe1985 --- /dev/null +++ b/game.py @@ -0,0 +1,26 @@ +from gameobject import * +from world import * + +class Game: + def __init__(self, world: World, hero: GameObject, home: GameObject): + self.__world = world + self.__hero = hero + self.__home = home + + def run(self) -> bool: + gui = Gui(self.__world.width, self.__world.height) + + while True: + gui.clear() + self.__world.draw(gui) + self.__home.draw(gui) + self.__hero.draw(gui) + gui.show() + + if self.__hero.position == self.__home.position: + return True + + if not self.__world.is_empty(self.__hero.position): + return False + + self.__hero.move(gui.input_direction()) diff --git a/gameobject.py b/gameobject.py new file mode 100644 index 0000000..bda2d84 --- /dev/null +++ b/gameobject.py @@ -0,0 +1,17 @@ +from vector2 import * +from gui import * + +class GameObject: + def __init__(self, position: Vector2, symbol: str) -> None: + self.__position = position + self.__symbol = symbol + + @property + def position(self): + return self.__position + + def move(self, direction: Vector2): + self.__position += direction + + def draw(self, gui: Gui): + gui.draw(self.__position.x, self.__position.y, self.__symbol) \ No newline at end of file diff --git a/gui.py b/gui.py new file mode 100644 index 0000000..a1eb725 --- /dev/null +++ b/gui.py @@ -0,0 +1,35 @@ +from vector2 import * + +class Gui: + def __init__(self, width: int, height: int) -> None: + self.__width = width + self.__height = height + # self.__data = None + self.clear() + + def draw(self, x: int, y: int, symbol: str): + self.__data[x + y * self.__width] = symbol + + def show(self): + for y in range(self.__height): + for x in range(self.__width): + print(self.__data[x + y * self.__width], end="") + + print("") + + def clear(self): + self.__data = [" " for _ in range(self.__width * self.__height)] + + def input_direction(self) -> Vector2: + match input("pohyb: "): + case "2": + return Vector2(0, 1) + case "4": + return Vector2(-1, 0) + case "6": + return Vector2(1, 0) + case "8": + return Vector2(0, -1) + case _: + return Vector2(0, 0) + diff --git a/main.py b/main.py new file mode 100644 index 0000000..25046c5 --- /dev/null +++ b/main.py @@ -0,0 +1,23 @@ +from game import * + +def main(): + world = World( + [[1,1,1,1,1,1], + [1,0,0,1,0,1], + [1,0,0,1,0,1], + [1,0,0,0,0,1], + [1,1,1,1,1,1]], + [' ','#']) + + hero = GameObject(Vector2(1,1),"@") + home = GameObject(Vector2(4,1),"^") + + destination = Game(world,hero,home).run() + if destination: + print("Vitej doma!") + else: + print("... a uz ho nikdy nikdo nevidel... ") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/tests/game_test.py b/tests/game_test.py new file mode 100644 index 0000000..6c6ee36 --- /dev/null +++ b/tests/game_test.py @@ -0,0 +1,61 @@ +import unittest +import builtins +import typing +from typing import Any +from typing import Optional + +from game import Game +from world import World +from gameobject import GameObject +from vector2 import Vector2 + +class GameTest(unittest.TestCase): + class InputShim: + """ + Třída pro nahrazení standardní funkce input pro vstup dat + """ + __idx:int = 0 + __lines:list[str] = [] + + @staticmethod + def set_input_data(lines:list[str]): + GameTest.InputShim.__idx = 0 + GameTest.InputShim.__lines = lines + builtins.input = typing.cast(Any, GameTest.InputShim.__next) + + @staticmethod + def get_latest_input() -> Optional[str]: + if GameTest.InputShim.__idx==0: + return None + else: + return GameTest.InputShim.__lines[GameTest.InputShim.__idx-1] + + @staticmethod + def __next(prompt:str=""): + assert GameTest.InputShim.__idx Optional[str]: + if GuiTest.InputShim.__idx==0: + return None + else: + return GuiTest.InputShim.__lines[GuiTest.InputShim.__idx-1] + + @staticmethod + def __next(prompt:str=""): + assert GuiTest.InputShim.__idx None: + self.__x = x + self.__y = y + + @property + def x(self): + return self.__x + + @property + def y(self): + return self.__y + + def __add__(self, other: Vector2) -> Vector2: + return Vector2(self.x + other.x, self.y + other.y) + + def __eq__(self, other) -> bool: + return self.x == other.x and self.y == other.y + + def __str__(self) -> str: + return f"{self.x}; {self.y}" + + \ No newline at end of file diff --git a/world.py b/world.py new file mode 100644 index 0000000..4722ec4 --- /dev/null +++ b/world.py @@ -0,0 +1,25 @@ +from gui import * +from vector2 import * + +class World: + def __init__(self, data: list[list[int]], symbols: list[str]) -> None: + self.__width = len(data[0]) + self.__height = len(data) + self.__data = data + self.__symbols = symbols + + @property + def width(self): + return self.__width + + @property + def height(self): + return self.__height + + def is_empty(self, position: Vector2): + return self.__data[position.y][position.x] == 0 + + def draw(self, gui: Gui) -> None: + for y in range(self.height): + for x in range(self.width): + gui.draw(x, y, self.__symbols[self.__data[y][x]]) \ No newline at end of file