From 38b1d3eb68ef6b676e0a74cbc04d63799c731bcf Mon Sep 17 00:00:00 2001 From: korzepadawid Date: Sun, 10 Apr 2022 18:38:57 +0200 Subject: [PATCH] smart logs --- logic/game.py | 11 +++++++---- ui/logs.py | 29 +++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/logic/game.py b/logic/game.py index 436d514..6ccc107 100644 --- a/logic/game.py +++ b/logic/game.py @@ -86,7 +86,7 @@ class Game: running = True grid = Grid(self.tiles) stats = Stats() - logs = Logs() + logs = Logs(self.screen) knights_sprite_group = pygame.sprite.Group() monsters_sprite_group = pygame.sprite.Group() @@ -117,8 +117,8 @@ class Game: castle_spawn.spawn() - #grid.put_on_tile(0, 0, knights_left[0]) - + # grid.put_on_tile(0, 0, knights_left[0]) + i = 0 # log testing while running: self.screen.blit(self.bg, (0, 0)) @@ -129,10 +129,13 @@ class Game: if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: running = False + if event.key == pygame.K_SPACE: # log testing + logs.enqueue_log(f'AI: Zmiana pozycji na (4, {i}).') + i = i + 1 grid.update(self.screen) stats.draw(self.screen) - logs.draw(self.screen) + logs.draw() knights_sprite_group.draw(self.screen) monsters_sprite_group.draw(self.screen) diff --git a/ui/logs.py b/ui/logs.py index a3455e9..e9cab0f 100644 --- a/ui/logs.py +++ b/ui/logs.py @@ -1,3 +1,5 @@ +from queue import Queue + import pygame from common.colors import FONT_DARK, ORANGE, WHITE @@ -6,20 +8,31 @@ from common.helpers import draw_text class Logs: - def __init__(self): - self.grid = [] + def __init__(self, screen): + self.log_queue = Queue(maxsize=7) + self.screen = screen - def draw(self, screen): + def draw(self): x = (GRID_CELL_PADDING + GRID_CELL_WIDTH) * COLUMNS + BORDER_WIDTH + 15 y = 470 # background - pygame.draw.rect(screen, WHITE, pygame.Rect(x, y, 340, 323), 0, BORDER_RADIUS) + pygame.draw.rect(self.screen, WHITE, pygame.Rect(x, y, 340, 323), 0, BORDER_RADIUS) # title - draw_text('LOGS', FONT_DARK, screen, x + 120, y + 10, 36) - pygame.draw.rect(screen, ORANGE, pygame.Rect(x, y + 65, 340, 3)) + draw_text('LOGS', FONT_DARK, self.screen, x + 120, y + 10, 36) + pygame.draw.rect(self.screen, ORANGE, pygame.Rect(x, y + 65, 340, 3)) # texts - draw_text('AI Blue: Zniszczyła fortecę (4, 8).', FONT_DARK, screen, x + 35, y + 90, 16) - draw_text('AI Red: Zniszczyła fortecę (12, 5).', FONT_DARK, screen, x + 35, y + 120, 16) + next_y = y + 90 + i = 0 + start = len(self.log_queue.queue) - 1 + for idx in range(start, -1, -1): + draw_text(self.log_queue.queue[idx], FONT_DARK, self.screen, x + 35, next_y + i * 30, 16) + i = i + 1 + + def enqueue_log(self, text): + if self.log_queue.full(): + self.log_queue.get() + self.log_queue.put(text) + self.draw()