Skip to content

RPi Minecraft Cannon

Goal:

Using Minecraft’s API and the Python programming language, spawn a cannon that’s orientation and launch trajectory can be adjusted in real time.

What You Will Learn:

  • Introduction to Github

What is GitHub?

GitHub is an online repository that offers version control.  It is most often used for computer code and interacted with using a command line tool called git.

Getting Started:

Open a terminal either using the icon or the keyboard shortcut CTRL+ALT+t.When opening the terminal like this we are placed in the home directory.  In RPi: Minecraft + Python Introduction we created a directory named code in the home directory.  We will now navigate into that directory using cd (change directory). Enter the following into the terminal.cd codeYou may have noticed that the prompt that was previouslypi@raspberrypi:~ $Has now becomepi@raspberrypi:~/code $It is informing you of the directory you are currently in, also known as the current working directory.  Remember that ~ (tilde) refers to the home directory.

 

Now that we are in our code directory, lets use git to download the cannon code from this GitHub repository

git clone https://github.com/martinohanlon/minecraft-cannon.git

Now the code has been cloned from the specified github repository into the current working directory.  Lets inspect ours with the ls (lists all files and directories)

ls

You should notice that a new directory “minecraft-cannon” has been created.  Lets navigate into the directory using cd and then inspect the contents using ls

cd minecraft-code

ls

There are several files and directories here, feel free to explore them using cd.  

Note that,

cd ..

will move your current working directory “up” one or backwards into the directory containing the current directory.

At this point we need to launch Minecraft and get into a game.

Once Minecraft is open, click Start Game then join a preexisting world or Create New.  

Back in the terminal we want Python to execute the “minecraft-cannon.py”.  If you left the directory we downloaded with git you can get back there with the following

cd ~/code/minecraft-cannon

Then,

python minecraft-cannon.py

this instructs the Raspberry Pi to use Python to run the “minecraft-cannon.py” file.

Your prompt in the terminal should have changed to

Stuffaboutcode.com Cannon >>

You can now use the following list of special commands to create, aim and fire a cannon in the running Minecraft game.

  • start
  • rotate [0-360]
  • tilt [0-90]
  • fire
  • exit

Start  spawns a cannon, exit an active cannon.  Using start multiple times in a row will construct multiple cannons however only the most recently made cannon can be controlled with the other commands.  You do not need to include the square brackets when typing the commands. 

Whats Next?

  • RPi: Minecraft Clock
  • RPi: Minecraft Snake

Article adapted from source

Back To Top