You are going to learn to program in Python and create a game called Lava Trap.
If your Raspberry Pi is at the command line and not the GUI - login, type startx and then press Enter.
Click Menu > Games > Minecraft: Pi Edition to run the game.
Click Start Game, then click Create New (or choose an existing one) to enter a world:
The mouse changes where you look
Holding left button destroys blocks
Right button ‘places’ blocks
1, 2, 3, 4, 5, 6, 7, 8 change what you are holding.
W, S, A, D move you forward, backward, left and right
E opens the inventory
ESC takes you back and to the Menu
Space is jump, double tapping Space makes you fly or stop flying
Using Python and Minecraft: Pi edition we can change the game to do amazing things.
You are going to create mini game - a Lava Pit will instantly appear and Steve will be put at the centre of it, soon through the block he is standing on will disappear so he will have to move, but hang on all the blocks keep disappearing!
The first task is to start your program and get “Welcome to the Lava Trap” to appear on the screen:
Press ESC to go back to the Minecraft menu but leave the game playing.
Open Python IDLE by clicking Menu > Programming > Python 3.
Use File > New Window to create a new program and save it as ‘myprogram.py’.
Type the following code into the program to import the modules you will need.
from mcpi.minecraft import Minecraft
from mcpi import block
Create a connection to Minecraft using the code.
Post a message to the chat window.
Run your program by clicking Run > Run Module.
You should see your message appear in the Minecraft chat window.
An errors will be displayed in the Python Shell in red.
If you get an error check your code carefully.
Capital letters are important Minecraft is different to minecraft.
Next you will use the api to create the pit where the game will be played - when your program runs it will instantly appear under Steve before the game starts.
First update your program so it creates a single block of lava under Steve, by adding the following code:
Put a 3 second delay into your program so that you can see what going on.
Find out where Steve is in the world.
Create a block of lava under Steve.
Run your program by clicking Run > Run Module or by pressing F5.
Your program will wait 3 seconds and then a block of Lava will appear under Steve - he’s going fall in and burn!
Walk somewhere different and run the program again - another Lava block will appear.
Try changing the code to use DIAMOND_BLOCK rather than LAVA and see what happens.
You need to create a lot more lava in order for the game to be a lot more fun - next you will program a large slab of STONE with LAVA on the top:
Use setBlocks() to create an area of STONE 2 blocks below Steve for the LAVA to sit on.
Then create the LAVA under Steve.
Run your program to see the Lava ‘pit’.
Make a DIAMOND_BLOCK platform in the middle for Steve to stand on:
Create the diamond block.
Run your program Steve will be stuck in the middle of the Lava pit.
At the moment unless Steve flies or lays blocks down he can’t get out without getting burned - update your program so he can get out.
Use more setBlocks() commands to make a bridge out of the Lava pit.
You can change Steve’s x,y,z position using mc.player.setTilePos(x, y, z).
If it’s too easy, make the Lava Pit really big - is it still easy?
Update your program to make blocks under Steve disappear:
Post messages to the chat screen to warn the player the game is about to start.
Create a variable called gameover and set it to False - it will be set to True at the end of the game.
Create a loop which will continue until the game is over.
Get Steve’s position.
Turn the block under Steve to OBSIDIAN as a warning and wait for 2 seconds.
After the warning turn the block to AIR, if Steve is standing on it, he’s going to be in the Lava pit.
Run the program, the game will start and you will have to put blocks down in the Lava pit to escape because otherwise they are going to disappear and Steve will fall in.
The game is over if Steve falls into the Lava, you need to modify your program to check if he has fallen into the Lava and put a message on the screen:
Use an if statement to see if Steve’s height (y) is not equal to where he started, if it is set the gameover variable to True.
Put a message on the screen to let the player know they have been caught in the lava trap.
Run your program and see how long you can stay out of the lava.
This game is just the start, can you finish it? Here are some challenges:
Make the game harder?
Make a better game arena, perhaps build a stadium or walls around it so Steve can get out.
Add points to the game, each time Steve doesn’t fall in he gets a point.
Change the game so it starts easy but gets harder the longer you play.
Add a 2 player (or even multiplayer!) option.
Lava Game code made by Martin O'Hanlon.
See full post at https://www.stuffaboutcode.com/2015/09/minecraft-game-tutorial-lavatrap-pycon.html. No changes made.
Licensed under a Creative Commons Attribution-ShareALike 3.0 Unported License.