From c33043986c9a699477fc9b752a25ad18d52cfc7f Mon Sep 17 00:00:00 2001 From: andrzej Date: Sun, 5 Apr 2020 15:04:38 +0200 Subject: [PATCH] =?UTF-8?q?Dodanie=20paczek,=20paczka=20jest=20obiektem=20?= =?UTF-8?q?klasy=20Pack,=20obiekt=20ten=20przyjmuje=20informacje=20o=20swo?= =?UTF-8?q?im=20rozmiarze/wadze(domy=C5=9Blnie=20MEDIUM),=20kategorii=20(d?= =?UTF-8?q?omy=C5=9Blnie=20'general'=20TODO=20kategoria=20'mrozone'/'latwo?= =?UTF-8?q?=5Fpsujace=5Fsie'),=20oraz=20pole=20na=20kt=C3=B3rym=20paczka?= =?UTF-8?q?=20le=C5=BCy,=20kt=C3=B3re=20jest=20obiektem=20klasy=20Tile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 25 ++++++++++++++++++++----- warehouse.py | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index 230527e..89a5861 100644 --- a/main.py +++ b/main.py @@ -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() @@ -36,10 +38,12 @@ class MainGameFrame: def draw_floor(self): for x in range(self.warehouse_map.width): for y in range(self.warehouse_map.height): - self.draw_field(x,y) + self.draw_field(x, y) 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() \ No newline at end of file diff --git a/warehouse.py b/warehouse.py index 7e3c035..ebd0e3f 100644 --- a/warehouse.py +++ b/warehouse.py @@ -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) - self.break_wall(next_node, current_node) + 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'])