Building A Castle In Minecraft With Python

Once I had Minecraft setup on my Pi and had figured out how to use the 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.

Minecraft Python Castle #0

Setup

To use the script below you will need to have Minecraft and the Python API setup. If you haven’t you may want to take a look at the following articles :

The rest of this article assumes Minecraft is installed in /home/pi/mcpi and the API files are in /home/pi/mcpi-api.

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
#
# http://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 navigate to your Minecraft API directory (mcpi-api in my example) and use wget :

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

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

python castle.py

It should take about 15-20 seconds to complete.

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.

Minecraft Python Castle #4

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.

Minecraft Python Castle #3

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.

Minecraft Python Castle #2

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

Minecraft Python Castle #1

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.

This entry was posted in Minecraft, Python, Tutorials & Help and tagged , . Bookmark the permalink.



3 Responses to Building A Castle In Minecraft With Python

  1. Pops says:

    Thanks, this is exactly the sort of thing I was looking to do with my sons over the summer holidays.

  2. SteveH says:

    Great idea! As for the solution for getting from one floor to the next, you might simply consider ladders.

    • Matt says:

      That’s probably a better idea than messing around with stairs. I think I’ll implement ladders when I get a chance.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>