From 06fa7c4cf725297e6d05a8a892b9027fd914b603 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Thu, 4 Jan 2024 00:57:56 +0200 Subject: [PATCH] fix(game): flickering --- src/game/game.py | 35 +++++++++++++++++++++++++++++++++++ src/game/main.py | 3 ++- src/game/preview.py | 1 + src/game/score.py | 3 ++- src/utils/config.py | 2 ++ 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/game/game.py b/src/game/game.py index 25b70a9..6956c42 100644 --- a/src/game/game.py +++ b/src/game/game.py @@ -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, + ) diff --git a/src/game/main.py b/src/game/main.py index 765a25f..cb9c304 100644 --- a/src/game/main.py +++ b/src/game/main.py @@ -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: diff --git a/src/game/preview.py b/src/game/preview.py index 0d5b9a6..7c05db5 100644 --- a/src/game/preview.py +++ b/src/game/preview.py @@ -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) diff --git a/src/game/score.py b/src/game/score.py index 3f92c9b..6f4ae53 100644 --- a/src/game/score.py +++ b/src/game/score.py @@ -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) diff --git a/src/utils/config.py b/src/utils/config.py index 22adf15..abf3ea3 100644 --- a/src/utils/config.py +++ b/src/utils/config.py @@ -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)