diff --git a/src/game/block.py b/src/game/block.py index 14b2d6a..ee1ee4a 100644 --- a/src/game/block.py +++ b/src/game/block.py @@ -1,13 +1,16 @@ import pygame -from utils import CONFIG, Position, Size +from utils import CONFIG, Size class Block(pygame.sprite.Sprite): def __init__( - self, /, *, group: pygame.sprite.Group, pos: Position, color: str + self, /, *, group: pygame.sprite.Group, pos: pygame.Vector2, color: str ) -> None: super().__init__(group) self.image = pygame.Surface(CONFIG.game.cell) self.image.fill(color) - self.rect = self.image.get_rect(topleft=(0, 0)) + self.pos = pygame.Vector2(pos) + x = self.pos.x * CONFIG.game.cell.width + y = self.pos.y * CONFIG.game.cell.height + self.rect = self.image.get_rect(topleft=(x, y)) diff --git a/src/utils/__init__.py b/src/utils/__init__.py index da5c232..aebd831 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -1,7 +1,14 @@ from .config import CONFIG +from .figure import Figure, FigureConfig from .log import log 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", + "Figure", + "FigureConfig", +] diff --git a/src/utils/config.py b/src/utils/config.py index 49dc9ec..aa063f8 100644 --- a/src/utils/config.py +++ b/src/utils/config.py @@ -1,7 +1,7 @@ from attr import define, field +from pygame import Vector2 as Vec2 from .colors import TokyoNightNight -from .position import Position from .size import Size PADDING = 20 @@ -16,7 +16,7 @@ class Game: padding: int = PADDING cell: Size = Size(40, 40) size: Size = Size(columns * cell.width, rows * cell.width) - pos: Position = Position(padding, padding) + pos: Vec2 = Vec2(padding, padding) @define diff --git a/src/utils/figure.py b/src/utils/figure.py new file mode 100644 index 0000000..15116ac --- /dev/null +++ b/src/utils/figure.py @@ -0,0 +1,84 @@ +import random +from enum import Enum +from typing import NamedTuple + +from attr import define +from pygame import Vector2 as Vec2 + +from .colors import TokyoNightNight + + +class FigureConfig(NamedTuple): + shape: list[Vec2, Vec2, Vec2, Vec2] + color: str + + +class Figure(Enum): + I = FigureConfig( + [ + Vec2(0, 0), + Vec2(0, -1), + Vec2(0, -2), + Vec2(0, 1), + ], + TokyoNightNight().cyan, + ) + O = FigureConfig( + [ + Vec2(0, 0), + Vec2(0, -1), + Vec2(1, 0), + Vec2(1, -1), + ], + TokyoNightNight().yellow, + ) + T = FigureConfig( + [ + Vec2(0, 0), + Vec2(-1, 0), + Vec2(1, 0), + Vec2(0, -1), + ], + TokyoNightNight().purple, + ) + + S = FigureConfig( + [ + Vec2(0, 0), + Vec2(-1, 0), + Vec2(0, -1), + Vec2(1, -1), + ], + TokyoNightNight().green, + ) + Z = FigureConfig( + [ + Vec2(0, 0), + Vec2(1, 0), + Vec2(0, -1), + Vec2(-1, -1), + ], + TokyoNightNight().red, + ) + J = FigureConfig( + [ + Vec2(0, 0), + Vec2(0, -1), + Vec2(0, 1), + Vec2(-1, 1), + ], + TokyoNightNight().blue, + ) + L = FigureConfig( + [ + Vec2(0, 0), + Vec2(0, -1), + Vec2(0, 1), + Vec2(1, 1), + ], + TokyoNightNight().orange, + ) + + @staticmethod + def random() -> "Figure": + return random.choice(list(Figure)) diff --git a/src/utils/position.py b/src/utils/position.py deleted file mode 100644 index 9dc1474..0000000 --- a/src/utils/position.py +++ /dev/null @@ -1,43 +0,0 @@ -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 - )