feat(utils): add Size and Position

This commit is contained in:
Kristofers Solo 2024-01-04 00:30:41 +02:00
parent 04e346a9b9
commit 306e59ce07
2 changed files with 38 additions and 9 deletions

View File

@ -1,5 +1,5 @@
from .config import CONFIG from .config import CONFIG, Position, Size
from .log import log from .log import log
from .path import BASE_PATH from .path import BASE_PATH
__all__ = ["BASE_PATH", "CONFIG", "log"] __all__ = ["BASE_PATH", "CONFIG", "log", "Size", "Position"]

View File

@ -1,34 +1,63 @@
from typing import NamedTuple from typing import NamedTuple, Union
from attr import define from attr import define, field
from .colors import TokyoNightNight from .colors import TokyoNightNight
PADDING = 20
class Size(NamedTuple): class Size(NamedTuple):
width: int width: int | float
height: int height: int | float
def add(self, other: Union["Size", int, float]) -> "Size":
if isinstance(other, Size):
return Size(self.width + other.width, self.height + other.height)
return Size(self.width + other, self.height + other)
def sub(self, other: Union["Size", int, float]) -> "Size":
if isinstance(other, Size):
return Size(self.width - other.width, self.height - other.height)
return Size(self.width - other, self.height - other)
class Position(NamedTuple):
x: int | float
y: int | float
def add(self, other: Union["Position", int, float]) -> "Position":
if isinstance(other, Position):
return Position(self.x + other.x, self.y + other.y)
return Position(self.x + other, self.y + other)
def sub(self, other: Union["Position", int, float]) -> "Position":
if isinstance(other, Position):
return Position(self.x - other.x, self.y - other.y)
return Position(self.x - other, self.y - other)
@define @define
class Game: class Game:
columns: int = 10 columns: int = 10
rows: int = 20 rows: int = 20
padding: int = PADDING
cell_size: int = 40 cell_size: int = 40
size: Size = Size(columns * cell_size, rows * cell_size) size: Size = Size(columns * cell_size, rows * cell_size)
pos: Position = Position(padding, padding)
@define @define
class SideBar: class SideBar:
size: Size = Size(200, Game().size.height) size: Size = Size(200, Game().size.height)
preview_height_fraction: float = 0.7 score: Size = Size(size.width, size.height * 0.3)
score_height_fraction: float = 1 - preview_height_fraction preview: Size = Size(size.width, size.height * 0.7)
@define @define
class Window: class Window:
title = "Tetris" title = "Tetris"
padding: int = 20 padding: int = PADDING
size: Size = Size( size: Size = Size(
Game().size.width + SideBar().size.width + padding * 3, Game().size.width + SideBar().size.width + padding * 3,
Game().size.height + padding * 2, Game().size.height + padding * 2,