feat: logs queue

This commit is contained in:
korzepadawid 2022-04-12 20:44:27 +02:00
parent 04f17e3293
commit af2f61984a
3 changed files with 32 additions and 13 deletions

View File

@ -19,12 +19,13 @@ class Game:
pygame.display.set_icon(pygame.image.load('./resources/icons/sword.png')) pygame.display.set_icon(pygame.image.load('./resources/icons/sword.png'))
self.screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) self.screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
self.logs = Logs(self.screen)
self.clock = pygame.time.Clock() self.clock = pygame.time.Clock()
self.bg = pygame.image.load("./resources/textures/bg.jpg") self.bg = pygame.image.load("./resources/textures/bg.jpg")
self.screens = {'credits': Credits(self.screen, self.clock), 'options': Options(self.screen, self.clock)} self.screens = {'credits': Credits(self.screen, self.clock), 'options': Options(self.screen, self.clock)}
self.level = Level(self.screen) self.level = Level(self.screen, self.logs)
def main_menu(self): def main_menu(self):
menu = MainMenu(self.screen, self.clock, self.bg, menu = MainMenu(self.screen, self.clock, self.bg,
@ -35,7 +36,6 @@ class Game:
def game(self): def game(self):
stats = Stats() stats = Stats()
logs = Logs()
# setup clock for rounds # setup clock for rounds
NEXT_TURN = pygame.USEREVENT + 1 NEXT_TURN = pygame.USEREVENT + 1
@ -62,7 +62,7 @@ class Game:
self.level.handle_turn() self.level.handle_turn()
stats.draw(self.screen) stats.draw(self.screen)
logs.draw(self.screen) self.logs.draw()
self.level.update() self.level.update()

View File

@ -14,9 +14,9 @@ from models.tile import Tile
class Level: class Level:
def __init__(self, screen): def __init__(self, screen, logs):
self.screen = screen self.screen = screen
self.logs = logs
# sprite group setup # sprite group setup
self.sprites = pygame.sprite.Group() self.sprites = pygame.sprite.Group()
@ -119,8 +119,10 @@ class Level:
next_action = action_list.pop(0) next_action = action_list.pop(0)
if next_action == TURN_LEFT: if next_action == TURN_LEFT:
self.logs.enqueue_log(f'AI {current_knight.team}: Obrót w lewo.')
current_knight.rotate_left() current_knight.rotate_left()
elif next_action == TURN_RIGHT: elif next_action == TURN_RIGHT:
self.logs.enqueue_log(f'AI {current_knight.team}: Obrót w prawo.')
current_knight.rotate_right() current_knight.rotate_right()
elif next_action == FORWARD: elif next_action == FORWARD:
current_knight.step_forward() current_knight.step_forward()
@ -128,12 +130,16 @@ class Level:
# update knight on map # update knight on map
if current_knight.direction.name == UP: if current_knight.direction.name == UP:
self.logs.enqueue_log(f'AI {current_knight.team}: Ruch do góry.')
self.map[knight_pos_y - 1][knight_pos_x] = current_knight.team_alias() self.map[knight_pos_y - 1][knight_pos_x] = current_knight.team_alias()
elif current_knight.direction.name == RIGHT: elif current_knight.direction.name == RIGHT:
self.logs.enqueue_log(f'AI {current_knight.team}: Ruch w prawo.')
self.map[knight_pos_y][knight_pos_x + 1] = current_knight.team_alias() self.map[knight_pos_y][knight_pos_x + 1] = current_knight.team_alias()
elif current_knight.direction.name == DOWN: elif current_knight.direction.name == DOWN:
self.logs.enqueue_log(f'AI {current_knight.team}: Ruch w dół.')
self.map[knight_pos_y + 1][knight_pos_x] = current_knight.team_alias() self.map[knight_pos_y + 1][knight_pos_x] = current_knight.team_alias()
elif current_knight.direction.name == LEFT: elif current_knight.direction.name == LEFT:
self.logs.enqueue_log(f'AI {current_knight.team}: Ruch w lewo.')
self.map[knight_pos_y][knight_pos_x - 1] = current_knight.team_alias() self.map[knight_pos_y][knight_pos_x - 1] = current_knight.team_alias()
def update(self): def update(self):

View File

@ -1,3 +1,5 @@
from queue import Queue
import pygame import pygame
from common.colors import FONT_DARK, ORANGE, WHITE from common.colors import FONT_DARK, ORANGE, WHITE
@ -6,20 +8,31 @@ from common.helpers import draw_text
class Logs: class Logs:
def __init__(self): def __init__(self, screen):
self.grid = [] self.log_queue = Queue(maxsize=7)
self.screen = screen
def draw(self, screen): def draw(self):
x = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH + 15 x = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH + 15
y = 470 y = 470
# background # 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 # title
draw_text('LOGS', FONT_DARK, screen, x + 120, y + 10, 36) draw_text('LOGS', FONT_DARK, self.screen, x + 120, y + 10, 36)
pygame.draw.rect(screen, ORANGE, pygame.Rect(x, y + 65, 340, 3)) pygame.draw.rect(self.screen, ORANGE, pygame.Rect(x, y + 65, 340, 3))
# texts # texts
draw_text('AI Blue: Zniszczyła fortecę (4, 8).', FONT_DARK, screen, x + 35, y + 90, 16) next_y = y + 90
draw_text('AI Red: Zniszczyła fortecę (12, 5).', FONT_DARK, screen, x + 35, y + 120, 16) 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()