Do you allow this site to use Cookies?

Programming Minecraft Pi with python to make an explosive TNT path using a maze creating algorithm on the Raspberry Pi.

Whilst looking around the net for some some information on programming I was doing,  I came across a page showing various algorithms used in creating mazes which I found interesting. 

Wanting to give my new found knowledge ago, it made sense to try out a maze algorithm in Minecraft PI.

Minecraft on the Raspberry Pi is fun for being creative but some of the blocks and features available in other versions of Minecraft are not available.
Though on the good side you can program things to happen on your island using the python programming language.

The first efforts was to make a trail of flowers around the landscape. It was interesting to watch it decide where to go but it soon became obvious there was going to more to it than just creating a trail. There is more to a Minecraft landscape than the maze algorithm was designed for.

Minecraft Pi TNT 2

The Maze generating algorithm i had decided to use is called a Recursive Backtracker. At each step the algorithm tests if the current location has been previously visited by the algorithm on any of the four sides. For the un-visited sides it randomly selects one and moves forward.  If the algorithm ends up in a dead end then it back tracks where it came from until it comes across an un-visited side. It then continues along that route until the maze is complete.

I adapted the program so it created a path instead of a Maze. In the Minecraft environment there are a few obstacles such as cliffs, trees, water and lava and the edge of the land, that I would need to avoid as well as navigating the uneven terrain in 3D not 2D.

The rules I decided to apply to the path algorithm was that the path could not:

  • go up or down more than 1 vertical block
  • go into water
  • go into lava
  • go into a tree
  • go into the air
  • go into TNT

One of the blocks that is available in Minecraft Pi is the TNT explosive block, with the slightly disapointing fact that you can't get the TNT to explode in creative mode. Then again you can't create much with TNT other than a big hole. 

The fun fact about the TNT blocks created by a Python script is they can explode. So to put a bit more fun into the path I changed the flowers to exploding TNT blocks.

Minecraft Pi TNT low1

I then set the script off again. Once the TNT path has guided it's self round the landscape, the first TNT block can be set off with a tap. Then quickly fly up high to watch the TNT chain reaction remove a few thousand tones of Minecraft rock.

Minecraft Pi TNT 1

Minecraft Pi TNT 5

As the TNT path is random as it crawls across the landscape, your hours of finely crafted building may get a visit from the explosve footpath. So if you decide to give the code below a try make sure you do it on a deserted island.

The script I wrote is available here: followTNT.py

 To run the script in Mincraft PI. Create a new landscape then press ALT & TAB which will allow you to move the mouse to the desktop.

Open a terminal window. navigate to the folder the followTNT.py script is in. For example cd ~/Desktop.

Then run in python with the command python3 followTNT.py or use Thonney.

Now click on the Minecraft window and watch the TNT Path get created .

 

Examples of Types of Maze Creating Algorithms

https://en.wikipedia.org/wiki/Maze_generation_algorithm

https://www.jamisbuck.org/mazes/


Add comment