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