feat(utils): add module

This commit is contained in:
Kristofers Solo 2024-01-03 17:03:16 +02:00
parent d3e1d8b7bf
commit fc305cd239
6 changed files with 118 additions and 6 deletions

View File

@ -2,10 +2,8 @@
import argparse
from ai import train
from loguru import logger
from path import BASE_PATH
from py2048 import Menu
from utils import BASE_PATH
def pos_int(string: str) -> int:

View File

@ -1,3 +0,0 @@
from pathlib import Path
BASE_PATH = Path(__file__).resolve().parent.parent

20
src/utils/__init__.py Normal file
View File

@ -0,0 +1,20 @@
from pathlib import Path
from .collections import Board, Font, Header, Position, Screen, Size, Tile
from .color import ColorScheme
from .enums import Direction
BASE_PATH = Path(__file__).resolve().parent.parent.parent.parent
__all__ = [
"Board",
"ColorScheme",
"Direction",
"Font",
"Position",
"Size",
"Tile",
"Header",
"Screen",
"BASE_PATH",
]

52
src/utils/collections.py Normal file
View File

@ -0,0 +1,52 @@
from typing import NamedTuple
from attr import Factory, define, field
class Position(NamedTuple):
x: int | float
y: int | float
class Size(NamedTuple):
width: int
height: int
@define
class Font:
family: str = "Roboto"
size: int = 32
@define
class Border:
width: int
radius: int
@define
class Tile:
size: int = 75
border: Border = Border(size // 20, size // 10)
initial_count: int = 2
value_probability: float = 0.9
@define
class Board:
len: int = 4
size: Size = Size(len * Tile().size, len * Tile().size)
pos: Position = Position(Tile().size // 2, Tile().size + Tile().size // 2)
@define
class Header:
size: Size = Size(Board().size.width + Tile().size, Tile().size)
@define
class Screen:
size: Size = Size(
Header().size.width, Board().size.height + Tile().size + Header().size.height
)

27
src/utils/color.py Normal file
View File

@ -0,0 +1,27 @@
from enum import Enum
class Original:
TILE_0 = "#cdc1b4"
TILE_2 = "#eee4da"
TILE_4 = "#eee1c9"
TILE_8 = "#f3b27a"
TILE_16 = "#f69664"
TILE_32 = "#f77c5f"
TILE_64 = "#f75f3b"
TILE_128 = "#edcf72"
TILE_256 = "#edcc61"
TILE_512 = "#edc850"
TILE_1024 = "#edc53f"
TILE_2048 = "#edc22e"
TILE_ELSE = "#ff0000"
LIGHT_TEXT = "#f9f6f2"
DARK_TEXT = "#776e65"
OTHER = "#000000"
BG = "#faf8ef"
BOARD_BG = "#bbada0"
class ColorScheme(Enum):
ORIGINAL = Original
DARK = ... # TODO: Implement dark color scheme

18
src/utils/enums.py Normal file
View File

@ -0,0 +1,18 @@
from enum import Enum
from .collections import Position
class Direction(Enum):
UP = Position(0, -1)
DOWN = Position(0, 1)
LEFT = Position(-1, 0)
RIGHT = Position(1, 0)
def __mul__(self, num: int) -> Position:
"""Multiply the direction by a constant."""
return Position(self.value.x * num, self.value.y * num)
def __imul__(self, num: int) -> tuple[int, int]:
"""Multiply the direction by a constant."""
return Position(self.value.x * num, self.value.y * num)