Once I had experimented with the Minecraft-Python API I wanted a little project to try it out. So I decided to create a script that would automatically create a castle with a moat and a couple of perimeter walls.

This seemed like a good choice as the walls and the keep are really the same construct just a different width and height. The keep would just need a bit of extra work to add floors, windows and a door.

Here is the finished castle.

Setup

To use the script below you will need to have Minecraft and the Python API setup. In the latest Raspbian image Minecraft and the API are already setup so you dive straight into the fun bit.

Download The Castle Builder Script

This is my script :

#!/usr/bin/python #-------------------------------------- # # Minecraft Python API # Castle Builder # # This script creates a castle complete # with moat and perimeter walls. # # Author : Matt Hawkins # Date : 07/06/2014 # # https://www.raspberrypi-spy.co.uk/ # #-------------------------------------- # Import Minecraft libraries import mcpi.minecraft as minecraft import mcpi.block as block mc = minecraft.Minecraft.create() mc.postToChat("Let's build a castle!") #-------------------------------------- # Define Functions #-------------------------------------- def CreateWalls(size,baseheight,height,material,battlements,walkway): # Create 4 walls with a specified width, height and material. # Battlements and walkways can also be added to the top edges. mc.setBlocks(-size,baseheight+1,-size,size,baseheight+height,-size,material) mc.setBlocks(-size,baseheight+1,-size,-size,baseheight+height,size,material) mc.setBlocks(size,baseheight+1,size,-size,baseheight+height,size,material) mc.setBlocks(size,baseheight+1,size,size,baseheight+height,-size,material) # Add battlements to top edge if battlements==True: for x in range(0,(2*size)+1,2): mc.setBlock(size,baseheight+height+1,(x-size),material) mc.setBlock(-size,baseheight+height+1,(x-size),material) mc.setBlock((x-size),baseheight+height+1,size,material) mc.setBlock((x-size),baseheight+height+1,-size,material) # Add wooden walkways if walkway==True: mc.setBlocks(-size+1,baseheight+height-1,size-1,size-1,baseheight+height-1,size-1,block.WOOD_PLANKS) mc.setBlocks(-size+1,baseheight+height-1,-size+1,size-1,baseheight+height-1,-size+1,block.WOOD_PLANKS) mc.setBlocks(-size+1,baseheight+height-1,-size+1,-size+1,baseheight+height-1,size-1,block.WOOD_PLANKS) mc.setBlocks(size-1,baseheight+height-1,-size+1,size-1,baseheight+height-1,size-1,block.WOOD_PLANKS) def CreateLandscape(moatwidth,moatdepth,islandwidth): # Set upper half to air mc.setBlocks(-128,1,-128,128,128,128,block.AIR) # Set lower half of world to dirt with a layer of grass mc.setBlocks(-128,-1,-128,128,-128,128,block.DIRT) mc.setBlocks(-128,0,-128,128,0,128,block.GRASS) # Create water moat mc.setBlocks(-moatwidth,0,-moatwidth,moatwidth,-moatdepth,moatwidth,block.WATER) # Create island inside moat mc.setBlocks(-islandwidth,0,-islandwidth,islandwidth,1,islandwidth,block.GRASS) def CreateKeep(size,baseheight,levels): # Create a keep with a specified number # of floors levels and a roof height=(levels*5)+5 CreateWalls(size,baseheight,height,block.STONE_BRICK,True,True) # Floors & Windows for level in range(1,levels+1): mc.setBlocks(-size+1,(level*5)+baseheight,-size+1,size-1,(level*5)+baseheight,size-1,block.WOOD_PLANKS) # Windows for level in range(1,levels+1): CreateWindows(0,(level*5)+baseheight+2,size,"N") CreateWindows(0,(level*5)+baseheight+2,-size,"S") CreateWindows(-size,(level*5)+baseheight+2,0,"W") CreateWindows(size,(level*5)+baseheight+2,0,"E") # Door mc.setBlocks(0,baseheight+1,size,0,baseheight+2,size,block.AIR) def CreateWindows(x,y,z,dir): if dir=="N" or dir=="S": z1=z z2=z x1=x-2 x2=x+2 if dir=="E" or dir=="W": z1=z-2 z2=z+2 x1=x x2=x mc.setBlocks(x1,y,z1,x1,y+1,z1,block.AIR) mc.setBlocks(x2,y,z2,x2,y+1,z2,block.AIR) if dir=="N": a=3 if dir=="S": a=2 if dir=="W": a=0 if dir=="E": a=1 mc.setBlock(x1,y-1,z1,109,a) mc.setBlock(x2,y-1,z2,109,a) #-------------------------------------- # # Main Script # #-------------------------------------- print("Create ground and moat") CreateLandscape(33,10,23) print("Create outer walls") CreateWalls(21,1,5,block.STONE_BRICK,True,True) print("Create inner walls") CreateWalls(13,1,6,block.STONE_BRICK,True,True) print("Create Keep with 4 levels") CreateKeep(5,1,4) print("Position player on Keep's walkway") mc.player.setPos(0,30,4)

If you want to download it directly to your Pi use wget :

wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/minecraft/castle.py

A Few Points to Note

The “CreateLandscape” function clears the whole Minecraft world (a 256x256x256 cube). It sets the top half to “air” and the bottom half to “dirt”. It adds a top layer of grass to the dirt. You may wish to save your world if you have been working on something else as running this script will replace it.

The castle will be placed directly in the centre of the world.

Run the Script to Build the Castle

With Minecraft running you can execute the Python script in a terminal window using :

python castle.py

It may take up to 20 seconds to complete depending on the Pi model you are using.

The script consists of some function definitions with the main script at the end just calling these functions. This makes it easier to tweak a function once and it then be used multiple times.

The perimeter walls are created using the Python function “CreateWalls” with a different width and height. They both have a wooden walkway on the inner edge as well as battlements.

The Keep is created using “CreateKeep” which in turn calls “CreateWalls”. The Keep is just a set of walls that are taller than the others. It has battlements and a wooden walkway.

The inside of the keep has floors splitting it up into a number of levels. You can make the Keep taller by telling “CreateKeep” to use more levels.

The “CreateKeep” function also punches a hole in the ground floor wall to form a doorway.

The function “CreateWindows” adds windows to all sides of the Keep. Be careful you don’t fall out!

In general the Python API is really easy to use. The hard bit is working out the x,y,z co-ordinates so that blocks end up in the correct place.

Future Upgrades

The obvious thing that is missing is a way of getting from one Keep floor level to another. This really needs a new function to create a hole in the floor with some steps which can be called for each level as it is created in the “CreateKeep” function.