# Minecraft Trampoline

In this post we will use Python 2 and Minecraft to create a virtual trampoline for Minecraft. We will first build the trampoline using Minecraft blocks (in Python). Then we will write a routine to make the main player bounce up and down on the trampoline.

#### Step 1: Let’s connect to the Minecraft World via our Python script

For this challenge we are using a Raspberry Pi with Minecraft and Python 2 pre-installed.

```from mcpi import minecraft, block
import time

mc = minecraft.Minecraft.create()

msg = "Hello Minecraft World from 101 Computing"
mc.postToChat(msg)```

Now try this, just to make sure it works! You should see a message appearing on your minecraft screen. If you are not sure how to do this you may want to check our first minecraft + python tutorial.

#### (x,y,z) coordinates?

Before getting started you need to understand how the (x,y,z) coordinates are used to retrieve the position of the player in the minecraft world or to add blocks at the right position. As you can see on the picture to the left, it is slightly confusing as the vertical axis is not the z axis but the y axis.

#### Step 2: Building the trampoline

Our trampoline will be quite basic. It will consists of 4 posts (made of stone blocks) and a 6 by 6 bouncing area made of wood (ouch!).

Before we can build this trampoline we are going to find the position of the player so we can build this trampoline next to our main player (in fact just 5 blocks away on the x axis)

So let’s see how this all works:

```from mcpi import minecraft, block
import time

mc = minecraft.Minecraft.create()
msg = "Hello Minecraft World from 101 Computing"
mc.postToChat(msg)

def buildTrampoline(x , y, z, width):
time.sleep(1)
mc.setBlock(x, y, z, block.STONE)
mc.setBlock(x, y, z+width, block.STONE)
mc.setBlock(x + width, y, pos.z, block.STONE)
mc.setBlock(x+width, y, z+width, block.STONE)
mc.setBlocks(x, y+1, z, x + width, y+1, z + width, block.WOOD)

#Let's get the player's coordinates
pos=mc.player.getPos()

#We will build the trampoline 5 blocks away from the player
trampoline_xPos = pos.x + 5
trampoline_yPos = pos.y
trampoline_zPos = pos.z
trampoline_width = 6

buildTrampoline(trampoline_xPos,trampoline_yPos,trampoline_zPos,trampoline_width)
```

#### Step 3: Make it Bounce!

Let’s first create a new routine to make the player bounce up in the sky using a for loop.

```def bounce():
x, y, z = mc.player.getPos()
for i in range(1,21):
time.sleep(0.01)
mc.player.setPos(x,y+i,z)```

We don’t have to worry about getting the player to go down as it will do so automatically (there is gravity in the minecraft world!).

Then we will use an infinite loop to constantly check on the player’s position. If the player is on the trampoline then we will call our bounce() procedure.

```while True:
x, y, z = mc.player.getPos()
if (x>=trampoline_xPos and x<=(trampoline_xPos+trampoline_width)) and (z>=trampoline_zPos and z<=(trampoline_zPos+trampoline_width)):
block_beneath = mc.getBlock(x, y-1, z)
if block_beneath==17: #17 is wood
bounce()
```

#### Complete script:

```from mcpi import minecraft, block
import time

def buildTrampoline(x , y, z, width):
time.sleep(1)
mc.setBlock(x, y, z, block.STONE)
mc.setBlock(x, y, z+width, block.STONE)
mc.setBlock(x + width, y, pos.z, block.STONE)
mc.setBlock(x+width, y, z+width, block.STONE)
mc.setBlocks(x, y+1, z, x + width, y+1, z + width, block.WOOD)

def bounce():
x, y, z = mc.player.getPos()
for i in range(1,21):
time.sleep(0.01)
mc.player.setPos(x,y+i,z)

mc = minecraft.Minecraft.create()

msg = "Hello Minecraft World from 101 Computing"
mc.postToChat(msg)

#Let's get the player's coordinates
pos=mc.player.getPos()

#We will build the trampoline 5 blocks away from the player
trampoline_xPos = pos.x + 5
trampoline_yPos = pos.y
trampoline_zPos = pos.z
trampoline_width = 6

buildTrampoline(trampoline_xPos,trampoline_yPos,trampoline_zPos,trampoline_width)

mc.postToChat("Climb on the trampoline next to you to start bouncing.")

while True:
x, y, z = mc.player.getPos()
if (x>=trampoline_xPos and x<=(trampoline_xPos+trampoline_width)) and (z>=trampoline_zPos and z<=(trampoline_zPos+trampoline_width)):
block_beneath = mc.getBlock(x, y-1, z)
if block_beneath==17: #17 is wood
bounce()
```

Use the above script to make your trampoline.

Once you have it working adapt this script to create an escalator. The escalator should be built using a Python script. As the soon as the main player goes on the escalator they should automatically move up the stairs, one step at a time.

Did you like this challenge?

Click on a star to rate it!

Average rating 3.4 / 5. Vote count: 7

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: , ,