mirror of
https://github.com/kristoferssolo/2048.git
synced 2025-10-21 15:20:35 +00:00
refactor(game): setup formatter
This commit is contained in:
parent
abdecc36d2
commit
7d1edb3c43
@ -23,10 +23,16 @@ warn_unreachable = true
|
|||||||
warn_unused_configs = true
|
warn_unused_configs = true
|
||||||
|
|
||||||
[tool.ruff]
|
[tool.ruff]
|
||||||
line-length = 160
|
line-length = 120
|
||||||
|
indent-width = 4
|
||||||
|
|
||||||
[tool.black]
|
[tool.ruff.lint]
|
||||||
line-length = 160
|
select = ["I"]
|
||||||
|
|
||||||
[tool.ruff.flake8-quotes]
|
[tool.ruff.format]
|
||||||
docstring-quotes = "double"
|
quote-style = "double"
|
||||||
|
indent-style = "space"
|
||||||
|
skip-magic-trailing-comma = false
|
||||||
|
line-ending = "auto"
|
||||||
|
docstring-code-format = true
|
||||||
|
docstring-code-line-length = 40
|
||||||
|
|||||||
@ -1,12 +1,10 @@
|
|||||||
import random
|
import random
|
||||||
|
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
from .colors import COLORS
|
from .color import Color
|
||||||
from .config import Config
|
from .config import Config
|
||||||
|
|
||||||
from .utils import Direction, grid_pos
|
from .utils import Direction, grid_pos
|
||||||
|
|
||||||
|
|
||||||
@ -25,7 +23,7 @@ class Block(pygame.sprite.Sprite):
|
|||||||
|
|
||||||
def _draw_value(self) -> None:
|
def _draw_value(self) -> None:
|
||||||
"""Draw the value of the block"""
|
"""Draw the value of the block"""
|
||||||
text = self.font.render(str(self.value), True, COLORS.FG)
|
text = self.font.render(str(self.value), True, Color.FG)
|
||||||
text_rect = text.get_rect(center=self.image.get_rect().center)
|
text_rect = text.get_rect(center=self.image.get_rect().center)
|
||||||
self.image.blit(text, text_rect)
|
self.image.blit(text, text_rect)
|
||||||
|
|
||||||
@ -84,19 +82,19 @@ class Block(pygame.sprite.Sprite):
|
|||||||
def _change_color(self) -> None:
|
def _change_color(self) -> None:
|
||||||
"""Change the color of the block based on its value"""
|
"""Change the color of the block based on its value"""
|
||||||
color_map = {
|
color_map = {
|
||||||
2: COLORS.BLUE,
|
2: Color.BLUE,
|
||||||
4: COLORS.BLUE0,
|
4: Color.BLUE0,
|
||||||
8: COLORS.BLUE1,
|
8: Color.BLUE1,
|
||||||
16: COLORS.BLUE2,
|
16: Color.BLUE2,
|
||||||
32: COLORS.DARK3,
|
32: Color.DARK3,
|
||||||
64: COLORS.BORDER_HIGHLIGHT,
|
64: Color.BORDER_HIGHLIGHT,
|
||||||
128: COLORS.BLUE5,
|
128: Color.BLUE5,
|
||||||
256: COLORS.BLUE6,
|
256: Color.BLUE6,
|
||||||
512: COLORS.BLUE7,
|
512: Color.BLUE7,
|
||||||
1024: COLORS.ORANGE,
|
1024: Color.ORANGE,
|
||||||
2048: COLORS.RED,
|
2048: Color.RED,
|
||||||
}
|
}
|
||||||
self.image.fill(color_map.get(self.value, COLORS.ERROR))
|
self.image.fill(color_map.get(self.value, Color.ERROR))
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
"""Return a string representation of the block"""
|
"""Return a string representation of the block"""
|
||||||
|
|||||||
@ -4,11 +4,18 @@ import pygame
|
|||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
from .block import Block
|
from .block import Block
|
||||||
|
from .color import Color
|
||||||
from .config import Config
|
from .config import Config
|
||||||
from .utils import Direction
|
from .utils import Direction
|
||||||
|
|
||||||
|
|
||||||
class Board(pygame.sprite.Group):
|
class Board(pygame.sprite.Group):
|
||||||
|
def __init__(self, screen: pygame.Surface):
|
||||||
|
super().__init__()
|
||||||
|
self.generate_block(Config.INITIAL_BLOCK_COUNT)
|
||||||
|
self.screen = screen
|
||||||
|
self._draw_grid()
|
||||||
|
|
||||||
def move(self, direction: Direction):
|
def move(self, direction: Direction):
|
||||||
blocks = self.sprites()
|
blocks = self.sprites()
|
||||||
block: Block
|
block: Block
|
||||||
@ -28,6 +35,15 @@ class Board(pygame.sprite.Group):
|
|||||||
|
|
||||||
self.generate_block()
|
self.generate_block()
|
||||||
|
|
||||||
|
def _draw_grid(self) -> None:
|
||||||
|
"""Draw the grid."""
|
||||||
|
for x in range(0, Config.WIDTH + 20, Config.BLOCK_SIZE):
|
||||||
|
pygame.draw.line(
|
||||||
|
self.screen, Color.BG_HIGHLIGHT, (x, 0), (x, Config.HEIGHT)
|
||||||
|
)
|
||||||
|
for y in range(0, Config.HEIGHT + 20, Config.BLOCK_SIZE):
|
||||||
|
pygame.draw.line(self.screen, Color.BG_HIGHLIGHT, (0, y), (Config.WIDTH, y))
|
||||||
|
|
||||||
def generate_block(self, amount: int = 1, *pos: tuple[int, int]) -> None:
|
def generate_block(self, amount: int = 1, *pos: tuple[int, int]) -> None:
|
||||||
"""Generate `amount` number of blocks."""
|
"""Generate `amount` number of blocks."""
|
||||||
if pos:
|
if pos:
|
||||||
@ -43,7 +59,9 @@ class Board(pygame.sprite.Group):
|
|||||||
y = random.randint(0, 3) * Config.BLOCK_SIZE
|
y = random.randint(0, 3) * Config.BLOCK_SIZE
|
||||||
block = Block(x, y, self)
|
block = Block(x, y, self)
|
||||||
|
|
||||||
colliding_blocks = pygame.sprite.spritecollide(block, self, False) # check for collisions
|
colliding_blocks = pygame.sprite.spritecollide(
|
||||||
|
block, self, False
|
||||||
|
) # check for collisions
|
||||||
|
|
||||||
if not colliding_blocks:
|
if not colliding_blocks:
|
||||||
self.add(block)
|
self.add(block)
|
||||||
|
|||||||
241
src/py2048/color.py
Normal file
241
src/py2048/color.py
Normal file
@ -0,0 +1,241 @@
|
|||||||
|
class TokyoNightDay:
|
||||||
|
BG = "#E1E2E7"
|
||||||
|
BG_DARK = "#E9E9EC"
|
||||||
|
BG_FLOAT = "#E9E9EC"
|
||||||
|
BG_HIGHLIGHT = "#C4C8DA"
|
||||||
|
BG_POPUP = "#E9E9EC"
|
||||||
|
BG_SEARCH = "#7890DD"
|
||||||
|
BG_SIDEBAR = "#E9E9EC"
|
||||||
|
BG_STATUSLINE = "#E9E9EC"
|
||||||
|
BG_VISUAL = "#B6BFE2"
|
||||||
|
BLACK = "#E9E9ED"
|
||||||
|
BLUE = "#2E7DE9"
|
||||||
|
BLUE0 = "#7890DD"
|
||||||
|
BLUE1 = "#188092"
|
||||||
|
BLUE2 = "#07879D"
|
||||||
|
BLUE5 = "#006A83"
|
||||||
|
BLUE6 = "#2E5857"
|
||||||
|
BLUE7 = "#92A6D5"
|
||||||
|
BORDER = "#E9E9ED"
|
||||||
|
BORDER_HIGHLIGHT = "#2496AC"
|
||||||
|
COMMENT = "#848CB5"
|
||||||
|
CYAN = "#007197"
|
||||||
|
DARK3 = "#8990B3"
|
||||||
|
DARK5 = "#68709A"
|
||||||
|
DELTA_ADD = "#57A7BC"
|
||||||
|
DELTA_DELETE = "#D99EA2"
|
||||||
|
DIFF_ADD = "#AECDE6"
|
||||||
|
DIFF_CHANGE = "#D6D8E3"
|
||||||
|
DIFF_DELETE = "#DFCCD4"
|
||||||
|
DIFF_TEXT = "#92A6D5"
|
||||||
|
ERROR = "#C64343"
|
||||||
|
FG = "#3760BF"
|
||||||
|
FG_DARK = "#6172B0"
|
||||||
|
FG_FLOAT = "#3760BF"
|
||||||
|
FG_GUTTER = "#A8AECB"
|
||||||
|
FG_SIDEBAR = "#6172B0"
|
||||||
|
GIT_ADD = "#4197A4"
|
||||||
|
GIT_CHANGE = "#506D9C"
|
||||||
|
GIT_DELETE = "#C47981"
|
||||||
|
GIT_IGNORE = "#8990B3"
|
||||||
|
GIT_SIGNS_ADD = "#399A96"
|
||||||
|
GIT_SIGNS_CHANGE = "#6482BD"
|
||||||
|
GITSIGNS_DELETE = "#C25D64"
|
||||||
|
GREEN = "#587539"
|
||||||
|
GREEN1 = "#387068"
|
||||||
|
GREEN2 = "#38919F"
|
||||||
|
HINT = "#118C74"
|
||||||
|
INFO = "#07879D"
|
||||||
|
MAGENTA = "#9854F1"
|
||||||
|
MAGENTA2 = "#D20065"
|
||||||
|
ORANGE = "#B15C00"
|
||||||
|
PURPLE = "#7847BD"
|
||||||
|
RED = "#F52A65"
|
||||||
|
RED1 = "#C64343"
|
||||||
|
TEAL = "#118C74"
|
||||||
|
TERMINAL_BLACK = "#A1A6C5"
|
||||||
|
WARNING = "#8C6C3E"
|
||||||
|
YELLOW = "#8C6C3E"
|
||||||
|
|
||||||
|
|
||||||
|
class TokyoNightMoon:
|
||||||
|
BG = "#222436"
|
||||||
|
BG_DARK = "#1E2030"
|
||||||
|
BG_FLOAT = "#1E2030"
|
||||||
|
BG_HIGHLIGHT = "#2F334D"
|
||||||
|
BG_POPUP = "#1E2030"
|
||||||
|
BG_SEARCH = "#3E68D7"
|
||||||
|
BG_SIDEBAR = "#1E2030"
|
||||||
|
BG_STATUSLINE = "#1E2030"
|
||||||
|
BG_VISUAL = "#2D3F76"
|
||||||
|
BLACK = "#1B1D2B"
|
||||||
|
BLUE = "#82AAFF"
|
||||||
|
BLUE0 = "#3E68D7"
|
||||||
|
BLUE1 = "#65BCFF"
|
||||||
|
BLUE2 = "#0DB9D7"
|
||||||
|
BLUE5 = "#89DDFF"
|
||||||
|
BLUE6 = "#B4F9F8"
|
||||||
|
BLUE7 = "#394B70"
|
||||||
|
BORDER = "#1B1D2B"
|
||||||
|
BORDER_HIGHLIGHT = "#589ED7"
|
||||||
|
COMMENT = "#636DA6"
|
||||||
|
CYAN = "#86E1FC"
|
||||||
|
DARK3 = "#545C7E"
|
||||||
|
DARK5 = "#737AA2"
|
||||||
|
DELTA_ADD = "#305F6F"
|
||||||
|
DELTA_DELETE = "#6B2E43"
|
||||||
|
DIFF_ADD = "#273849"
|
||||||
|
DIFF_CHANGE = "#252A3F"
|
||||||
|
DIFF_DELETE = "#3A273A"
|
||||||
|
DIFF_TEXT = "#394B70"
|
||||||
|
ERROR = "#C53B53"
|
||||||
|
FG = "#C8D3F5"
|
||||||
|
FG_DARK = "#828BB8"
|
||||||
|
FG_FLOAT = "#C8D3F5"
|
||||||
|
FG_GUTTER = "#3B4261"
|
||||||
|
FG_SIDEBAR = "#828BB8"
|
||||||
|
GIT_ADD = "#B8DB87"
|
||||||
|
GIT_CHANGE = "#7CA1F2"
|
||||||
|
GIT_DELETE = "#E26A75"
|
||||||
|
GIT_IGNORE = "#545C7E"
|
||||||
|
GIT_SIGNS_ADD = "#627259"
|
||||||
|
GIT_SIGNS_CHANGE = "#485A86"
|
||||||
|
GIT_SIGNS_DELETE = "#B55A67"
|
||||||
|
GREEN = "#C3E88D"
|
||||||
|
GREEN1 = "#4FD6BE"
|
||||||
|
GREEN2 = "#41A6B5"
|
||||||
|
HINT = "#4FD6BE"
|
||||||
|
INFO = "#0DB9D7"
|
||||||
|
MAGENTA = "#C099FF"
|
||||||
|
MAGENTA2 = "#FF007C"
|
||||||
|
ORANGE = "#FF966C"
|
||||||
|
PURPLE = "#FCA7EA"
|
||||||
|
RED = "#FF757F"
|
||||||
|
RED1 = "#C53B53"
|
||||||
|
TEAL = "#4FD6BE"
|
||||||
|
TERMINAL_BLACK = "#444A73"
|
||||||
|
WARNING = "#FFC777"
|
||||||
|
YELLOW = "#FFC777"
|
||||||
|
|
||||||
|
|
||||||
|
class TokyoNightNight:
|
||||||
|
BG = "#1A1B26"
|
||||||
|
BG_DARK = "#16161E"
|
||||||
|
BG_FLOAT = "#16161E"
|
||||||
|
BG_HIGHLIGHT = "#292E42"
|
||||||
|
BG_POPUP = "#16161E"
|
||||||
|
BG_SEARCH = "#3D59A1"
|
||||||
|
BG_SIDEBAR = "#16161E"
|
||||||
|
BG_STATUSLINE = "#16161E"
|
||||||
|
BG_VISUAL = "#283457"
|
||||||
|
BLACK = "#15161E"
|
||||||
|
BLUE = "#7AA2F7"
|
||||||
|
BLUE0 = "#3D59A1"
|
||||||
|
BLUE1 = "#2AC3DE"
|
||||||
|
BLUE2 = "#0DB9D7"
|
||||||
|
BLUE5 = "#89DDFF"
|
||||||
|
BLUE6 = "#B4F9F8"
|
||||||
|
BLUE7 = "#394B70"
|
||||||
|
BORDER = "#15161E"
|
||||||
|
BORDER_HIGHLIGHT = "#27A1B9"
|
||||||
|
COMMENT = "#565F89"
|
||||||
|
CYAN = "#7DCFFF"
|
||||||
|
DARK3 = "#545C7E"
|
||||||
|
DARK5 = "#737AA2"
|
||||||
|
DELTA_ADD = "#2C5A66"
|
||||||
|
DELTA_DELETE = "#713137"
|
||||||
|
DIFF_ADD = "#20303B"
|
||||||
|
DIFF_CHANGE = "#1F2231"
|
||||||
|
DIFF_DELETE = "#37222C"
|
||||||
|
DIFF_TEXT = "#394B70"
|
||||||
|
ERROR = "#DB4B4B"
|
||||||
|
FG = "#C0CAF5"
|
||||||
|
FG_DARK = "#A9B1D6"
|
||||||
|
FG_FLOAT = "#C0CAF5"
|
||||||
|
FG_GUTTER = "#3B4261"
|
||||||
|
FG_SIDEBAR = "#A9B1D6"
|
||||||
|
GIT_ADD = "#449DAB"
|
||||||
|
GIT_CHANGE = "#6183BB"
|
||||||
|
GIT_DELETE = "#914C54"
|
||||||
|
GIT_IGNORE = "#545C7E"
|
||||||
|
GIT_SIGNS_ADD = "#266D6A"
|
||||||
|
GIT_SIGNS_CHANGE = "#536C9E"
|
||||||
|
GIT_SIGNS_DELETE = "#B2555B"
|
||||||
|
GREEN = "#9ECE6A"
|
||||||
|
GREEN1 = "#73DACA"
|
||||||
|
GREEN2 = "#41A6B5"
|
||||||
|
HINT = "#1ABC9C"
|
||||||
|
INFO = "#0DB9D7"
|
||||||
|
MAGENTA = "#BB9AF7"
|
||||||
|
MAGENTA2 = "#FF007C"
|
||||||
|
ORANGE = "#FF9E64"
|
||||||
|
PURPLE = "#9D7CD8"
|
||||||
|
RED = "#F7768E"
|
||||||
|
RED1 = "#DB4B4B"
|
||||||
|
TEAL = "#1ABC9C"
|
||||||
|
TERMINAL_BLACK = "#414868"
|
||||||
|
WARNING = "#E0AF68"
|
||||||
|
YELLOW = "#E0AF68"
|
||||||
|
|
||||||
|
|
||||||
|
class TokyoNightStorm:
|
||||||
|
BG = "#24283B"
|
||||||
|
BG_DARK = "#1F2335"
|
||||||
|
BG_FLOAT = "#1F2335"
|
||||||
|
BG_HIGHLIGHT = "#292E42"
|
||||||
|
BG_POPUP = "#1F2335"
|
||||||
|
BG_SEARCH = "#3D59A1"
|
||||||
|
BG_SIDEBAR = "#1F2335"
|
||||||
|
BG_STATUSLINE = "#1F2335"
|
||||||
|
BG_VISUAL = "#2E3C64"
|
||||||
|
BLACK = "#1D202F"
|
||||||
|
BLUE = "#7AA2F7"
|
||||||
|
BLUE0 = "#3D59A1"
|
||||||
|
BLUE1 = "#2AC3DE"
|
||||||
|
BLUE2 = "#0DB9D7"
|
||||||
|
BLUE5 = "#89DDFF"
|
||||||
|
BLUE6 = "#B4F9F8"
|
||||||
|
BLUE7 = "#394B70"
|
||||||
|
BORDER = "#1D202F"
|
||||||
|
BORDER_HIGHLIGHT = "#29A4BD"
|
||||||
|
COMMENT = "#565F89"
|
||||||
|
CYAN = "#7DCFFF"
|
||||||
|
DARK3 = "#545C7E"
|
||||||
|
DARK5 = "#737AA2"
|
||||||
|
DELTA_ADD = "#316172"
|
||||||
|
DELTA_DELETE = "#763842"
|
||||||
|
DIFF_ADD = "#283B4D"
|
||||||
|
DIFF_CHANGE = "#272D43"
|
||||||
|
DIFF_DELETE = "#3F2D3D"
|
||||||
|
DIFF_TEXT = "#394B70"
|
||||||
|
ERROR = "#DB4B4B"
|
||||||
|
FG = "#C0CAF5"
|
||||||
|
FG_DARK = "#A9B1D6"
|
||||||
|
FG_FLOAT = "#C0CAF5"
|
||||||
|
FG_GUTTER = "#3B4261"
|
||||||
|
FG_SIDEBAR = "#A9B1D6"
|
||||||
|
GIT_ADD = "#449DAB"
|
||||||
|
GIT_CHANGE = "#6183BB"
|
||||||
|
GIT_DELETE = "#914C54"
|
||||||
|
GIT_IGNORE = "#545C7E"
|
||||||
|
GIT_SIGNS_ADD = "#266D6A"
|
||||||
|
GIT_SIGNS_CHANGE = "#536C9E"
|
||||||
|
GIT_SIGNS_DELETE = "#B2555B"
|
||||||
|
GREEN = "#9ECE6A"
|
||||||
|
GREEN1 = "#73DACA"
|
||||||
|
GREEN2 = "#41A6B5"
|
||||||
|
HINT = "#1ABC9C"
|
||||||
|
INFO = "#0DB9D7"
|
||||||
|
MAGENTA = "#BB9AF7"
|
||||||
|
MAGENTA2 = "#FF007C"
|
||||||
|
ORANGE = "#FF9E64"
|
||||||
|
PURPLE = "#9D7CD8"
|
||||||
|
RED = "#F7768E"
|
||||||
|
RED1 = "#DB4B4B"
|
||||||
|
TEAL = "#1ABC9C"
|
||||||
|
TERMINAL_BLACK = "#414868"
|
||||||
|
WARNING = "#E0AF68"
|
||||||
|
YELLOW = "#E0AF68"
|
||||||
|
|
||||||
|
|
||||||
|
Color = TokyoNightNight
|
||||||
@ -238,4 +238,4 @@ class TokyoNightStorm:
|
|||||||
YELLOW = "#E0AF68"
|
YELLOW = "#E0AF68"
|
||||||
|
|
||||||
|
|
||||||
COLORS = TokyoNightNight
|
Colors = TokyoNightNight
|
||||||
|
|||||||
@ -4,8 +4,7 @@ import pygame
|
|||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
from .board import Board
|
from .board import Board
|
||||||
|
from .color import Color
|
||||||
from .colors import COLORS
|
|
||||||
from .config import Config
|
from .config import Config
|
||||||
from .logger import setup_logger
|
from .logger import setup_logger
|
||||||
from .utils import Direction
|
from .utils import Direction
|
||||||
@ -19,9 +18,7 @@ class Game:
|
|||||||
pygame.init()
|
pygame.init()
|
||||||
self.screen: pygame.Surface = pygame.display.set_mode((Config.WIDTH, Config.HEIGHT))
|
self.screen: pygame.Surface = pygame.display.set_mode((Config.WIDTH, Config.HEIGHT))
|
||||||
pygame.display.set_caption("2048")
|
pygame.display.set_caption("2048")
|
||||||
self.blocks = Board()
|
self.blocks = Board(self.screen)
|
||||||
self.blocks.generate_block(Config.INITIAL_BLOCK_COUNT)
|
|
||||||
# self.blocks.generate_block(2, (1, 1), (1, 3))
|
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
"""Run the game loop."""
|
"""Run the game loop."""
|
||||||
@ -36,7 +33,7 @@ class Game:
|
|||||||
|
|
||||||
def _render(self) -> None:
|
def _render(self) -> None:
|
||||||
"""Render the game."""
|
"""Render the game."""
|
||||||
self.screen.fill(COLORS.BG)
|
self.screen.fill(Color.BG)
|
||||||
self.blocks.draw(self.screen)
|
self.blocks.draw(self.screen)
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user