Imagine you’re building your house in Minecraft, minding your own business, when suddenly you’re teleported high in the air. You’re forced to compete in the Bedrock Challenge. If you fail to get to the other side of the arena, you plummet to your doom! Have a go at coding your own Bedrock Challenge, see if your friends can complete it, and have a go yourself.
The full article can be found in The MagPi 43 and was written by Ozzy Hayler-Goodall
The Bedrock Challenge is a game where the goal is to get to the other side of the invisible bedrock grid without falling down the holes. The catch is that there’s a wooden roof so you can’t see the light shining through the holes onto the ground, where invisible bedrock would cast a shadow.
You can’t cheat by jumping or flying because if you do, you get teleported back to the start. The only help you get is an LED which will light up if there’s a hole in any of the nine blocks around you, but not the one you’re on.
We use the Python library PyAutoGUI, which can cause a key to be pressed down or released. We also use another library called ‘threading’, which lets us run something while other parts of the program are working. Other libraries are used, but we don’t have to install them because they are standard Python libraries.
Get prepared
Here’s how you install PyAutoGUI. Open a terminal and type the following into it:
sudo pip3 install pyautogui
Next, install threading:
sudo pip3 install threading
Finally, python3-xlib, as required by PyAutoGUI:
sudo apt-get install python3-xlib
Now we’re on to the wiring: you need to connect the LED to the Raspberry Pi. Build the circuit as shown below:
To test it, open a terminal and type python3, then enter the following lines:
from gpiozero import LED led = LED(13) led.on()
This should turn on the LED assigned to GPIO pin 13.
Modding Minecraft
Now we’ve done all our preparations, let’s get coding! In this code, we have two functions: one stops you cheating while the other creates the holes. There are two main ways of cheating, and we block both of these. One way is to hold down the Shift key (‘enable sneak’), which normally stops you falling. We use PyAutoGUI to hold the Shift key up to prevent you from using sneak. The other way is to fly to the finish. We stop this by finding your Y position at the start, then checking if it has changed - in other words, if you’ve gone higher. It also won’t allow you to run around the perimeter walls. The function that stops you cheating has another purpose: it turns the LED on if there’s air around you. We do this by finding the positions of all nine blocks, then we see if one of them is air. If one or more is air, we turn the LED on. If you do sadly fall down a hole, ‘Uh-Oh’ gets displayed and you’ll be teleported back to the start.
Here’s how we create the arena. We first build the invisible bedrock plane, then we pick random positions to place the holes. After that, we build the roof, and then finally we create the wool walls. To give you a time limit to reach the other end we have a counter, which gets displayed on the Minecraft screen when it reaches a multiple of ten. When it reaches zero, we turn all the invisible bedrock into air. Game over!
You could make it easier by giving the player more time, or make it harder and have more holes. If you have an annoying older brother, they might find some other sneaky ways to cheat. Can you think of any? If you can, try to write code to prevent them!
Code listing
Download bedrockchallenge.py or copy it from here:
from gpiozero import LED from mcpi.minecraft import Minecraft import time, sys, random, threading import pyautogui as pag led = LED(13) mc = Minecraft.create() running = True def make_holes(num, x, y, z): # make the holes for I in range(num): rx = random.randint(2,49) rz = random.randint(2,49) mc.setBlocks(x+rx, y+20, z+rz, x+rx, y+22, z+rz, 0) def monitor(starting_pos): global running LED = [(-1, -1, 1), (-1, -1, 0), (-1, -1, -1), ( 0, -1, -1), (1, -1, -1), (1, -1, 0), (1, -1, 1), (0, -1, 1)] y_start = starting_pos.y while running: pag.keyUp('shift') pos = mc.player.getTilePos() for p in LED: boss = mc.getBlock(pos.x+p[0], pos.y+p[1], pos.z+p[2]) if boss==0: led.on() # LED on else: led.off() # LED off if pos.y > y_start: mc.postToChat ('cheat') mc.player.setPos( starting_pos.x, pos.y, starting_pos.z) # teleport after cheating time.sleep(1) if pos.y < y_start: mc.postToChat ('Uh-oh') time.sleep(3) mc.player.setPos( starting_pos.x, starting_pos.y, starting_pos.z) # move after cheating if pos.z==starting_pos.z+51: mc.postToChat('Well Done') running = False try: pos = mc.player.getTilePos() mc.postToChat( 'get ready for the bedrock challenge') time.sleep(1) mc.postToChat( 'get to the other side without falling down the holes!') time.sleep(3) mc.setBlocks( pos.x+1, pos.y+20, pos.z, pos.x+53, pos.y+22, pos.z+53, 35) #wool mc.setBlocks( pos.x+2, pos.y+20, pos.z+1, pos.x+52, pos.y+20, pos.z+52, 95)#bedrock mc.setBlocks( pos.x+1, pos.y+30, pos.z, pos.x+53, pos.y+30, pos.z+53, 17) #roof mc.setBlocks( pos.x+2, pos.y+21, pos.z+1, pos.x+52, pos.y+22, pos.z+52, 0) # air for Steve starting position mc.setBlocks( pos.x+25, pos.y+21, pos.z+1, pos.x+25, pos.y+22, pos.z+1, 0) mc.player.setPos( pos.x+25, pos.y+21, pos.z+1) # teleport to start newpos = mc.player.getTilePos() # get new position t1 = threading.Thread(target = monitor, args = (newpos, )) t1.start() make_holes(250, pos.x, pos.y, pos.z) time_start = time.time() counter = 80 # setting the timer while time.time()<time_start+80: # starting the timer time.sleep(1) counter-=1 if counter%10==0: mc.postToChat(str(counter)) mc.setBlocks( pos.x+2, pos.y+20, pos.z+1, pos.x+52, pos.y+20, pos.z+52, 0) mc.postToChat('GAME OVER') running = False except KeyboardInterrupt: # type Ctrl+C print('bye') running = False sys.exit()