def create_block(*block_id): """(Block) Creates a block (this function can be thought of as a block factory) Parameters: block_id (*tuple): N-length tuple to uniquely identify the block, often comprised of strings, but not necessarily (arguments are grouped into a single tuple) Examples: >>> create_block("leaf") LeafBlock() >>> create_block("stone") ResourceBlock('stone') >>> create_block("mayhem", 1) TrickCandleFlameBlock(1) """ if len(block_id) == 1: block_id = block_id[0] if block_id == "leaf": return LeafBlock() elif block_id == "crafting_table": return CraftingTableBlock(block_id, BREAK_TABLES["wood"]) elif block_id in BREAK_TABLES: return ResourceBlock(block_id, BREAK_TABLES[block_id]) elif block_id[0] == 'mayhem': return TrickCandleFlameBlock(block_id[1]) raise KeyError(f"No block defined for {block_id}")
def create_block(*block_ids): if len(block_ids) == 1: block_id = block_ids[0] if block_id == "leaf": return LeafBlock() elif block_id in BREAK_TABLES: return ResourceBlock(block_id, BREAK_TABLES[block_id]) # ~< remove from template elif block_id == "crafting_table": return CraftingTableBlock() # ~> elif block_ids[0] == 'mayhem': return TrickCandleFlameBlock(block_ids[1]) raise KeyError(f"No block defined for {block_ids}")
def load_simple_world(world): block_weights = [ (100, 'dirt'), (30, 'stone'), ] cells = {} ground = [] width, height = world.get_grid_size() for x in range(width): for y in range(height): if x < 22: if y <= 8: continue else: if x + y < 30: continue ground.append((x, y)) weights, blocks = zip(*block_weights) kinds = random.choices(blocks, weights=weights, k=len(ground)) for cell, block_id in zip(ground, kinds): cells[cell] = create_block(block_id) trunks = [(3, 8), (3, 7), (3, 6), (3, 5)] for trunk in trunks: cells[trunk] = create_block('wood') leaves = [(4, 3), (3, 3), (2, 3), (4, 2), (3, 2), (2, 2), (4, 4), (3, 4), (2, 4)] for leaf in leaves: cells[leaf] = create_block('leaf') for cell, block in cells.items(): # cell -> box i, j = cell world.add_block_to_grid(block, i, j) world.add_block_to_grid(TrickCandleFlameBlock(0), 14, 8)