Skip to content

Instantly share code, notes, and snippets.

@topshed
Last active November 8, 2018 16:06
Show Gist options
  • Save topshed/5ea8622f01930790da5876b4f22d4216 to your computer and use it in GitHub Desktop.
Save topshed/5ea8622f01930790da5876b4f22d4216 to your computer and use it in GitHub Desktop.

Note that I spoke to the creator of this project who said that they were a few bugs in the original code as published. This gist uses the updated, fixed code.

What's needed

A Pi with Minecraft installed

How would the participants interact?

There should be a Minecraft window open and an editor with the almost-complete code alongside. There should be two tabs open in the editor.

Tab 1: Playground

#import all the necessary modules
from mcpi.minecraft import Minecraft
from mcpi import block

#connect with the minecraft world
mc=Minecraft.create()

Tab 2: TNTRun Starting code - reset button should return to this point.

#import all the necessary modules
from mcpi.minecraft import Minecraft
from mcpi import block
import time

#connect with the minecraft world
mc=Minecraft.create()

#get the players position
pos=mc.player.getTilePos()

#check if the end of the world will engulf your TNT creation and will then move you if you are to close
if pos.z<-20:
    mc.postToChat('teleporting to safer distance in progress!')
    mc.player.setPos(pos.x,pos.y,-20)
    pos=mc.player.getTilePos()


#mark were the teleport is
zpos=pos.z-40

#create the valley by hollowing it out with air
mc.setBlocks(pos.x-2,pos.y+3,pos.z,pos.x+2,pos.y-7,pos.z-100,block.AIR.id)

mc.setBlocks(pos.x,pos.y-1,pos.z,pos.x,pos.y-7,pos.z,block.BEDROCK_INVISIBLE.id)
mc.setBlocks(pos.x-1,pos.y-1,pos.z,pos.x,pos.y-7,pos.z,block.BEDROCK_INVISIBLE.id)
mc.setBlocks(pos.x+1,pos.y-1,pos.z,pos.x,pos.y-7,pos.z,block.BEDROCK_INVISIBLE.id)
mc.setBlocks(pos.x,pos.y-1,pos.z-88,pos.x-1,pos.y-7,pos.z-88,block.BEDROCK_INVISIBLE.id)
mc.setBlocks(pos.x-1,pos.y-1,pos.z-88,pos.x,pos.y-7,pos.z-88,block.BEDROCK_INVISIBLE.id)
mc.setBlocks(pos.x+1,pos.y-1,pos.z-88,pos.x,pos.y-7,pos.z-88,block.BEDROCK_INVISIBLE.id)
mc.setBlocks(pos.x,pos.y,pos.z,pos.x,pos.y-7,pos.z-92,block.BEDROCK_INVISIBLE.id)

#build the bomb
mc.setBlocks(pos.x,pos.y,pos.z,pos.x,pos.y,pos.z-88,block.TNT.id)

#build the end podium
mc.setBlocks(pos.x-2,pos.y,pos.z-93,pos.x+2,pos.y,pos.z-97,block.GLOWING_OBSIDIAN.id)
mc.setBlocks(pos.x-1,pos.y+1,pos.z-94,pos.x+1,pos.y+1,pos.z-96,block.NETHER_REACTOR_CORE.id,1)
mc.setBlock(pos.x,pos.y+2,pos.z-95,block.REDSTONE_ORE.id)

#setting how many teleports you have
teleport=1

#build the display teleport signal block
mc.setBlock(pos.x+1,pos.y+1,pos.z-44,block.NETHER_REACTOR_CORE.id,2)
mc.setBlock(pos.x-1,pos.y+1,pos.z-44,block.NETHER_REACTOR_CORE.id,2)

#teleport player when at a certain position
while teleport ==1:
    pos=mc.player.getTilePos()
    if pos.z==zpos:
        mc.player.setPos(pos.x,pos.y,pos.z)
        teleport=0

Imgur

Instructions

One of the many amazing things about Raspberry Pis is that they have their own edition of Minecraft for free; what’s even better is you can code it in Python using the Minecraft API! The code on the left in the TNTRun tab creates a game in which the player starts at one end of a long line of TNT and has to make it to the safe area without the TNT exploding in their face. To start the game, the player hits the first TNT block on the path (there is no need for flint and steel in the Raspberry Pi Minecraft Universe).

But the code isn't quite finished yet. If you run the code in the TNTRun tab, the path of TNT will appear but it won't explode if the player strikes it.

Step 1 - Make the TNT explode

  1. You can place a single block at a given set of coordinates with mc.setBlock():
x, y, z = mc.player.getPos()
mc.setBlock(x+1, y, z, block.STONE.id)

Imgur

This will place a stone block should appear beside where you’re standing. Try this out by adding these lines to the code in the "playground" tab.

  1. There are some blocks which have extra properties, such as Wool which has an extra setting you can specify the colour.
x, y, z = mc.player.getPos()
mc.setBlock(x+1, y, z, block.WOOL.id, 4)

Imgur

Here the fifth parameter 4 sets the wool colour to yellow. Without the fifth parameter it is set to the default (0) which is white. Try this out.

  1. To make TNT explosive, add an extra parameter 1.
x, y, z = mc.player.getPos()
mc.setBlock(x+1, y, z, block.TNT.id, 1)
  1. can you use this to fix the code in the TNTRun tab so that it explodes?

Move to a clear space in the world and try it out. Run the code, strike the first block in the TNT path and then start running!

Imgur

Step - Teleportation

  1. It's quite hard to outrun the explosion. Let's make it a little easier by adding a teleporter to the course that will whisk the player closer to safety if they manage to reach it.

You can specify a particular location to teleport to.

x, y, z = mc.player.getPos()
mc.player.setPos(x, y+100, z)

This will transport your player to 100 spaces in the air. This will mean you’ll teleport to the middle of the sky and fall straight back down to where you started.

Try teleporting to somewhere else by modifying the code in the Playground tab.

  1. In the TNTRun, the teleporter is not working correctly as it doesn't actually move the player anywhere. Can you find the line that teleports the player and fix it so that it moves you 24 blocks away in the direction you're running?

Final code

#import all the necessary modules
from mcpi.minecraft import Minecraft
from mcpi import block
import time

#connect with the minecraft world
mc=Minecraft.create()

#get the players position
pos=mc.player.getTilePos()

#check if the end of the world will engulf your TNT creation and will then move you if you are to close
if pos.z<-20:
    mc.postToChat('teleporting to safer distance in progress!')
    mc.player.setPos(pos.x,pos.y,-20)
    pos=mc.player.getTilePos()


#mark were the teleport is
zpos=pos.z-40

#create the valley by hollowing it out with air
mc.setBlocks(pos.x-2,pos.y+3,pos.z,pos.x+2,pos.y-7,pos.z-100,block.AIR.id)

#build the invisible bedrock support
mc.setBlocks(pos.x,pos.y-1,pos.z,pos.x,pos.y-7,pos.z,block.BEDROCK_INVISIBLE.id)
mc.setBlocks(pos.x-1,pos.y-1,pos.z,pos.x,pos.y-7,pos.z,block.BEDROCK_INVISIBLE.id)
mc.setBlocks(pos.x+1,pos.y-1,pos.z,pos.x,pos.y-7,pos.z,block.BEDROCK_INVISIBLE.id)
mc.setBlocks(pos.x,pos.y-1,pos.z-88,pos.x-1,pos.y-7,pos.z-88,block.BEDROCK_INVISIBLE.id)
mc.setBlocks(pos.x-1,pos.y-1,pos.z-88,pos.x,pos.y-7,pos.z-88,block.BEDROCK_INVISIBLE.id)
mc.setBlocks(pos.x+1,pos.y-1,pos.z-88,pos.x,pos.y-7,pos.z-88,block.BEDROCK_INVISIBLE.id)
mc.setBlocks(pos.x,pos.y,pos.z,pos.x,pos.y-7,pos.z-92,block.BEDROCK_INVISIBLE.id)

#build the bomb
mc.setBlocks(pos.x,pos.y,pos.z,pos.x,pos.y,pos.z-88,block.TNT.id,1)

#build the end podium
mc.setBlocks(pos.x-2,pos.y,pos.z-93,pos.x+2,pos.y,pos.z-97,block.GLOWING_OBSIDIAN.id)
mc.setBlocks(pos.x-1,pos.y+1,pos.z-94,pos.x+1,pos.y+1,pos.z-96,block.NETHER_REACTOR_CORE.id,1)
mc.setBlock(pos.x,pos.y+2,pos.z-95,block.REDSTONE_ORE.id)

#setting how many teleports you have
teleport=1

#build the display teleport signal block
mc.setBlock(pos.x+1,pos.y+1,pos.z-44,block.NETHER_REACTOR_CORE.id,2)
mc.setBlock(pos.x-1,pos.y+1,pos.z-44,block.NETHER_REACTOR_CORE.id,2)
#teleport player when at a certain position
while teleport ==1:
    pos=mc.player.getTilePos()
    if pos.z==zpos:
        mc.player.setPos(pos.x,pos.y,pos.z-24)
        teleport=0

Resetting

In addition to replacing the modified code files with the starting code, the Minecraft world will need to be restored to a clean version or simply re-created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment