Building A Castle In Minecraft With Python

5

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.

Minecraft Python Castle #0

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.

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 use wget :

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 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.

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.

Share.

5 Comments

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

  1. Hey i have changed your code a bit to make the castle a bit cooler (just the blocks used) Instead of water it uses lava and wood it uses glow stone so it lights up the keep. new code will be copy and pasted from my pi (username BRYANPI)

  2. Weird! MC ran very slowly – unplayably slowly – for about 5 mins after running the script, and the went back to normal speed. During this time I exited MC and restarted it. It normally says “Generating World. Building Terrain” when I start playing, it then displayed a new message I haven’t seen before: “Chunking…”
    Any ideas what caused that? Anyway it seems to be OK again now!

Leave A Reply