mirror of
https://github.com/kristoferssolo/2048.git
synced 2025-10-21 15:20:35 +00:00
feat: draw Block on screen
This commit is contained in:
parent
d1490bd49e
commit
a5a44cb5b5
21
src/py2048/block.py
Normal file
21
src/py2048/block.py
Normal 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)
|
||||||
@ -236,3 +236,6 @@ class TokyoNightStorm:
|
|||||||
TERMINAL_BLACK = "#414868"
|
TERMINAL_BLACK = "#414868"
|
||||||
WARNING = "#E0AF68"
|
WARNING = "#E0AF68"
|
||||||
YELLOW = "#E0AF68"
|
YELLOW = "#E0AF68"
|
||||||
|
|
||||||
|
|
||||||
|
COLORS = TokyoNightNight
|
||||||
|
|||||||
@ -3,3 +3,4 @@ class Config:
|
|||||||
HEIGHT = 800
|
HEIGHT = 800
|
||||||
FONT_FAMILY = "Roboto"
|
FONT_FAMILY = "Roboto"
|
||||||
FONT_SIZE = 40
|
FONT_SIZE = 40
|
||||||
|
BLOCK_SIZE = 200
|
||||||
|
|||||||
@ -2,25 +2,37 @@ import sys
|
|||||||
|
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
|
from .block import Block
|
||||||
|
|
||||||
|
from .colors import COLORS
|
||||||
|
from .config import Config
|
||||||
|
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
pygame.init()
|
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")
|
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:
|
def run(self) -> None:
|
||||||
while True:
|
while True:
|
||||||
for event in pygame.event.get():
|
self.hande_events()
|
||||||
if event.type == pygame.QUIT:
|
self.update()
|
||||||
sys.exit()
|
self.render()
|
||||||
self.screen.fill(self.bg_color)
|
|
||||||
pygame.display.flip()
|
|
||||||
|
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
pass
|
self.sprites.update()
|
||||||
|
|
||||||
def render(self) -> None:
|
def render(self) -> None:
|
||||||
self.screen.fill((255, 255, 255))
|
self.screen.fill(COLORS.BG)
|
||||||
|
self.sprites.draw(self.screen)
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
|
def hande_events(self) -> None:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
pygame.quit()
|
||||||
|
sys.exit()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user