2
\$\begingroup\$

I'm fairly new to coding and I wrote this script in Python for a house with a roof in Minecraft Pi. I've found different ways to write the coordinates to make the code base shorter, but I'm wondering if it is possible for me to refactor it any further so I don't have to list each coordinate line by line.

import mcpi.minecraft as minecraft
import mcpi.block as block
mc = minecraft.Minecraft.create()
p = mc.player.getTilePos()

mc.setBlocks(p.x+1, p.y, p.z+1, #Main structure starting coordinates (DO NOT ALTER)
             p.x+19, p.y+4, p.z+6, #Structure size/coordinates (Alters cube size)
             block.STONE_BRICK)

mc.setBlocks(p.x+2, p.y+1, p.z+2, #Hollowing out main structure interior
             p.x+18, p.y+3, p.z+5,
             block.AIR)

mc.setBlocks(p.x, p.y+4, p.z, #Main Structure Roof (Front Side, 1st Row)
             p.x+12, p.y+4, p.z,
             block.STAIRS_WOOD.id, 2)

mc.setBlocks(p.x+20, p.y+4, p.z,
             p.x+20, p.y+4, p.z,
             block.STAIRS_WOOD.id, 2)

mc.setBlocks(p.x, p.y+5, p.z+1, #Main Structure Roof (Front Side, 2nd Row)
             p.x+13, p.y+5, p.z+1,
             block.STAIRS_WOOD.id, 2)

mc.setBlocks(p.x+19, p.y+5, p.z+1,
             p.x+20, p.y+5, p.z+1,
             block.STAIRS_WOOD.id, 2)

mc.setBlocks(p.x, p.y+6, p.z+2, #Main Structure Roof (Front Side, 3rd Row)
             p.x+14, p.y+6, p.z+2,
             block.STAIRS_WOOD.id, 2)

mc.setBlocks(p.x+18, p.y+6, p.z+2,
             p.x+20, p.y+6, p.z+2,
             block.STAIRS_WOOD.id, 2)

mc.setBlocks(p.x, p.y+7, p.z+3, #Main Structure Roof (Front Side, 4th Row)
             p.x+20, p.y+7, p.z+3,
             block.STAIRS_WOOD.id, 2)

mc.setBlocks(p.x, p.y+4, p.z+7, #Main Structure Roof (Rear Side, 1st Row)
             p.x+20, p.y+4, p.z+7,
             block.STAIRS_WOOD.id, 3)

mc.setBlocks(p.x, p.y+5, p.z+6, #Main Structure Roof (Rear Side, 2nd Row)
             p.x+20, p.y+5, p.z+6,
             block.STAIRS_WOOD.id, 3)

mc.setBlocks(p.x, p.y+6, p.z+5, #Main Structure Roof (Rear Side, 3rd Row)
             p.x+20, p.y+6, p.z+5,
             block.STAIRS_WOOD.id, 3)

mc.setBlocks(p.x, p.y+7, p.z+4, #Main Structure Roof (Rear Side, 4th Row)
             p.x+20, p.y+7, p.z+4,
             block.STAIRS_WOOD.id, 3)

mc.setBlocks(p.x+1, p.y+5, p.z+2, #Main Structure Roof End (Right Side, 1st Row)
             p.x+1, p.y+5, p.z+5,
             block.STONE_BRICK)

mc.setBlocks(p.x+1, p.y+6, p.z+3, #Main Structure Roof End (Right Side, 2nd Row)
             p.x+1, p.y+6, p.z+4,
             block.STONE_BRICK)

mc.setBlocks(p.x+19, p.y+5, p.z+2, #Main Structure Roof End (Left Side, 1st Row)
             p.x+19, p.y+5, p.z+5,
             block.STONE_BRICK)

mc.setBlocks(p.x+19, p.y+6, p.z+3, #Main Structure Roof End (Left Side, 2nd Row)
             p.x+19, p.y+6, p.z+4,
             block.STONE_BRICK)
New contributor
DysFunction is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
  • \$\begingroup\$ You could also introduce functions to do lines and squares instead of just doing volumes. \$\endgroup\$ – holroy Feb 13 at 0:45
  • \$\begingroup\$ That's what I was wondering about, but not sure how to pull off. Would I need to have a function for each part of whatever I am building? \$\endgroup\$ – DysFunction 18 hours ago
2
\$\begingroup\$

You can put the different parts of the building in a list and then iterate through this list, using the values in the list.

I would propose this structure:

parts = [
    # x0  y0  z0    x1  y1  z1    block id          meta data  
    (( 1,  0,  1), (19,  4, 19), (block.STONE_BRICK, 0)), # Main structure
    (( 2,  1,  2), (18,  3,  5), (block.AIR        , 0)), # Hollowing out main structure interior
    # ... the other parts
]

You can then use this loop to build the parts:

for (x0, y0, z0), (x1, y1, z1), (block, meta) in parts:
    mc.setBlocks(p.x + x0, p.y + y0, p.z + z0,
                 p.x + x1, p.y + y1, p.z + z1,
                 block, meta)

I would also advice you to follow a few style suggestions from PEP8, in this case I specifically mean that you should add empty lines after the imports:

import mcpi.minecraft as minecraft
import mcpi.block as block

mc = minecraft.Minecraft.create()
p = mc.player.getTilePos()

and that you should always add spaces around operators and after #:

mc.setBlocks(p.x+1, p.y, p.z+1 #...

to:

mc.setBlocks(p.x + 1, p.y, p.z + 1 # ...

This simply eases the reading process.

\$\endgroup\$

Your Answer

DysFunction is a new contributor. Be nice, and check out our Code of Conduct.

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.