Minecraft – Python experiments

Here’s a few little Python files that illustrate control of Minecraft. It starts with connecting to the Minecraft API and writing to the screen and then develops it using loops to create towers, stairs, stacks of TNT and beyond. Note that most of the blocks will be drawn at coordinates (0,0,0) so you’ll have to navigate around to find them. Occasionally Minecraft draws a mountain in the way, so the drawing might be underground.

Sending text to the screen:

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

Creating a stack of ten blocks:

This little code snippet uses a loop to count from 0 to 9. (It’s a feature of Python that the loop starts at zero and finishes one before the last number asked for). The setblock function then places a block at each of the Y locations from 0 up to 9.

from mcpi import minecraft
mc = minecraft.Minecraft.create()
mc.postToChat("Python is now in control!")
for stack in range(10):
    mc.setBlock(0,stack,0,1)

Creating a stack of one hundred blocks:
Here the range of values being created is much larger. This extends a long way up into the sky.

from mcpi import minecraft
mc = minecraft.Minecraft.create()
mc.postToChat("Python is now in control!")
for stack in range(100):
    mc.setBlock(0,stack,0,1)

Creating a huge stack of TNT ready to explode:
This version of the previous program uses setBlock but with the TNT block type (46). This is then followed with a 1 to ensure that the TNT can be triggered.

from mcpi import minecraft
mc = minecraft.Minecraft.create()
mc.postToChat("Python is now in control!")
tnt=46
for stack in range(100):
    mc.setBlock(0,stack,0,tnt,1)

Creating a staircase which extends into the sky:
This snippet moves the blocks along the X and Y axis at the same time to ensure that the blocks move up and forward at the same time. This creates a staircase effect.

from mcpi import minecraft
mc = minecraft.Minecraft.create()
mc.postToChat("Python is now in control!")
tnt=46
for stack in range(100):
    mc.setBlock(stack,stack,0,1)

Creating a wide staircase:
This modification of the staircase duplicates the stacking, making sure that the blocks are placed at Z=0, Z=1, Z=2 and Z=3. The staircase is much wider and more interesting.

from mcpi import minecraft
mc = minecraft.Minecraft.create()
mc.postToChat("Python is now in control!")
tnt=46
for stack in range(100):
    mc.setBlock(stack,stack,0,1)
    mc.setBlock(stack,stack,1,1)
    mc.setBlock(stack,stack,2,1)
    mc.setBlock(stack,stack,3,1)

Using a nested loop to create a row of stacks:
Python allows one loop inside another. This means that a stack can then be placed multiple times. The output of this looks like a row of fence posts, although it could equally be a set of bridge supports over a wide gap.

from mcpi import minecraft
mc = minecraft.Minecraft.create()
#mc.postToChat("Python is now in control!")
for row in range(0,50,5):
    for stack in range(10):
        print("Row ",row," : stack ",stack)
        mc.setBlock(row,stack,0,1)

Creating a grid of stacks:
This is another nested loop which creates a grid of stacked cubes. This might be ideal for the columns supporting the roof of a temple.

from mcpi import minecraft
mc = minecraft.Minecraft.create()
#mc.postToChat("Python is now in control!")
for columns in range(0,50,5):
    for row in range(0,50,5):
        for stack in range(10):
        print("Row ",row," : stack ",stack)
        mc.setBlock(row,stack,columns,1)
Advertisements

Minecraft Text Generator

RPi Kitchen written in blocks

Using block-type 1 to automatically write text from a Python file.

A few more experiments with the Minecraft API and Python has brought be to this point… combining figlet with Python to create a minecraft text generator. More information on figlet can be found at www.figlet.org.

First… install figlet on your Raspberry Pi using:

sudo apt-get install figlet

Then test figlet by using:

figlet "hello world"

The basic structure of the program requires:

  • A connection to the Minecraft API.
  • Input the phrase to be rendered into a string variable.
  • Send the string to the operating system to be processed by figlet using the banner font.
  • Read the response back into a string variable and then split it into individual lines.
  • Work along each line creating a block or air depending on whether figlet added a “#” symbol to represent part of the text.

The screenshots were generated by raspi2png using the instructions from Raspberry Pi Spy.

The Python program is shown below:

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

wr_str = raw_input('What would you like to write? ') # ask the user what they'd like to write at the current position

cmd = 'figlet -f banner '+wr_str # create an operating system command

line = commands.getoutput( cmd ) # send cmd as a command to the operating system and receive the result.

list_line = line.rstrip().split('\n') # split the result from 'figlet' into separate lines (right strip new line feeds)
print 'Your text has been converted to'
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 list_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)

Something that looks rather nice is to replace the “air” blocks (code 0 in the penultimate line) with something like 22 which translates to Lapis Lazuli. Alternatively, use 8 for the block type, which becomes water which flows from the words shown.

.