Config for cat

This commit is contained in:
Mateusz Dokowicz 2023-04-20 00:15:00 +02:00
parent e972dcfd83
commit f5003e93fe
4 changed files with 109 additions and 46 deletions

3
config.ini Normal file
View File

@ -0,0 +1,3 @@
[APP]
cat = False
movment = human #(human, movment)

View File

@ -1,6 +1,7 @@
from random import randint from random import randint
import pygame import pygame
import configparser
from domain.commands.random_cat_move_command import RandomCatMoveCommand from domain.commands.random_cat_move_command import RandomCatMoveCommand
from domain.commands.vacuum_move_command import VacuumMoveCommand from domain.commands.vacuum_move_command import VacuumMoveCommand
@ -12,7 +13,8 @@ from domain.world import World
from view.renderer import Renderer from view.renderer import Renderer
# initial_draw(500, 10) config = configparser.ConfigParser()
config.read("config.ini")
class Main: class Main:
@ -62,6 +64,7 @@ class Main:
) )
def update(self): def update(self):
if config.getboolean("APP", "cat"):
self.commands.append(RandomCatMoveCommand(self.world, self.world.cat)) self.commands.append(RandomCatMoveCommand(self.world, self.world.cat))
for command in self.commands: for command in self.commands:
command.run() command.run()
@ -76,6 +79,8 @@ def generate_world(tiles_x: int, tiles_y: int) -> World:
world.add_entity(Entity(temp_x, temp_y, "PEEL")) world.add_entity(Entity(temp_x, temp_y, "PEEL"))
world.vacuum = Vacuum(1, 1) world.vacuum = Vacuum(1, 1)
world.doc_station = Doc_Station(9, 8) world.doc_station = Doc_Station(9, 8)
print(config.getboolean("APP", "cat"))
if config.getboolean("APP", "cat"):
world.cat = Cat(7, 8) world.cat = Cat(7, 8)
world.add_entity(world.cat) world.add_entity(world.cat)
world.add_entity(Entity(2, 8, "PLANT1")) world.add_entity(Entity(2, 8, "PLANT1"))

View File

@ -1,2 +1,3 @@
pygame pygame
configparser
formaFormatting: Provider - black formaFormatting: Provider - black

View File

@ -2,15 +2,18 @@ import random
from random import randint from random import randint
import pygame import pygame
import configparser
from pygame import Color from pygame import Color
from domain.entities.cat import Cat from domain.entities.cat import Cat
from domain.entities.entity import Entity from domain.entities.entity import Entity
from domain.world import World from domain.world import World
config = configparser.ConfigParser()
config.read("config.ini")
class Renderer: class Renderer:
def __init__( def __init__(
self, self,
width=800, width=800,
@ -31,33 +34,66 @@ class Renderer:
pygame.display.set_caption("AI Vacuum Cleaner") pygame.display.set_caption("AI Vacuum Cleaner")
self.screen = pygame.display.set_mode((self.width, self.height)) self.screen = pygame.display.set_mode((self.width, self.height))
self.font = pygame.font.SysFont('Arial', 26, bold=True) self.font = pygame.font.SysFont("Arial", 26, bold=True)
self.sprites = { self.sprites = {
"VACUUM": pygame.transform.scale(pygame.image.load("media/sprites/vacuum.png"), "VACUUM": pygame.transform.scale(
(self.tile_width, self.tile_height)), pygame.image.load("media/sprites/vacuum.png"),
"DOC_STATION": pygame.transform.scale(pygame.image.load("media/sprites/docking_station.png"), (self.tile_width, self.tile_height),
(self.tile_width, self.tile_height)), ),
"WALL": pygame.transform.scale(pygame.image.load("media/sprites/wall.png"), "DOC_STATION": pygame.transform.scale(
(self.tile_width, self.tile_height)), pygame.image.load("media/sprites/docking_station.png"),
"TILE": pygame.transform.scale(pygame.image.load("media/sprites/tile_cropped.jpeg"), (self.tile_width, self.tile_height),
(self.tile_width, self.tile_height)), ),
"PEEL": pygame.transform.scale(pygame.image.load("media/sprites/peel.webp"), "WALL": pygame.transform.scale(
(self.tile_width, self.tile_height)), pygame.image.load("media/sprites/wall.png"),
"CAT_FRONT": pygame.transform.scale(pygame.image.load("media/sprites/cat/standing_front.png"), (self.tile_width, self.tile_height),
(self.tile_width, self.tile_height)), ),
"CAT_BACK": pygame.transform.scale(pygame.image.load("media/sprites/cat/standing_back.png"), "TILE": pygame.transform.scale(
(self.tile_width, self.tile_height)), pygame.image.load("media/sprites/tile_cropped.jpeg"),
"CAT_LEFT": pygame.transform.scale(pygame.image.load("media/sprites/cat/standing_left.png"), (self.tile_width, self.tile_height),
(self.tile_width, self.tile_height)), ),
"CAT_RIGHT": pygame.transform.scale(pygame.image.load("media/sprites/cat/standing_right.png"), "PEEL": pygame.transform.scale(
(self.tile_width, self.tile_height)), pygame.image.load("media/sprites/peel.webp"),
"PLANT1": pygame.transform.scale(pygame.image.load("media/sprites/plants/plant1.png"), (self.tile_width, self.tile_height),
(self.tile_width + self.tile_width / 4, self.tile_height + self.tile_height / 4)), ),
"PLANT2": pygame.transform.scale(pygame.image.load("media/sprites/plants/plant2.png"), "CAT_FRONT": pygame.transform.scale(
(self.tile_width + self.tile_width / 4, self.tile_height + self.tile_height / 4)), pygame.image.load("media/sprites/cat/standing_front.png"),
"PLANT3": pygame.transform.scale(pygame.image.load("media/sprites/plants/plant3.png"), (self.tile_width, self.tile_height),
(self.tile_width + self.tile_width / 4, self.tile_height + self.tile_height / 4)), ),
"CAT_BACK": pygame.transform.scale(
pygame.image.load("media/sprites/cat/standing_back.png"),
(self.tile_width, self.tile_height),
),
"CAT_LEFT": pygame.transform.scale(
pygame.image.load("media/sprites/cat/standing_left.png"),
(self.tile_width, self.tile_height),
),
"CAT_RIGHT": pygame.transform.scale(
pygame.image.load("media/sprites/cat/standing_right.png"),
(self.tile_width, self.tile_height),
),
"PLANT1": pygame.transform.scale(
pygame.image.load("media/sprites/plants/plant1.png"),
(
self.tile_width + self.tile_width / 4,
self.tile_height + self.tile_height / 4,
),
),
"PLANT2": pygame.transform.scale(
pygame.image.load("media/sprites/plants/plant2.png"),
(
self.tile_width + self.tile_width / 4,
self.tile_height + self.tile_height / 4,
),
),
"PLANT3": pygame.transform.scale(
pygame.image.load("media/sprites/plants/plant3.png"),
(
self.tile_width + self.tile_width / 4,
self.tile_height + self.tile_height / 4,
),
),
} }
self.cat_direction_sprite = { self.cat_direction_sprite = {
@ -80,6 +116,7 @@ class Renderer:
self.draw_entity(entity) self.draw_entity(entity)
self.draw_entity(world.vacuum) self.draw_entity(world.vacuum)
self.draw_entity(world.doc_station) self.draw_entity(world.doc_station)
if config.getboolean("APP", "cat"):
self.draw_entity(world.cat) self.draw_entity(world.cat)
pygame.display.update() pygame.display.update()
@ -88,33 +125,50 @@ class Renderer:
def render_board(self, color=Color("black")): def render_board(self, color=Color("black")):
for i in range(1, self.tiles_x): for i in range(1, self.tiles_x):
self.line(self.tile_width * i, 0, self.tile_width * i, self.height, color=color) self.line(
self.tile_width * i, 0, self.tile_width * i, self.height, color=color
)
for i in range(1, self.tiles_y): for i in range(1, self.tiles_y):
self.line(0, self.tile_height * i, self.width, self.tile_height * i, color=color) self.line(
0, self.tile_height * i, self.width, self.tile_height * i, color=color
)
def draw_entity(self, entity: Entity): def draw_entity(self, entity: Entity):
sprite = self.sprites.get(entity.type, None) sprite = self.sprites.get(entity.type, None)
draw_pos = (entity.x * self.tile_width, entity.y * self.tile_height) draw_pos = (entity.x * self.tile_width, entity.y * self.tile_height)
if "PEEL" in entity.type: if "PEEL" in entity.type:
draw_pos = ((entity.x - 0.1) * self.tile_width, (entity.y - 0.25) * self.tile_height) draw_pos = (
(entity.x - 0.1) * self.tile_width,
(entity.y - 0.25) * self.tile_height,
)
if "PLANT" in entity.type: if "PLANT" in entity.type:
draw_pos = ((entity.x - 0.1) * self.tile_width, (entity.y - 0.25) * self.tile_height) 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): if "CAT" in entity.type and isinstance(entity, Cat):
sprite = self.cat_direction_sprite[entity.direction] sprite = self.cat_direction_sprite[entity.direction]
if "VACUUM" in entity.type: if "VACUUM" in entity.type:
# Add text displaying container filling level # Add text displaying container filling level
text_surface = self.font.render(f"Filling: {entity.container_filling}%", True, Color("black")) text_surface = self.font.render(
text_pos = (draw_pos[0] + self.tile_width / 2 - text_surface.get_width() / 2, draw_pos[1] + self.tile_height) 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(text_surface, text_pos)
if "DOC_STATION" in entity.type: if "DOC_STATION" in entity.type:
draw_pos = ((entity.x - 0.1) * self.tile_width, (entity.y - 0.25) * self.tile_height) draw_pos = (
(entity.x - 0.1) * self.tile_width,
(entity.y - 0.25) * self.tile_height,
)
self.screen.blit(sprite, draw_pos) self.screen.blit(sprite, draw_pos)
def draw_sprite(self, x: int, y: int, sprite_name: str): def draw_sprite(self, x: int, y: int, sprite_name: str):
self.screen.blit( self.screen.blit(
self.sprites[sprite_name], self.sprites[sprite_name], (x * self.tile_width, y * self.tile_height)
(x * self.tile_width, y * self.tile_height)
) )
def fill_grid_with_sprite(self, sprite): def fill_grid_with_sprite(self, sprite):