Feel like building a Minecraft castle using the Python API? If so then take a look at my Python script. You can build a castle in your Minecraft world complete with a keep, walls, battlements and a moat.
Here is the finished castle:
Setup
To use the castle building script below you will need to have Minecraft and the Python API setup. If you don’t have Minecraft listed under the “Games” menu then you can install using the following commands:
sudo apt-get update
sudo apt-get install minecraft-pi
Download Minecraft Castle Script
You can download the script directly to your Pi using:
wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/minecraft/castle.py
Build the Minecraft Castle
With Minecraft running you position your character where you want the castle to be built. Press the Tab key to free your mouse cursor and in a terminal window run the following command:
python3 castle.py
The time it takes to build will depend on the Raspberry Pi model you are using. It may take up to 30 seconds on an original Model B but is much faster on a Pi 3 or Pi 4.
How it Works
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.
In general operations are based around a starting point (x, y & z). X and Z are the North/South & East/West. Z is up and down. Walls are built the the left and right of the starting block so a wall width of “21” will result in a wall 21+1+21 wide. Whereas a height of 10 will simply result in a wall 10 blocks high.
The “CreateLandscape” function prepares the ground for the castle.
The perimeter walls and the keep walls are all created using the 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 at the top.
A loop creates floors inside of the keep splitting it up into a number of rooms. You can make the Keep taller by increasing the “keepFloors” variable.
More loops add windows, stairs and a torch to each floor.
The “CreateKeep” function also punches a hole in the ground floor wall to form a doorway.
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.
Variables
The script has a block of variables that you can tweak:
keepFloors=4
keepSize=5
outerWallSize=21
outerWallHeight=5
innerWallSize=13
innerWallHeight=6
moatDepth=5
moatWidth=5
The “size” variables are the number of blocks either side of the starting point. So they will result in a wall that is double the value plus one wide. So a “keepSize” of 5 will result in a keep that is 11 blocks wide.
The “height” variables will result in a height equal to that number of blocks.
11 Comments
Thanks, this is exactly the sort of thing I was looking to do with my sons over the summer holidays.
Great idea! As for the solution for getting from one floor to the next, you might simply consider ladders.
That’s probably a better idea than messing around with stairs. I think I’ll implement ladders when I get a chance.
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)
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!
Whenever I launch the code it tells me that there is a parenthesis syntax in the print section
Sounds like you are using Python 3 but my script contained Python 2 style Print statements. I’ve updated the script so it works with both Python 2 and Python 3. Download it again and it should work OK.
how do i use this on raspberry pi using python 3
In principle it should work on any model of Pi using Python 2. It will probably require some modification to the print statements to work on Python 3. I’ll update when I get the chance.
Hi, Is it possible to change the starting position?
I’ve managed it with the Pyramid build, struggling with this. 🙂
Hi this is awesome. Thanks!