mirror of
https://github.com/kristoferssolo/Tetris.git
synced 2025-10-21 20:00:35 +00:00
refactor(utils): move Position and Size to separate files
This commit is contained in:
parent
e045779158
commit
9b82ca080e
@ -1,5 +1,7 @@
|
|||||||
from .config import CONFIG, Position, Size
|
from .config import CONFIG
|
||||||
from .log import log
|
from .log import log
|
||||||
from .path import BASE_PATH
|
from .path import BASE_PATH
|
||||||
|
from .position import Position
|
||||||
|
from .size import Size
|
||||||
|
|
||||||
__all__ = ["BASE_PATH", "CONFIG", "log", "Size", "Position"]
|
__all__ = ["BASE_PATH", "CONFIG", "log", "Size", "Position"]
|
||||||
|
|||||||
@ -1,42 +1,12 @@
|
|||||||
from typing import NamedTuple, Union
|
|
||||||
|
|
||||||
from attr import define, field
|
from attr import define, field
|
||||||
|
|
||||||
from .colors import TokyoNightNight
|
from .colors import TokyoNightNight
|
||||||
|
from .position import Position
|
||||||
|
from .size import Size
|
||||||
|
|
||||||
PADDING = 20
|
PADDING = 20
|
||||||
|
|
||||||
|
|
||||||
class Size(NamedTuple):
|
|
||||||
width: int | float
|
|
||||||
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
|
||||||
@ -44,8 +14,8 @@ class Game:
|
|||||||
line_width: int = 1
|
line_width: int = 1
|
||||||
border_radius: int = 2
|
border_radius: int = 2
|
||||||
padding: int = PADDING
|
padding: int = PADDING
|
||||||
cell_size: int = 40
|
cell: Size = Size(40, 40)
|
||||||
size: Size = Size(columns * cell_size, rows * cell_size)
|
size: Size = Size(columns * cell.width, rows * cell.width)
|
||||||
pos: Position = Position(padding, padding)
|
pos: Position = Position(padding, padding)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
43
src/utils/position.py
Normal file
43
src/utils/position.py
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
from typing import NamedTuple, Union
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
def add_x(self, other: Union["Position", int, float]) -> "Position":
|
||||||
|
if isinstance(other, Position):
|
||||||
|
return Position(self.x + other.x, self.y)
|
||||||
|
return Position(self.x + other, self.y)
|
||||||
|
|
||||||
|
def add_y(self, other: Union["Position", int, float]) -> "Position":
|
||||||
|
if isinstance(other, Position):
|
||||||
|
return Position(self.x, self.y + other.y)
|
||||||
|
return Position(self.x, self.y + other)
|
||||||
|
|
||||||
|
def sub_x(self, other: Union["Position", int, float]) -> "Position":
|
||||||
|
if isinstance(other, Position):
|
||||||
|
return Position(self.x - other.x, self.y)
|
||||||
|
return Position(self.x - other, self.y)
|
||||||
|
|
||||||
|
def sub_y(self, other: Union["Position", int, float]) -> "Position":
|
||||||
|
if isinstance(other, Position):
|
||||||
|
return Position(self.x, self.y - other.y)
|
||||||
|
return Position(self.x, self.y - other)
|
||||||
|
|
||||||
|
def to_grid(self) -> "Position":
|
||||||
|
from .config import CONFIG
|
||||||
|
|
||||||
|
return Position(
|
||||||
|
self.x * CONFIG.game.cell.width, self.y * CONFIG.game.cell.height
|
||||||
|
)
|
||||||
16
src/utils/size.py
Normal file
16
src/utils/size.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
from typing import NamedTuple, Union
|
||||||
|
|
||||||
|
|
||||||
|
class Size(NamedTuple):
|
||||||
|
width: int | float
|
||||||
|
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)
|
||||||
Loading…
Reference in New Issue
Block a user