More Minecraft programming

Symbols

SymbolI’ve done some more experiments with the Minecraft API – trying to use it to create shapes and patterns, as well as a few automatic structures. The first little program uses a pattern stored as “#” symbols in a list and reads them one-by-one. If there is a “#” then it creates a block, and if not it creates air.

from mcpi import minecraft
mc = minecraft.Minecraft.create()
mc.postToChat("Python is now in control!")

line = "#########","# # # # #","#########","# # # # #","#########","# # # # #","#########","# # # # #","#########","# # # # #"

print line

startx,starty,startz = mc.player.getPos() #get the player's position

starty=starty+10 #lift the starty so that it's not written under your feet!

for row in line: # one row at a time from list_line (the result from figlet)
    starty=starty-1 # work down from the top
    column=0
    for letter in row: # work along each row - check each character. If it's a "#" then print a block else leave it as air
        column = column+1
        if letter =="#":
            mc.setBlock(startx-column,starty,startz,1)
        else:
            mc.setBlock(startx-column,starty,startz,0)

 
 

Heart
Heart This program is a development of the symbol program, but to create a heart and fill it with wool. The wool has can have another number added to the block id which means that the colour can be controlled. Just the thing with Valentines day a few weeks ago…


from mcpi import minecraft
mc = minecraft.Minecraft.create()
mc.postToChat("Python is now in control!")


line = " ##   ## ","#### ####","#########","#########","#########"," ####### ","   ###   ","    #    "

print line

startx,starty,startz = mc.player.getPos() #get the player's position

starty=starty+10 #lift the starty so that it's not written under your feet!

for row in line: # one row at a time from list_line (the result from figlet)
    starty=starty-1 # work down from the top
    column=0
    for letter in row: # work along each row - check each character. If it's a "#" then print a block else leave it as air
        column = column+1
        if letter =="#":
            mc.setBlock(startx-column,starty,startz,35,10)
        else:
            mc.setBlock(startx-column,starty,startz,0)

 
 

Heart2
Heart2Here I’ve added another version of the heart program. At the beginning it generates random coordinates and a random colour for the wool blocks.



import random
from mcpi import minecraft
mc = minecraft.Minecraft.create()
mc.postToChat("Python is now in control!")


line = " ##   ## ","#### ####","#########","#########","#########"," ####### ","   ###   ","    #    "

#print line

for loop in range(1,100):
    startx=random.randint(-127,127)
    starty=random.randint(10,20)
    startz=random.randint(-127,127)
    colour =random.randint(1,16)
    #print(loop," : ",startx,starty,startz)
    for row in line: # one row at a time from list_line (the result from figlet)
        starty=starty-1 # work down from the top
        column=0
        for letter in row: # work along each row - check each character. If it's a "#" then print a block else leave it as air
            column = column+1
            if letter =="#":
                mc.setBlock(startx-column,starty,startz,35,colour)
            else:
                mc.setBlock(startx-column,starty,startz,0)

 
 

Face
Face

The face program reads the characters from the list and turns them into blocks in the shape of a face. It wouldn’t take a great deal of work to make all sorts of structures, doorways or windows with this type of program. By extending the “if letter ==”#” section of the program to include other characters, it would be possible to use a variety of block effects.


from mcpi import minecraft
mc = minecraft.Minecraft.create()
mc.postToChat("Python is now in control!")

line = " ####### ","#########","##  #  ##","##  #  ##","#########","## ### ##","###   ###","#########"," ####### ","  #####  "



startx,starty,startz = mc.player.getPos() #get the player's position

starty=starty+10 #lift the starty so that it's not written under your feet!

for row in line: # one row at a time from list_line (the result from figlet)
    starty=starty-1 # work down from the top
    column=0
    print row
    for letter in row: # work along each row - check each character. If it's a "#" then print a block else leave it as air
        column = column+1
        if letter =="#":
            mc.setBlock(startx-column,starty,startz,1)
        else:
            mc.setBlock(startx-column,starty,startz,0)

 
 

Tile
tile
This program is similar to the Face program, but it creates a patterned tile on the ground. This could be altered to produce interesting mosaic flooring designs or perhaps even direction indicator markers.


from mcpi import minecraft
mc = minecraft.Minecraft.create()
mc.postToChat("Python is now in control!")

line = " ####### ","#########","##  #  ##","##  #  ##","#########","## ### ##","###   ###","#########"," ####### ","  #####  "



startx,starty,startz = mc.player.getPos() #get the player's position

startz=startz+10 #lift the starty so that it's not written under your feet!
starty=starty-1 # put it under the feet
for row in line: # one row at a time from list_line (the result from figlet)
    startz=startz-1 # work down from the top
    column=0
    print row
    for letter in row: # work along each row - check each character. If it's a "#" then print a block else leave it as air
        column = column+1
        if letter =="#":
            mc.setBlock(startx-column,starty,startz,2)
        else:
            mc.setBlock(startx-column,starty,startz,1)

 
 

Tower
Tower

I’ve seen a program to create a pyramid on another website and wondered if I could develop my own from scratch. I also wanted to see if some kind of communication tower could be created – or perhaps a pylon.

from mcpi import minecraft
import time
mc = minecraft.Minecraft.create()

mc.postToChat("Python is now in control!")

def Pillars(x,y,z,height,width):
    #put in the four pillars
    mc.setBlocks(x,y,z,x,y+height,z,1)
    mc.setBlocks(x+width,y,z,x+width,y+height,z,1)
    mc.setBlocks(x,y,z+width,x,y+height,z+width,1)
    mc.setBlocks(x+width,y,z+width,x+width,y+height,z+width,1)
    #join the corners
    mc.setBlocks(x,y+height,z,x+width,y+height,z,1)
    mc.setBlocks(x,y+height,z,x,y+height,z+width,1)
    mc.setBlocks(x+width,y+height,z,x+width,y+height,z+width,1)
    mc.setBlocks(x,y+height,z+width,x+width,y+height,z+width,1)
    
#   ] time.sleep(10)
#    mc.setBlocks(x,y,z,x+width,y+height,z+width,0)

startx,starty,startz = mc.player.getPos() #get the player's position

towerheight=3
offset=0
towerwidth=10
for i in range(0,towerwidth/2):
    print i, offset, starty+offset
    Pillars(startx+i,starty+offset,startz+i,3,towerwidth-(i*2))
    offset=offset+towerheight

 
 

Tower2
Tower2

Here’s a development of the above program. It is a bit better structurally, having better support on the upright pieces. There’s very little to it, so perhaps more useful to illustrate how to develop the idea further.

from mcpi import minecraft
import time
mc = minecraft.Minecraft.create()

mc.postToChat("Python is now in control!")

def Pillars(x,y,z,height,width):
    #put in the four pillars
    mc.setBlocks(x,y,z,x,y+height,z,1)
    mc.setBlocks(x+width,y,z,x+width,y+height,z,1)
    mc.setBlocks(x,y,z+width,x,y+height,z+width,1)
    mc.setBlocks(x+width,y,z+width,x+width,y+height,z+width,1)

    #join the top corners
    mc.setBlocks(x,y+height,z,x+width,y+height,z,1)
    mc.setBlocks(x,y+height,z,x,y+height,z+width,1)
    mc.setBlocks(x+width,y+height,z,x+width,y+height,z+width,1)
    mc.setBlocks(x,y+height,z+width,x+width,y+height,z+width,1)

    #join the bottom corners
    mc.setBlocks(x,y,z,x+width,y,z,1)
    mc.setBlocks(x,y,z,x,y,z+width,1)
    mc.setBlocks(x+width,y,z,x+width,y,z+width,1)
    mc.setBlocks(x,y,z+width,x+width,y,z+width,1)
    
startx,starty,startz = mc.player.getPos() #get the player's position

towerheight=3
offset=0
towerwidth=10
for i in range(0,towerwidth/2):
    print i, offset, starty+offset
    Pillars(startx+i,starty+offset,startz+i,3,towerwidth-(i*2))
    offset=offset+towerheight


 
 

Tower3
Tower3

Here’s the Pyramid that I was originally intending to have a go at creating. The door isn’t centred properly and perhaps there’s more elegant coding, but it’s suitable as a quick structure to add to the Minecraft landscape. I’d like to have a go at creating a labyrinth inside the pyramid eventually, so watch this space…

from mcpi import minecraft
import time
mc = minecraft.Minecraft.create()

mc.postToChat("Python is now in control!")
#mc.setBlocks(-100,0,-100,100,100,100,0)
#mc.setBlocks(-100,1,-100,100,1,100,1)


def Pillars(x,y,z,height,width):
    useblock=1
    #join the bottom corners
    mc.setBlocks(x,y,z,x+width,y+height,z,useblock)
    mc.setBlocks(x,y,z,x,y+height,z+width,useblock)
    mc.setBlocks(x+width,y,z,x+width,y+height,z+width,useblock)
    mc.setBlocks(x,y,z+width,x+width,y+height,z+width,useblock)
    
startx,starty,startz = mc.player.getPos() #get the player's position

towerheight=4
offset=0
towerwidth=10
for i in range(0,towerwidth/2):
    Pillars(startx+i,starty+offset,startz+i,towerheight,towerwidth-(i*2))
    offset=offset+towerheight

#create door
    mc.setBlocks((startx-1)+(towerwidth/2),starty,startz,startx+1,starty+2,startz,0)


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s