From 20eda4e31c51a0bb1dd05d989edb5494f78c3585 Mon Sep 17 00:00:00 2001 From: andrzej Date: Tue, 7 Apr 2020 01:18:04 +0200 Subject: [PATCH] =?UTF-8?q?Demo=20poruszania=20si=C4=99=20agenta?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- agent.py | 56 ++++++++++++++++++++++++++++++++++++++++++++------------ main.py | 13 ++++++++++++- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/agent.py b/agent.py index 83d7fcd..d57400a 100644 --- a/agent.py +++ b/agent.py @@ -1,28 +1,60 @@ +from warehouse import Coordinates, Tile + class Agent: def __init__(self, start_x, start_y, assigned_warehouse, radius=5): self.x = start_x self.y = start_y + self.direction = 'down' self.radius = radius self.assigned_warehouse = assigned_warehouse self.is_loaded = False + + def move(self): + next_coords = self.get_next_move_coordinates() + can_move = self.check_if_can_move(next_coords) + if can_move: + self.x = next_coords.x + self.y = next_coords.y + + def get_next_move_coordinates(self): + direction_moves = { + 'up': self.move_up(), + 'down': self.move_down(), + 'left': self.move_left(), + 'right': self.move_right() + } + next_coords = direction_moves.get(self.direction, Coordinates(self.x, self.y)) + return next_coords + + def check_if_can_move(self, next_coords: Coordinates): + # import pdb + # pdb.set_trace() + next_tile = self.assigned_warehouse.tiles[next_coords.x][next_coords.y] + tile_passable = isinstance(next_tile, Tile) and next_tile.category.passable + return tile_passable + def move_right(self): - self.x += 1 - if self.x > self.assigned_warehouse.width - 1: - self.x = self.assigned_warehouse.width - 1 + pos_x = self.x + 1 + if pos_x > self.assigned_warehouse.width - 1: + pos_x = self.assigned_warehouse.width - 1 + return Coordinates(x=pos_x, y=self.y) def move_left(self): - self.x -= 1 - if self.x < 0: - self.x = 0 + pos_x = self.x - 1 + if pos_x < 0: + pos_x = 0 + return Coordinates(x=pos_x, y=self.y) def move_down(self): - self.y += 1 - if self.y > self.assigned_warehouse.height - 1: - self.y = self.assigned_warehouse.height - 1 + pos_y = self.y + 1 + if pos_y > self.assigned_warehouse.height - 1: + pos_y = self.assigned_warehouse.height - 1 + return Coordinates(x=self.x, y=pos_y) def move_up(self): - self.y -= 1 - if self.y < 0: - self.y = 0 + pos_y = self.y - 1 + if pos_y < 0: + pos_y = 0 + return Coordinates(x=self.x, y=pos_y) \ No newline at end of file diff --git a/main.py b/main.py index 4ddf3bc..75387aa 100644 --- a/main.py +++ b/main.py @@ -31,8 +31,10 @@ class MainGameFrame: self.warehouse_map = warehouse.Warehouse(20, 20, 150, 20) starting_x, starting_y = self.set_starting_agent_position() self.agent = agent.Agent(starting_x, starting_y, self.warehouse_map, agent_radius) - + self.clock = pygame.time.Clock() def run(self): + demo_agent_step = 1 + demo_agent_sign = 1 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: @@ -41,7 +43,16 @@ class MainGameFrame: self.draw_floor() self.draw_packages() self.draw_agent() + self.agent.move() + demo_agent_step += (1*demo_agent_sign) + if demo_agent_step >= 10: + demo_agent_sign = -1 + self.agent.direction = 'up' + if demo_agent_step == 0: + demo_agent_sign = 1 + self.agent.direction = 'down' pygame.display.update() + self.clock.tick(5) def draw_floor(self): for x in range(self.warehouse_map.width):