feat: draw Block on screen

This commit is contained in:
Kristofers Solo 2023-12-23 21:55:45 +02:00
parent d1490bd49e
commit a5a44cb5b5
4 changed files with 46 additions and 9 deletions

21
src/py2048/block.py Normal file
View File

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

View File

@ -236,3 +236,6 @@ class TokyoNightStorm:
TERMINAL_BLACK = "#414868"
WARNING = "#E0AF68"
YELLOW = "#E0AF68"
COLORS = TokyoNightNight

View File

@ -3,3 +3,4 @@ class Config:
HEIGHT = 800
FONT_FAMILY = "Roboto"
FONT_SIZE = 40
BLOCK_SIZE = 200

View File

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