From bb4800eb333b51bc46db3cf15e1813853e07f840 Mon Sep 17 00:00:00 2001 From: andrzej Date: Tue, 7 Apr 2020 18:50:25 +0200 Subject: [PATCH] =?UTF-8?q?Demo=20poruszania=20si=C4=99=20agenta,=20popraw?= =?UTF-8?q?ka?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- agent.py | 21 ++++++++++++++++++++- attributes.py | 10 ++++++++++ main.py | 19 +++++-------------- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/agent.py b/agent.py index d57400a..1d529d7 100644 --- a/agent.py +++ b/agent.py @@ -11,6 +11,24 @@ class Agent: self.is_loaded = False + def demo_agent_move(self, demo_agent_step, demo_step_max=5): + demo_agent_sign = 0 + next_coords = self.get_next_move_coordinates() + can_move = self.check_if_can_move(next_coords) + if demo_agent_step >= demo_step_max: + demo_agent_sign = -1 + self.direction = 'up' + elif demo_agent_step == 0: + demo_agent_sign = 1 + self.direction = 'down' + if not can_move: + # alt_move_left = + # alt_move_right = + alternative_move = ('left' if self.check_if_can_move(self.move_left()) else None) or ('right' if self.check_if_can_move(self.move_right()) else self.direction) + self.direction = alternative_move + else: + self.x = next_coords.x + self.y = next_coords.y def move(self): next_coords = self.get_next_move_coordinates() can_move = self.check_if_can_move(next_coords) @@ -33,7 +51,8 @@ class Agent: # 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 + tile_on_map = 0 <= next_coords.x < self.assigned_warehouse.width and 0 <= next_coords.y < self.assigned_warehouse.height + return tile_passable and tile_on_map def move_right(self): pos_x = self.x + 1 diff --git a/attributes.py b/attributes.py index da6ba83..3b33a48 100644 --- a/attributes.py +++ b/attributes.py @@ -16,3 +16,13 @@ PACK_CATEGORIES = { 'general', 'freezed', } + +COLORS = { + 'white': (255, 255, 255), + 'black': (0, 0, 0), + 'gray': (128, 128, 128), + 'darkgray': (60, 60, 60), + 'yellow': (235, 235, 0), + 'lightgreen': (70, 238, 70), + 'red': (255, 0, 0) + } \ No newline at end of file diff --git a/main.py b/main.py index 75387aa..e6cbc34 100644 --- a/main.py +++ b/main.py @@ -3,18 +3,9 @@ import warehouse import agent import random import sys -from attributes import PackSize, PackStatus +from attributes import PackSize, PackStatus, COLORS WINDOW_SIZE = (600, 600) -COLORS = { - 'white': (255, 255, 255), - 'black': (0, 0, 0), - 'gray': (128, 128, 128), - 'darkgray': (60, 60, 60), - 'yellow': (235, 235, 0), - 'lightgreen': (70, 238, 70), - 'red': (255, 0, 0) - } COLOR_OF_FIELD = { 'Floor': 'gray', 'Rack': 'white', @@ -32,6 +23,7 @@ class MainGameFrame: 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 @@ -43,14 +35,13 @@ class MainGameFrame: self.draw_floor() self.draw_packages() self.draw_agent() - self.agent.move() + self.agent.demo_agent_move(demo_agent_step, 8) #linijka w celu zademonstrowania poruszania siÄ™ agenta + # self.agent.move() #oryginalna linijka demo_agent_step += (1*demo_agent_sign) - if demo_agent_step >= 10: + if demo_agent_step >= 8: 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)