Compare commits
3 Commits
83ef0fecad
...
f7b97c5f74
Author | SHA1 | Date | |
---|---|---|---|
|
f7b97c5f74 | ||
|
291e028a8a | ||
|
047ef87bab |
@ -1,4 +1,3 @@
|
||||
class Command:
|
||||
def run(self):
|
||||
raise NotImplementedError()
|
||||
|
@ -9,7 +9,6 @@ from domain.world import World
|
||||
|
||||
|
||||
class RandomCatMoveCommand(Command):
|
||||
|
||||
def __init__(self, world: World, cat: Cat) -> None:
|
||||
super().__init__()
|
||||
self.world = world
|
||||
@ -24,10 +23,12 @@ class RandomCatMoveCommand(Command):
|
||||
if not cat.busy:
|
||||
while True:
|
||||
cat.direction = randint(0, 3)
|
||||
if not ((cat.direction == 0 and cat.y == 0)
|
||||
if not (
|
||||
(cat.direction == 0 and cat.y == 0)
|
||||
or (cat.direction == 1 and cat.x == self.world.width - 1)
|
||||
or (cat.direction == 2 and cat.y == self.world.height - 1)
|
||||
or (cat.direction == 3 and cat.x == 0)):
|
||||
or (cat.direction == 3 and cat.x == 0)
|
||||
):
|
||||
break
|
||||
|
||||
if cat.direction == 0: # up
|
||||
@ -54,10 +55,16 @@ class RandomCatMoveCommand(Command):
|
||||
end_x = cat.x + move_vector[0]
|
||||
end_y = cat.y + move_vector[1]
|
||||
|
||||
if end_x > self.world.width - 1 or end_y > self.world.height - 1 or end_x < 0 or end_y < 0:
|
||||
if (
|
||||
end_x > self.world.width - 1
|
||||
or end_y > self.world.height - 1
|
||||
or end_x < 0
|
||||
or end_y < 0
|
||||
):
|
||||
return
|
||||
|
||||
self.world.obstacles[cat.x][cat.y].remove(cat)
|
||||
cat.x = end_x
|
||||
cat.y = end_y
|
||||
self.world.obstacles[end_x][end_y].append(cat)
|
||||
# endregion cat random movement
|
||||
|
@ -6,8 +6,9 @@ from domain.world import World
|
||||
|
||||
|
||||
class VacuumMoveCommand(Command):
|
||||
|
||||
def __init__(self, world: World, vacuum: Vacuum, move_vector: Tuple[int, int]) -> None:
|
||||
def __init__(
|
||||
self, world: World, vacuum: Vacuum, move_vector: Tuple[int, int]
|
||||
) -> None:
|
||||
super().__init__()
|
||||
self.world = world
|
||||
self.vacuum = vacuum
|
||||
@ -18,7 +19,12 @@ class VacuumMoveCommand(Command):
|
||||
end_x = self.vacuum.x + self.dx
|
||||
end_y = self.vacuum.y + self.dy
|
||||
|
||||
if end_x > self.world.width - 1 or end_y > self.world.height - 1 or end_x < 0 or end_y < 0:
|
||||
if (
|
||||
end_x > self.world.width - 1
|
||||
or end_y > self.world.height - 1
|
||||
or end_x < 0
|
||||
or end_y < 0
|
||||
):
|
||||
return
|
||||
|
||||
if self.world.is_obstacle_at(end_x, end_y):
|
||||
|
@ -4,6 +4,6 @@ from domain.world import World
|
||||
|
||||
class Vacuum(Entity):
|
||||
def __init__(self, x: int, y: int):
|
||||
super().__init__(x, y, 'VACUUM')
|
||||
super().__init__(x, y, "VACUUM")
|
||||
self.battery = 100
|
||||
# TODO add more properties
|
||||
|
@ -5,12 +5,8 @@ class World:
|
||||
def __init__(self, width: int, height: int):
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.dust = [
|
||||
[[] for j in range(height)] for i in range(width)
|
||||
]
|
||||
self.obstacles = [
|
||||
[[] for j in range(height)] for i in range(width)
|
||||
]
|
||||
self.dust = [[[] for j in range(height)] for i in range(width)]
|
||||
self.obstacles = [[[] for j in range(height)] for i in range(width)]
|
||||
|
||||
self.vacuum = None
|
||||
self.cat = None
|
||||
|
18
main.py
18
main.py
@ -2,7 +2,6 @@ from random import randint
|
||||
|
||||
import pygame
|
||||
|
||||
from Interface.vacuum_render import initial_draw
|
||||
from domain.commands.random_cat_move_command import RandomCatMoveCommand
|
||||
from domain.commands.vacuum_move_command import VacuumMoveCommand
|
||||
from domain.entities.cat import Cat
|
||||
@ -14,6 +13,7 @@ from view.renderer import Renderer
|
||||
|
||||
# initial_draw(500, 10)
|
||||
|
||||
|
||||
class Main:
|
||||
def __init__(self):
|
||||
tiles_x = 10
|
||||
@ -44,13 +44,21 @@ class Main:
|
||||
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)))
|
||||
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)))
|
||||
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)))
|
||||
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)))
|
||||
self.commands.append(
|
||||
VacuumMoveCommand(self.world, self.world.vacuum, (0, 1))
|
||||
)
|
||||
|
||||
def update(self):
|
||||
self.commands.append(RandomCatMoveCommand(self.world, self.world.cat))
|
||||
|
Loading…
Reference in New Issue
Block a user