Saturday, November 05, 2016

Getting Oriented With Minecraft Pi

Minecraft Pi is Python for Minecraft. You can run Python programs from a folder on your computer that make things happen in a Minecraft server in the same computer or even on a remote one. Actually Minecraft Pi is for the Raspberry Pi but I'm using the MCPI mod which can work on a regular (vanilla) Minecraft server or I have it working on a MinecraftEdu server (version 1.7.10).

I've started making buildings with code and found the most confusing thing getting started was knowing where the building would materialize when I run the program. It turns out cardinal directions are important, as the X, Y, and Z axes are fixed in the world, with the X axis running west (negative) to east (positive), Y down (negative) to up (positive), and Z running north (negative) to south (positive). Wherever you're standing (or flying) is the x, y, z coordinate 0, 0, 0. If you don't want to be stuck inside the block you place with code you will want to place it away from where you are standing. So if you face the rising sun in the east and run this, the stone block will appear right in front of you:
pos = mc.player.getPos()
mc.setBlock(pos.x+1,pos.y,pos.z,block.STONE.id)
What's hard to remember until you do it a bunch is no matter which way you are facing it will appear between you and the eastern sun.


Wool blocks are fun to use for building because they come in a variety of colors. But the colors are coded with ID numbers, so this graphic helps with that:







A yellow wool block:
mc.setBlock(pos.x+1,pos.y,pos.z,block.WOOL.id,4)
So here's a house.

Source code:
import mcpi.minecraft as minecraft
import mcpi.block as block
import time

mc = minecraft.Minecraft.create("localhost", 4711)
pos = mc.player.getPos()

def wood_building():
    mc.player.setPos(pos.x+5,pos.y,pos.z-2) #this will move you back so you're not trapped
    time.sleep(5)
    #right wall
    for num in range(2):
        mc.setBlocks(pos.x, pos.y+num, pos.z, pos.x, pos.y+num, pos.z+10, block.WOOD_PLANKS.id)
    #windows
    for num in range(3,8):
        mc.setBlock(pos.x, pos.y+1, pos.z+num, block.GLASS_PANE.id)
    #left wall
    for num in range(2):
        mc.setBlocks(pos.x+10, pos.y+num, pos.z, pos.x+10, pos.y+num, pos.z+10, block.WOOD_PLANKS.id)
    #windows
    for num in range(3,8):
        mc.setBlock(pos.x+10, pos.y+1, pos.z+num, block.GLASS_PANE.id)
    #roof
    for col in range(11):
        if col < 5:
            mc.setBlocks(pos.x+col, pos.y+col+2, pos.z-1,pos.x+col, pos.y+col+2, pos.z+11, block.STAIRS_WOOD.id,0)
            mc.setBlocks(pos.x+col, pos.y+1, pos.z,pos.x+col, pos.y+1+col, pos.z, block.WOOD_PLANKS.id)            
            mc.setBlocks(pos.x+col, pos.y+1, pos.z+10,pos.x+col, pos.y+1+col, pos.z+10, block.WOOD_PLANKS.id)
        elif col == 5:
            mc.setBlocks(pos.x+col, pos.y+col+1, pos.z-1,pos.x+col, pos.y+col+1, pos.z+11, block.GLASS.id)
            mc.setBlocks(pos.x+col, pos.y+1, pos.z,pos.x+col, pos.y+1+col, pos.z, block.WOOD_PLANKS.id)            
            mc.setBlocks(pos.x+col, pos.y+1, pos.z+10,pos.x+col, pos.y+1+col, pos.z+10, block.WOOD_PLANKS.id)
        else:
            mc.setBlocks(pos.x+col, pos.y+12-col, pos.z-1,pos.x+col, pos.y+12-col, pos.z+11, block.STAIRS_WOOD.id,1)
            mc.setBlocks(pos.x+col, pos.y+1, pos.z+10,pos.x+col, pos.y+11-col, pos.z+10, block.WOOD_PLANKS.id)
            mc.setBlocks(pos.x+col, pos.y+1, pos.z,pos.x+col, pos.y+11-col, pos.z, block.WOOD_PLANKS.id)
    #front wall
    for num in range(2):
        mc.setBlocks(pos.x, pos.y+num, pos.z, pos.x+10, pos.y+num, pos.z, block.WOOD_PLANKS.id)
    #windows
    mc.setBlock(pos.x+3, pos.y+1, pos.z, block.GLASS_PANE.id)
    mc.setBlock(pos.x+7, pos.y+1, pos.z, block.GLASS_PANE.id)
    #back wall
    for num in range(2):
        mc.setBlocks(pos.x, pos.y+num, pos.z+10, pos.x+10, pos.y+num, pos.z+10, block.WOOD_PLANKS.id)
    #windows
    for num in range(3,8):
        mc.setBlock(pos.x+num, pos.y+1, pos.z+10, block.GLASS_PANE.id)
    #door frame
    mc.setBlocks(pos.x+5, pos.y, pos.z, pos.x+5, pos.y+1, pos.z, block.AIR.id)
    #floor
    for col in range(11):
        for row in range(11):
            mc.setBlock(pos.x+row, pos.y-1, pos.z+col, block.WOOD_PLANKS.id)

wood_building()


1 comment :

Admin said...

If you are playing Minecraft you will love it. Click on: OptiFine 1.15.1 Mod