Machine_learning_2023/main.py

91 lines
2.7 KiB
Python

from random import randint
import pygame
from domain.commands.random_cat_move_command import RandomCatMoveCommand
from domain.commands.vacuum_move_command import VacuumMoveCommand
from domain.entities.cat import Cat
from domain.entities.entity import Entity
from domain.entities.vacuum import Vacuum
from domain.world import World
from view.renderer import Renderer
# initial_draw(500, 10)
class Main:
def __init__(self):
tiles_x = 10
tiles_y = 10
self.renderer = Renderer(800, 800, tiles_x, tiles_y)
self.world = generate_world(tiles_x, tiles_y)
self.commands = []
self.clock = pygame.time.Clock()
self.running = True
self.fps = 60
def run(self):
while self.running:
self.process_input()
self.update()
self.renderer.render(self.world)
self.clock.tick(self.fps)
pygame.quit()
def process_input(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.commands.append(
VacuumMoveCommand(self.world, self.world.vacuum, (-1, 0))
)
if event.key == pygame.K_RIGHT:
self.commands.append(
VacuumMoveCommand(self.world, self.world.vacuum, (1, 0))
)
if event.key == pygame.K_UP:
self.commands.append(
VacuumMoveCommand(self.world, self.world.vacuum, (0, -1))
)
if event.key == pygame.K_DOWN:
self.commands.append(
VacuumMoveCommand(self.world, self.world.vacuum, (0, 1))
)
def update(self):
self.commands.append(RandomCatMoveCommand(self.world, self.world.cat))
for command in self.commands:
command.run()
self.commands.clear()
def generate_world(tiles_x: int, tiles_y: int) -> World:
world = World(tiles_x, tiles_y)
for _ in range(10):
temp_x = randint(0, tiles_x - 1)
temp_y = randint(0, tiles_y - 1)
world.add_entity(Entity(temp_x, temp_y, "PEEL"))
world.vacuum = Vacuum(1, 1)
world.cat = Cat(7, 8)
world.add_entity(world.cat)
world.add_entity(Entity(2, 8, "PLANT1"))
world.add_entity(Entity(4, 1, "PLANT1"))
world.add_entity(Entity(3, 4, "PLANT2"))
world.add_entity(Entity(8, 8, "PLANT2"))
world.add_entity(Entity(9, 3, "PLANT3"))
return world
if __name__ == "__main__":
app = Main()
app.run()