2020-04-02 15:25:15 +02:00
|
|
|
import pygame
|
|
|
|
import warehouse
|
2020-04-04 22:03:15 +02:00
|
|
|
import agent
|
|
|
|
import random
|
2020-04-06 15:18:00 +02:00
|
|
|
import sys
|
2020-04-07 18:50:25 +02:00
|
|
|
from attributes import PackSize, PackStatus, COLORS
|
2020-04-02 15:25:15 +02:00
|
|
|
|
2020-04-04 22:03:15 +02:00
|
|
|
WINDOW_SIZE = (600, 600)
|
|
|
|
COLOR_OF_FIELD = {
|
|
|
|
'Floor': 'gray',
|
|
|
|
'Rack': 'white',
|
|
|
|
'Pack': 'yellow'
|
|
|
|
}
|
|
|
|
TILE_WIDTH = 30
|
|
|
|
TILE_HEIGHT = 30
|
|
|
|
CIRCLE_CENTER_X, CIRCLE_CENTER_Y = int(TILE_WIDTH/2), int(TILE_HEIGHT/2)
|
2020-04-02 15:25:15 +02:00
|
|
|
|
2020-04-04 22:03:15 +02:00
|
|
|
class MainGameFrame:
|
|
|
|
def __init__(self):
|
|
|
|
self.display = pygame.display.set_mode(WINDOW_SIZE)
|
2020-04-06 16:11:57 +02:00
|
|
|
agent_radius = int(TILE_WIDTH/2)
|
2020-04-05 16:49:45 +02:00
|
|
|
self.warehouse_map = warehouse.Warehouse(20, 20, 150, 20)
|
2020-04-05 15:04:38 +02:00
|
|
|
starting_x, starting_y = self.set_starting_agent_position()
|
2020-04-06 16:11:57 +02:00
|
|
|
self.agent = agent.Agent(starting_x, starting_y, self.warehouse_map, agent_radius)
|
2020-04-07 01:18:04 +02:00
|
|
|
self.clock = pygame.time.Clock()
|
2020-04-07 18:50:25 +02:00
|
|
|
|
2020-04-04 22:03:15 +02:00
|
|
|
def run(self):
|
2020-04-26 19:57:00 +02:00
|
|
|
# demo_agent_step = 1
|
|
|
|
# demo_agent_sign = 1
|
2020-04-04 22:03:15 +02:00
|
|
|
while True:
|
2020-04-06 15:18:00 +02:00
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
pygame.quit()
|
|
|
|
sys.exit()
|
|
|
|
self.draw_floor()
|
|
|
|
self.draw_packages()
|
|
|
|
self.draw_agent()
|
2020-04-26 19:57:00 +02:00
|
|
|
# self.agent.demo_agent_move(demo_agent_step, 8) #linijka w celu zademonstrowania poruszania się agenta
|
2020-04-07 18:50:25 +02:00
|
|
|
# self.agent.move() #oryginalna linijka
|
2020-04-26 19:57:00 +02:00
|
|
|
# demo_agent_step += (1*demo_agent_sign)
|
|
|
|
# if demo_agent_step >= 8:
|
|
|
|
# demo_agent_sign = -1
|
|
|
|
# if demo_agent_step == 0:
|
|
|
|
# demo_agent_sign = 1
|
2020-04-04 22:03:15 +02:00
|
|
|
pygame.display.update()
|
2020-04-07 01:18:04 +02:00
|
|
|
self.clock.tick(5)
|
2020-04-02 15:25:15 +02:00
|
|
|
|
2020-04-04 22:03:15 +02:00
|
|
|
def draw_floor(self):
|
|
|
|
for x in range(self.warehouse_map.width):
|
|
|
|
for y in range(self.warehouse_map.height):
|
2020-04-05 15:04:38 +02:00
|
|
|
self.draw_field(x, y)
|
2020-04-04 22:03:15 +02:00
|
|
|
|
|
|
|
def draw_field(self, x, y):
|
|
|
|
current_tile = self.warehouse_map.tiles[x][y]
|
2020-04-05 15:04:38 +02:00
|
|
|
if not isinstance(current_tile, warehouse.Tile):
|
|
|
|
current_tile = current_tile.lays_on_field if isinstance(current_tile, warehouse.Pack) else None
|
2020-04-04 22:03:15 +02:00
|
|
|
color = COLOR_OF_FIELD.get(current_tile.category.name, 'white')
|
|
|
|
color = COLORS[color]
|
|
|
|
pygame.draw.rect(self.display, COLORS['black'],
|
|
|
|
(x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT))
|
|
|
|
pygame.draw.rect(self.display, color,
|
|
|
|
((x * TILE_WIDTH) + 1, (y * TILE_HEIGHT) + 1, TILE_WIDTH - 1, TILE_HEIGHT - 1))
|
|
|
|
|
2020-04-05 15:04:38 +02:00
|
|
|
def draw_packages(self):
|
2020-04-05 15:26:28 +02:00
|
|
|
def get_package_color(pack):
|
|
|
|
colors = {
|
|
|
|
PackStatus.LOOSE: COLORS['yellow'],
|
|
|
|
PackStatus.STORED: COLORS['lightgreen'],
|
|
|
|
PackStatus.STORED_BAD_LOCATION: COLORS['red']
|
|
|
|
}
|
|
|
|
return colors[pack.status]
|
|
|
|
|
2020-04-05 15:04:38 +02:00
|
|
|
for pack in self.warehouse_map.packages:
|
|
|
|
pack_x, pack_y = pack.lays_on_field.x_position, pack.lays_on_field.y_position
|
2020-04-05 15:26:28 +02:00
|
|
|
package_color = get_package_color(pack)
|
2020-04-05 15:04:38 +02:00
|
|
|
pygame.draw.rect(self.display, package_color,
|
|
|
|
((pack_x * TILE_WIDTH) + 3, (pack_y * TILE_HEIGHT) + 3, TILE_WIDTH - 5, TILE_HEIGHT - 5))
|
|
|
|
|
2020-04-04 22:03:15 +02:00
|
|
|
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))
|
2020-04-02 15:25:15 +02:00
|
|
|
|
2020-04-05 15:04:38 +02:00
|
|
|
def set_starting_agent_position(self):
|
|
|
|
starting_x, starting_y = random.randrange(self.warehouse_map.width), random.randrange(self.warehouse_map.height)
|
2020-04-05 15:26:28 +02:00
|
|
|
while not isinstance(self.warehouse_map.tiles[starting_x][starting_y], warehouse.Tile) or self.warehouse_map.tiles[starting_x][starting_y].category.name != 'Floor':
|
2020-04-05 15:04:38 +02:00
|
|
|
starting_x, starting_y = random.randrange(self.warehouse_map.width), random.randrange(
|
|
|
|
self.warehouse_map.height)
|
|
|
|
return starting_x, starting_y
|
2020-04-23 19:43:51 +02:00
|
|
|
|
2020-04-02 15:25:15 +02:00
|
|
|
if __name__ == '__main__':
|
2020-04-04 22:03:15 +02:00
|
|
|
maingame = MainGameFrame()
|
|
|
|
maingame.run()
|