Dodanie paczek, paczka jest obiektem klasy Pack, obiekt ten przyjmuje informacje o swoim rozmiarze/wadze(domyślnie MEDIUM), kategorii (domyślnie 'general' TODO kategoria 'mrozone'/'latwo_psujace_sie'), oraz pole na którym paczka leży, które jest obiektem klasy Tile

This commit is contained in:
andrzej 2020-04-05 15:04:38 +02:00
parent fc7328470d
commit c33043986c
2 changed files with 50 additions and 9 deletions

23
main.py
View File

@ -24,11 +24,13 @@ 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)
self.warehouse_map = warehouse.Warehouse(20, 20)
starting_x, starting_y = random.randrange(40), random.randrange(40)
self.warehouse_map = warehouse.Warehouse(20, 20, 100, 10)
starting_x, starting_y = self.set_starting_agent_position()
self.agent = agent.Agent(starting_x, starting_y, 20)
def run(self):
self.draw_floor()
self.draw_packages()
self.draw_agent()
while True:
pygame.display.update()
@ -40,6 +42,8 @@ class MainGameFrame:
def draw_field(self, x, y):
current_tile = self.warehouse_map.tiles[x][y]
if not isinstance(current_tile, warehouse.Tile):
current_tile = current_tile.lays_on_field if isinstance(current_tile, warehouse.Pack) else None
color = COLOR_OF_FIELD.get(current_tile.category.name, 'white')
color = COLORS[color]
pygame.draw.rect(self.display, COLORS['black'],
@ -47,13 +51,24 @@ class MainGameFrame:
pygame.draw.rect(self.display, color,
((x * TILE_WIDTH) + 1, (y * TILE_HEIGHT) + 1, TILE_WIDTH - 1, TILE_HEIGHT - 1))
def draw_packages(self):
for pack in self.warehouse_map.packages:
pack_x, pack_y = pack.lays_on_field.x_position, pack.lays_on_field.y_position
package_color = COLORS['yellow']
pygame.draw.rect(self.display, package_color,
((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)
# import pdb
# pdb.set_trace()
pygame.draw.circle(self.display, COLORS['black'], agent_screen_position, self.agent.radius, int(self.agent.radius/2))
def set_starting_agent_position(self):
starting_x, starting_y = random.randrange(self.warehouse_map.width), random.randrange(self.warehouse_map.height)
while self.warehouse_map.tiles[starting_x][starting_y].category.name != 'Floor':
starting_x, starting_y = random.randrange(self.warehouse_map.width), random.randrange(
self.warehouse_map.height)
return starting_x, starting_y
if __name__ == '__main__':
maingame = MainGameFrame()
maingame.run()

View File

@ -2,6 +2,7 @@ from attributes import PackSize
import random
import queue
import itertools
class CategoryData:
def __init__(self, name, passable=False, can_store=True, location='general', pack_size=PackSize.ALL):
self.name = name
@ -14,9 +15,11 @@ class CategoryData:
return self.name
class Pack:
def __init__(self, size, categ='general'):
def __init__(self, size=PackSize.MEDIUM, categ='general', lays_on_field=None):
self.size = size
self.category = categ
assert isinstance(lays_on_field, Tile), AssertionError("Attribute lays_on_field must be a Tile object! You see, package cannot lay in void :)")
self.lays_on_field = lays_on_field
CATEGORY = {
@ -27,11 +30,14 @@ CATEGORY = {
class Warehouse:
def __init__(self, width, length):
def __init__(self, width, length, no_of_racks, no_of_packages=0):
self.width = width
self.height = length
self.tiles = self.generate_map()
self.no_of_racks = no_of_racks
self.no_of_packages = no_of_packages
self.generate_racks()
self.packages = self.place_packages(no_of_packages)
# import pdb
# pdb.set_trace()
def __str__(self):
@ -47,6 +53,7 @@ class Warehouse:
def generate_racks(self):
q = queue.Queue()
rack_counter = 0
# import pdb
# pdb.set_trace()
node_x, node_y = random.randrange(1, self.width-1), random.randrange(1, self.height-1)
@ -67,9 +74,12 @@ class Warehouse:
next_node = q.get()
else:
current_node = next_node if next_node is not None else node
next_node = random.choice(not_rack_nodes)
current_node = next_node if next_node is not None else node
self.break_wall(next_node, current_node)
rack_counter += 1
if rack_counter > self.no_of_racks:
return
node_x = next_node.x_position
node_y = next_node.y_position
self.tiles[node_x][node_y] = Tile('rack', node_x, node_y)
@ -79,6 +89,21 @@ class Warehouse:
possible_nodes = [line for line in adjacent_tiles if line.category.name != "Rack"]
return possible_nodes
def place_packages(self, no_of_packages):
packages = []
for i in range(no_of_packages):
pack_x, pack_y = self._set_package_position()
package_field = self.tiles[pack_x][pack_y]
new_package = Pack(lays_on_field=package_field)
self.tiles[pack_x][pack_y] = new_package
packages.append(new_package)
return packages
def _set_package_position(self):
starting_x, starting_y = random.randrange(self.width), random.randrange(self.height)
while self.tiles[starting_x][starting_y].category.name != 'Floor':
starting_x, starting_y = random.randrange(self.width), random.randrange(
self.height)
return starting_x, starting_y
def get_adjacent_tiles(self, x, y):
x_start = x - 2 if x - 2 >= 0 else 0
@ -109,6 +134,7 @@ class Warehouse:
wall_y = current_node.y_position + y_relative
wall = self.tiles[wall_x][wall_y]
return wall
class Tile:
def __init__(self, category, x_position, y_position):
self.category = CATEGORY.get(category, CATEGORY['floor'])