Demo poruszania się agenta

This commit is contained in:
andrzej 2020-04-07 01:18:04 +02:00
parent a1a7d4faf4
commit 20eda4e31c
2 changed files with 56 additions and 13 deletions

View File

@ -1,28 +1,60 @@
from warehouse import Coordinates, Tile
class Agent: class Agent:
def __init__(self, start_x, start_y, assigned_warehouse, radius=5): def __init__(self, start_x, start_y, assigned_warehouse, radius=5):
self.x = start_x self.x = start_x
self.y = start_y self.y = start_y
self.direction = 'down'
self.radius = radius self.radius = radius
self.assigned_warehouse = assigned_warehouse self.assigned_warehouse = assigned_warehouse
self.is_loaded = False 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): def move_right(self):
self.x += 1 pos_x = self.x + 1
if self.x > self.assigned_warehouse.width - 1: if pos_x > self.assigned_warehouse.width - 1:
self.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): def move_left(self):
self.x -= 1 pos_x = self.x - 1
if self.x < 0: if pos_x < 0:
self.x = 0 pos_x = 0
return Coordinates(x=pos_x, y=self.y)
def move_down(self): def move_down(self):
self.y += 1 pos_y = self.y + 1
if self.y > self.assigned_warehouse.height - 1: if pos_y > self.assigned_warehouse.height - 1:
self.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): def move_up(self):
self.y -= 1 pos_y = self.y - 1
if self.y < 0: if pos_y < 0:
self.y = 0 pos_y = 0
return Coordinates(x=self.x, y=pos_y)

13
main.py
View File

@ -31,8 +31,10 @@ class MainGameFrame:
self.warehouse_map = warehouse.Warehouse(20, 20, 150, 20) self.warehouse_map = warehouse.Warehouse(20, 20, 150, 20)
starting_x, starting_y = self.set_starting_agent_position() starting_x, starting_y = self.set_starting_agent_position()
self.agent = agent.Agent(starting_x, starting_y, self.warehouse_map, agent_radius) self.agent = agent.Agent(starting_x, starting_y, self.warehouse_map, agent_radius)
self.clock = pygame.time.Clock()
def run(self): def run(self):
demo_agent_step = 1
demo_agent_sign = 1
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
@ -41,7 +43,16 @@ class MainGameFrame:
self.draw_floor() self.draw_floor()
self.draw_packages() self.draw_packages()
self.draw_agent() 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() pygame.display.update()
self.clock.tick(5)
def draw_floor(self): def draw_floor(self):
for x in range(self.warehouse_map.width): for x in range(self.warehouse_map.width):