fix(game): flickering

This commit is contained in:
Kristofers Solo 2024-01-04 00:57:56 +02:00
parent 25e56a3482
commit 06fa7c4cf7
5 changed files with 42 additions and 2 deletions

View File

@ -6,6 +6,41 @@ class Game:
def __init__(self) -> None:
self.surface = pygame.Surface(CONFIG.game.size)
self.dispaly_surface = pygame.display.get_surface()
self.rect = self.surface.get_rect(topleft=CONFIG.game.pos)
self.surface.fill(CONFIG.colors.bg_float)
def run(self) -> None:
self.dispaly_surface.blit(self.surface, CONFIG.game.pos)
def draw(self) -> None:
self._draw_grid()
self._draw_border()
def _draw_grid(self) -> None:
for col in range(1, CONFIG.game.columns):
x = col * CONFIG.game.cell_size
pygame.draw.line(
self.surface,
CONFIG.colors.border_highlight,
(x, 0),
(x, self.surface.get_height()),
CONFIG.game.line_width,
)
for row in range(1, CONFIG.game.rows):
y = row * CONFIG.game.cell_size
pygame.draw.line(
self.surface,
CONFIG.colors.border_highlight,
(0, y),
(self.surface.get_width(), y),
CONFIG.game.line_width,
)
def _draw_border(self) -> None:
pygame.draw.rect(
self.dispaly_surface,
CONFIG.colors.border_highlight,
self.rect,
CONFIG.game.line_width,
CONFIG.game.border_radius,
)

View File

@ -13,6 +13,7 @@ class Main:
pygame.init()
pygame.display.set_caption(CONFIG.window.title)
self.display_surface = pygame.display.set_mode(CONFIG.window.size)
self.display_surface.fill(CONFIG.colors.bg)
self.clock = pygame.time.Clock()
self.game = Game()
@ -20,8 +21,8 @@ class Main:
self.preview = Preview()
def draw(self) -> None:
self.display_surface.fill(CONFIG.colors.bg)
pygame.display.update()
self.game.draw()
def run(self) -> None:
while True:

View File

@ -12,6 +12,7 @@ class Preview:
)
)
self.dispaly_surface = pygame.display.get_surface()
self.surface.fill(CONFIG.colors.bg_sidebar)
def run(self) -> None:
self.dispaly_surface.blit(self.surface, self.rect)

View File

@ -5,10 +5,11 @@ from utils import CONFIG, Size
class Score:
def __init__(self) -> None:
self.surface = pygame.Surface(CONFIG.sidebar.score)
self.dispaly_surface = pygame.display.get_surface()
self.rect = self.surface.get_rect(
bottomright=CONFIG.window.size.sub(CONFIG.window.padding)
)
self.dispaly_surface = pygame.display.get_surface()
self.surface.fill(CONFIG.colors.bg_sidebar)
def run(self) -> None:
self.dispaly_surface.blit(self.surface, self.rect)

View File

@ -41,6 +41,8 @@ class Position(NamedTuple):
class Game:
columns: int = 10
rows: int = 20
line_width: int = 1
border_radius: int = 2
padding: int = PADDING
cell_size: int = 40
size: Size = Size(columns * cell_size, rows * cell_size)