From 3b761e41abf11dea791b630cce7d702db18129b9 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 10 Apr 2023 18:43:16 +0200 Subject: [PATCH] Added the cleaning ability to vacuum and container_filling status now is visible. --- domain/commands/vacuum_move_command.py | 4 ++++ domain/entities/vacuum.py | 3 +++ domain/world.py | 5 ++++- view/renderer.py | 10 +++++++++- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/domain/commands/vacuum_move_command.py b/domain/commands/vacuum_move_command.py index b868e33..6138e9b 100644 --- a/domain/commands/vacuum_move_command.py +++ b/domain/commands/vacuum_move_command.py @@ -30,5 +30,9 @@ class VacuumMoveCommand(Command): if self.world.is_obstacle_at(end_x, end_y): return + if self.world.is_garbage_at(end_x, end_y): + self.vacuum.increase_container_filling() + self.world.dust[end_x][end_y].pop() + self.vacuum.x = end_x self.vacuum.y = end_y diff --git a/domain/entities/vacuum.py b/domain/entities/vacuum.py index 03fa7d8..4a6e919 100644 --- a/domain/entities/vacuum.py +++ b/domain/entities/vacuum.py @@ -9,4 +9,7 @@ class Vacuum(Entity): self.cleaning_detergent = 100 self.container_filling = 0 + def increase_container_filling(self) -> None: + self.container_filling += 10 + # TODO VACUUM: add more properties diff --git a/domain/world.py b/domain/world.py index e6d10b5..7ea72a4 100644 --- a/domain/world.py +++ b/domain/world.py @@ -2,7 +2,7 @@ from domain.entities.entity import Entity class World: - def __init__(self, width: int, height: int): + def __init__(self, width: int, height: int) -> object: self.width = width self.height = height self.dust = [[[] for j in range(height)] for i in range(width)] @@ -24,3 +24,6 @@ class World: def is_obstacle_at(self, x: int, y: int) -> bool: return bool(self.obstacles[x][y]) + + def is_garbage_at(self, x: int, y: int) -> bool: + return bool(self.dust[x][y]) \ No newline at end of file diff --git a/view/renderer.py b/view/renderer.py index bfe1dde..5376a3d 100644 --- a/view/renderer.py +++ b/view/renderer.py @@ -31,6 +31,7 @@ class Renderer: pygame.display.set_caption("AI Vacuum Cleaner") self.screen = pygame.display.set_mode((self.width, self.height)) + self.font = pygame.font.SysFont('Arial', 26, bold=True) self.sprites = { "VACUUM": pygame.transform.scale(pygame.image.load("media/sprites/vacuum.png"), @@ -92,10 +93,17 @@ class Renderer: def draw_entity(self, entity: Entity): sprite = self.sprites.get(entity.type, None) draw_pos = (entity.x * self.tile_width, entity.y * self.tile_height) + if "PEEL" in entity.type: + draw_pos = ((entity.x - 0.1) * self.tile_width, (entity.y - 0.25) * self.tile_height) if "PLANT" in entity.type: draw_pos = ((entity.x - 0.1) * self.tile_width, (entity.y - 0.25) * self.tile_height) if "CAT" in entity.type and isinstance(entity, Cat): sprite = self.cat_direction_sprite[entity.direction] + if "VACUUM" in entity.type: + # Add text displaying container filling level + text_surface = self.font.render(f"Filling: {entity.container_filling}%", True, Color("black")) + text_pos = (draw_pos[0] + self.tile_width / 2 - text_surface.get_width() / 2, draw_pos[1] + self.tile_height) + self.screen.blit(text_surface, text_pos) self.screen.blit(sprite, draw_pos) def draw_sprite(self, x: int, y: int, sprite_name: str): @@ -110,4 +118,4 @@ class Renderer: self.draw_sprite(tile_x, tile_y, sprite) def render_floor(self): - self.fill_grid_with_sprite("TILE") + self.fill_grid_with_sprite("TILE") \ No newline at end of file