doku doku
This commit is contained in:
parent
5bccd0df77
commit
c83499393a
13
game.py
13
game.py
@ -2,11 +2,24 @@ from gameobject import *
|
|||||||
from world import *
|
from world import *
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
|
"""
|
||||||
|
Obsahuje logiku hry
|
||||||
|
|
||||||
|
@Author: zbyv
|
||||||
|
@Date: 22.11.2023
|
||||||
|
"""
|
||||||
def __init__(self, world: World, hero: GameObject, home: GameObject):
|
def __init__(self, world: World, hero: GameObject, home: GameObject):
|
||||||
self.__world = world
|
self.__world = world
|
||||||
self.__hero = hero
|
self.__hero = hero
|
||||||
self.__home = home
|
self.__home = home
|
||||||
|
|
||||||
|
"""
|
||||||
|
Spusti hru
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True: hrdina dosel na domovskou pozici
|
||||||
|
False: hrdina se dostal na prazdne pole
|
||||||
|
"""
|
||||||
def run(self) -> bool:
|
def run(self) -> bool:
|
||||||
gui = Gui(self.__world.width, self.__world.height)
|
gui = Gui(self.__world.width, self.__world.height)
|
||||||
|
|
||||||
|
|||||||
@ -2,16 +2,37 @@ from vector2 import *
|
|||||||
from gui import *
|
from gui import *
|
||||||
|
|
||||||
class GameObject:
|
class GameObject:
|
||||||
|
"""
|
||||||
|
Dynamicky objekt
|
||||||
|
|
||||||
|
@Author: zbyv
|
||||||
|
@Date: 22.11.2023
|
||||||
|
"""
|
||||||
def __init__(self, position: Vector2, symbol: str) -> None:
|
def __init__(self, position: Vector2, symbol: str) -> None:
|
||||||
self.__position = position
|
self.__position = position
|
||||||
self.__symbol = symbol
|
self.__symbol = symbol
|
||||||
|
|
||||||
|
"""
|
||||||
|
Pozice objektu
|
||||||
|
"""
|
||||||
@property
|
@property
|
||||||
def position(self):
|
def position(self):
|
||||||
return self.__position
|
return self.__position
|
||||||
|
|
||||||
|
"""
|
||||||
|
Posune objekt zadanym vektorem
|
||||||
|
|
||||||
|
Args:
|
||||||
|
direction: Vektor, o ktery se ma objekt posunout
|
||||||
|
"""
|
||||||
def move(self, direction: Vector2):
|
def move(self, direction: Vector2):
|
||||||
self.__position += direction
|
self.__position += direction
|
||||||
|
|
||||||
|
"""
|
||||||
|
Vykresli objekt na uvedenou instanci Gui
|
||||||
|
|
||||||
|
Args:
|
||||||
|
gui: Gui, na ktere se ma objekt vykreslit
|
||||||
|
"""
|
||||||
def draw(self, gui: Gui):
|
def draw(self, gui: Gui):
|
||||||
gui.draw(self.__position.x, self.__position.y, self.__symbol)
|
gui.draw(self.__position.x, self.__position.y, self.__symbol)
|
||||||
31
gui.py
31
gui.py
@ -1,25 +1,52 @@
|
|||||||
from vector2 import *
|
from vector2 import *
|
||||||
|
|
||||||
class Gui:
|
class Gui:
|
||||||
|
"""
|
||||||
|
Zahrnuje funkcionalitu pro uzivatelsky vstup a vystup
|
||||||
|
|
||||||
|
@Author: zbyv
|
||||||
|
@Date: 22.11.2023
|
||||||
|
"""
|
||||||
def __init__(self, width: int, height: int) -> None:
|
def __init__(self, width: int, height: int) -> None:
|
||||||
self.__width = width
|
self.__width = width
|
||||||
self.__height = height
|
self.__height = height
|
||||||
# self.__data = None
|
# self.__data = None
|
||||||
self.clear()
|
self.clear()
|
||||||
|
|
||||||
def draw(self, x: int, y: int, symbol: str):
|
"""
|
||||||
|
Nastavi zadany symbol na zadanou pozici
|
||||||
|
|
||||||
|
Args:
|
||||||
|
x: `x` souradnice
|
||||||
|
y: `y` souradnice
|
||||||
|
symbol: znak pro vykresleni
|
||||||
|
"""
|
||||||
|
def draw(self, x: int, y: int, symbol: str) -> None:
|
||||||
self.__data[x + y * self.__width] = symbol
|
self.__data[x + y * self.__width] = symbol
|
||||||
|
|
||||||
def show(self):
|
"""
|
||||||
|
Vykresli cele herni pole
|
||||||
|
"""
|
||||||
|
def show(self) -> None:
|
||||||
for y in range(self.__height):
|
for y in range(self.__height):
|
||||||
for x in range(self.__width):
|
for x in range(self.__width):
|
||||||
print(self.__data[x + y * self.__width], end="")
|
print(self.__data[x + y * self.__width], end="")
|
||||||
|
|
||||||
print("")
|
print("")
|
||||||
|
|
||||||
|
"""
|
||||||
|
Vycisti cele herni pole
|
||||||
|
"""
|
||||||
def clear(self):
|
def clear(self):
|
||||||
self.__data = [" " for _ in range(self.__width * self.__height)]
|
self.__data = [" " for _ in range(self.__width * self.__height)]
|
||||||
|
|
||||||
|
"""
|
||||||
|
Vyzada si od uzivatele smer pohybu
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Vector2 reprezentujici zadany smer pohybu
|
||||||
|
"""
|
||||||
|
|
||||||
def input_direction(self) -> Vector2:
|
def input_direction(self) -> Vector2:
|
||||||
match input("pohyb: "):
|
match input("pohyb: "):
|
||||||
case "2":
|
case "2":
|
||||||
|
|||||||
23
vector2.py
23
vector2.py
@ -1,25 +1,44 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
class Vector2:
|
class Vector2:
|
||||||
|
"""
|
||||||
|
Dvojdimenzionalni vektor
|
||||||
|
|
||||||
|
@Author: zbyv
|
||||||
|
@Date: 22.11.2023
|
||||||
|
"""
|
||||||
def __init__(self, x, y) -> None:
|
def __init__(self, x, y) -> None:
|
||||||
self.__x = x
|
self.__x = x
|
||||||
self.__y = y
|
self.__y = y
|
||||||
|
|
||||||
|
"""
|
||||||
|
`x` souradnice tohoto vektoru
|
||||||
|
"""
|
||||||
@property
|
@property
|
||||||
def x(self):
|
def x(self):
|
||||||
return self.__x
|
return self.__x
|
||||||
|
|
||||||
|
"""
|
||||||
|
`y` souradnice tohoto vektoru
|
||||||
|
"""
|
||||||
@property
|
@property
|
||||||
def y(self):
|
def y(self):
|
||||||
return self.__y
|
return self.__y
|
||||||
|
|
||||||
|
"""
|
||||||
|
Kouzelna metoda pro soucet vektoru
|
||||||
|
"""
|
||||||
def __add__(self, other: Vector2) -> Vector2:
|
def __add__(self, other: Vector2) -> Vector2:
|
||||||
return Vector2(self.x + other.x, self.y + other.y)
|
return Vector2(self.x + other.x, self.y + other.y)
|
||||||
|
|
||||||
|
"""
|
||||||
|
Kouzelna metoda pro porovnani vektoru
|
||||||
|
"""
|
||||||
def __eq__(self, other) -> bool:
|
def __eq__(self, other) -> bool:
|
||||||
return self.x == other.x and self.y == other.y
|
return self.x == other.x and self.y == other.y
|
||||||
|
|
||||||
|
"""
|
||||||
|
Kouzelna metoda pro prevod vektoru na str
|
||||||
|
"""
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return f"{self.x}; {self.y}"
|
return f"{self.x}; {self.y}"
|
||||||
|
|
||||||
|
|
||||||
29
world.py
29
world.py
@ -2,24 +2,51 @@ from gui import *
|
|||||||
from vector2 import *
|
from vector2 import *
|
||||||
|
|
||||||
class World:
|
class World:
|
||||||
|
"""
|
||||||
|
Staticky herni svet
|
||||||
|
|
||||||
|
@Author: zbyv
|
||||||
|
@Date: 22.11.2023
|
||||||
|
"""
|
||||||
def __init__(self, data: list[list[int]], symbols: list[str]) -> None:
|
def __init__(self, data: list[list[int]], symbols: list[str]) -> None:
|
||||||
self.__width = len(data[0])
|
self.__width = len(data[0])
|
||||||
self.__height = len(data)
|
self.__height = len(data)
|
||||||
self.__data = data
|
self.__data = data
|
||||||
self.__symbols = symbols
|
self.__symbols = symbols
|
||||||
|
|
||||||
|
"""
|
||||||
|
Sirka sveta
|
||||||
|
"""
|
||||||
@property
|
@property
|
||||||
def width(self):
|
def width(self):
|
||||||
return self.__width
|
return self.__width
|
||||||
|
|
||||||
|
"""
|
||||||
|
Vyska sveta
|
||||||
|
"""
|
||||||
@property
|
@property
|
||||||
def height(self):
|
def height(self):
|
||||||
return self.__height
|
return self.__height
|
||||||
|
|
||||||
|
"""
|
||||||
|
Zjisti, jestli je policko na zadane pozici prazdne.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
position: Pozice pro kontrolu
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True pokud je policko prazdne, jinak False
|
||||||
|
"""
|
||||||
def is_empty(self, position: Vector2):
|
def is_empty(self, position: Vector2):
|
||||||
return self.__data[position.y][position.x] == 0
|
return self.__data[position.y][position.x] == 0
|
||||||
|
|
||||||
|
"""
|
||||||
|
Vykresli svet na zadanou instanci Gui
|
||||||
|
|
||||||
|
Args:
|
||||||
|
gui: Gui, na ktere se svet vykresli
|
||||||
|
"""
|
||||||
def draw(self, gui: Gui) -> None:
|
def draw(self, gui: Gui) -> None:
|
||||||
for y in range(self.height):
|
for y in range(self.height):
|
||||||
for x in range(self.width):
|
for x in range(self.width):
|
||||||
gui.draw(x, y, self.__symbols[self.__data[y][x]])
|
gui.draw(x, y, self.__symbols[self.__data[y][x]])
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user