diff --git a/agent.py b/agent.py index 6271134..087f8cd 100644 --- a/agent.py +++ b/agent.py @@ -103,10 +103,36 @@ class Agent: return else: next = self.path.pop() + star_dir = self.direction print(next.x, next.y) - self.x = next.x - self.y = next.y + if self.x > next.x and not self.direction == 'left': + if self.direction == 'down': + self.turn_right() + else: + self.turn_left() + elif self.x < next.x and not self.direction == 'right': + if self.direction == 'down': + self.turn_left() + else: + self.turn_right() + elif self.y > next.y and not self.direction == 'up': + if self.direction == 'left': + self.turn_right() + else: + self.turn_left() + elif self.y < next.y and not self.direction == 'down': + if self.direction == 'right': + self.turn_right() + else: + self.turn_left() + + if star_dir == self.direction: + self.x = next.x + self.y = next.y + else: + self.path.append(next) self.closed = [] + def check_if_can_move(self, next_coords: Coordinates): tile_on_map = 0 <= next_coords.x < self.warehouse.width and 0 <= next_coords.y < self.warehouse.height if not tile_on_map: diff --git a/attributes.py b/attributes.py index f0f938c..204b476 100644 --- a/attributes.py +++ b/attributes.py @@ -41,4 +41,11 @@ TURN_RIGHT_DIRECTIONS = { "up": "right", "right": "down", "down": "left" -} \ No newline at end of file +} + +DIRECTION_ANGLES = { + "left": 0, + "up": -90, + "right": 180, + "down": 90 +} diff --git a/forklift.png b/forklift.png new file mode 100755 index 0000000..7450bc5 Binary files /dev/null and b/forklift.png differ diff --git a/main.py b/main.py index 5d43c83..ee832a2 100644 --- a/main.py +++ b/main.py @@ -3,23 +3,24 @@ import warehouse import agent import random import sys -from attributes import PackSize, PackStatus, COLORS +from attributes import PackSize, PackStatus, COLORS, DIRECTION_ANGLES -WINDOW_SIZE = (600, 600) +WINDOW_SIZE = (640, 640) COLOR_OF_FIELD = { 'Floor': 'gray', 'Rack': 'white', 'Pack': 'yellow', 'path': 'orange' } -TILE_WIDTH = 30 -TILE_HEIGHT = 30 +TILE_WIDTH = 32 +TILE_HEIGHT = 32 CIRCLE_CENTER_X, CIRCLE_CENTER_Y = int(TILE_WIDTH/2), int(TILE_HEIGHT/2) class MainGameFrame: def __init__(self): self.display = pygame.display.set_mode(WINDOW_SIZE) agent_radius = int(TILE_WIDTH/2) + self.agent_tex = pygame.image.load('forklift.png') 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) @@ -78,9 +79,8 @@ class MainGameFrame: ((pack_x * TILE_WIDTH) + 3, (pack_y * TILE_HEIGHT) + 3, TILE_WIDTH - 5, TILE_HEIGHT - 5)) def draw_agent(self): - agent_position_x, agent_position_y = self.agent.x, self.agent.y - agent_screen_position = ((agent_position_x*TILE_WIDTH) + CIRCLE_CENTER_X, (agent_position_y*TILE_HEIGHT) + CIRCLE_CENTER_Y) - pygame.draw.circle(self.display, COLORS['black'], agent_screen_position, self.agent.radius, int(self.agent.radius/2)) + rotated = pygame.transform.rotate(self.agent_tex, DIRECTION_ANGLES.get(self.agent.direction)) + self.display.blit(rotated, (self.agent.x*TILE_WIDTH, self.agent.y*TILE_WIDTH)) def set_starting_agent_position(self): starting_x, starting_y = random.randrange(self.warehouse_map.width), random.randrange(self.warehouse_map.height)