refactor(utils): replace Position with Vector2

This commit is contained in:
Kristofers Solo 2024-01-04 02:02:33 +02:00
parent 9b82ca080e
commit 54b5aa5774
5 changed files with 101 additions and 50 deletions

View File

@ -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))

View File

@ -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",
]

View File

@ -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

84
src/utils/figure.py Normal file
View File

@ -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))

View File

@ -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
)