diff --git a/src/py2048/block.py b/src/py2048/block.py new file mode 100644 index 0000000..954033c --- /dev/null +++ b/src/py2048/block.py @@ -0,0 +1,21 @@ +import pygame + +from .colors import COLORS +from .config import Config + + +class Block(pygame.sprite.Sprite): + def __init__(self, x: int, y: int): + super().__init__() + self.image = pygame.Surface((Config.BLOCK_SIZE, Config.BLOCK_SIZE)) + self.image.fill(COLORS.ERROR) + self.rect = self.image.get_rect() + self.rect.topleft = (x, y) + self.value = 2 + self.draw_value() + + def draw_value(self) -> None: + font = pygame.font.SysFont(Config.FONT_FAMILY, Config.FONT_SIZE) + text = font.render(str(self.value), True, COLORS.FG) + text_rect = text.get_rect(center=self.image.get_rect().center) + self.image.blit(text, text_rect) diff --git a/src/py2048/colors.py b/src/py2048/colors.py index 2d94b6e..d7c95ec 100644 --- a/src/py2048/colors.py +++ b/src/py2048/colors.py @@ -236,3 +236,6 @@ class TokyoNightStorm: TERMINAL_BLACK = "#414868" WARNING = "#E0AF68" YELLOW = "#E0AF68" + + +COLORS = TokyoNightNight diff --git a/src/py2048/config.py b/src/py2048/config.py index d8e2972..acff2d9 100644 --- a/src/py2048/config.py +++ b/src/py2048/config.py @@ -3,3 +3,4 @@ class Config: HEIGHT = 800 FONT_FAMILY = "Roboto" FONT_SIZE = 40 + BLOCK_SIZE = 200 diff --git a/src/py2048/game.py b/src/py2048/game.py index d1a2cb9..892ee0c 100644 --- a/src/py2048/game.py +++ b/src/py2048/game.py @@ -2,25 +2,37 @@ import sys import pygame +from .block import Block + +from .colors import COLORS +from .config import Config + class Game: def __init__(self) -> None: pygame.init() - self.screen = pygame.display.set_mode((1200, 800)) + self.screen = pygame.display.set_mode((Config.WIDTH, Config.HEIGHT)) pygame.display.set_caption("2048") - self.bg_color = (230, 230, 230) + self.sprites = pygame.sprite.Group() + block = Block(0, 0) + self.sprites.add(block) def run(self) -> None: while True: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - sys.exit() - self.screen.fill(self.bg_color) - pygame.display.flip() + self.hande_events() + self.update() + self.render() def update(self) -> None: - pass + self.sprites.update() def render(self) -> None: - self.screen.fill((255, 255, 255)) + self.screen.fill(COLORS.BG) + self.sprites.draw(self.screen) pygame.display.flip() + + def hande_events(self) -> None: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + sys.exit()