Download presentation
Presentation is loading. Please wait.
Published byIndra Muljana Modified over 6 years ago
1
Hacking Minecraft on the Raspberry Pi using Python
Lesson 6
2
Starter Switch on your Raspberry Pi. Open Minecraft
Open Idle (not Idle 3) Click on File>New File This opens up the Idle editor where you can write and edit your Python code Open Minecraft>Create New World (Minecraft must be open for you to hack it)
3
Objective of the lesson
Use Python to control a game called Minecraft All of you will: Create a tower of blocks Most of you will: Make a tower by clicking a button which grows higher the longer you press Some of you will: If you release the button the program resets
4
import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create()
Type the following into the editor You always use these lines first in your Minecraft code This connects your code to Minecraft so that you can hack it. Careful, Python code is case sensitive Remember to have Minecraft open) import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create()
5
You need to add a ‘repeat’ which will repeatedly add blocks to the tower
for a in range(50): This repeats 50 times Each time it makes the letter a = 1, then a=2, then a=3 etc until a = 50 import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() for a in range(50):
6
We now need to get the player’s position Add a line of code
for a in range(50): ends in a : so think what you will need to do import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() for a in range(50):
7
Did you get it correct? Define x.pos as x y.pos as y z.pos as z
import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() for a in range(50): pos = mc.player.getPos()
8
Decide on a block ID for your tower and define it as ‘block’
Did you get it correct? Decide on a block ID for your tower and define it as ‘block’ I am going to build in Gold (Block ID 41) import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() for a in range(50): pos = mc.player.getPos() x = x.pos y = y.pos z = z.pos
9
Did you get it correct? Now you need to set a block down
The x coordinate should be x+3 (so that it is to one side of you) The y coordinate should be y+a so that it keeps adding blocks until it reaches height 50 The z coordinate can be z (the same depth backwards as the player import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() for a in range(50): pos = mc.player.getPos() x = x.pos y = y.pos z = z.pos block = 41
10
Did you get it correct? import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() for a in range(50): pos = mc.player.getPos() x = x.pos y = y.pos z = z.pos block = mc.setBlock(x+3,y+a,z,block)
11
Press F5 to save and run the program
When you run it, a tower should be built to the side of Steve
12
We can declare these variables (rename them) as x and y and z by using
What you have learned for a in range(50): The for a in range() function produces a loop which repeats 50 times (although the number of repeats can be changed) and makes a = 1, then a = 2 then a = 3 and so on until a = 50 pos = mc.player.getPos() The pos = mc.player.getPos() function gets the coordinates of where the player is within the game. The position variable is declared and save as ‘pos’. The x coordinate is pos.x and the y coordinate is pos.y and z coordinate is pos.z We can declare these variables (rename them) as x and y and z by using x = pos.x y = pos.y z = pos.z
13
Change the block to be another block e.g. TNT (Block ID 46)
Challenge 1 Change the block to be another block e.g. TNT (Block ID 46) import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() for a in range(50): pos = mc.player.getPos() x = x.pos y = y.pos z = z.pos block = mc.setBlock(x+3,y+a,z,block)
14
Challenge 2 Make it so that the tower is built when the Pibrella button is pressed and a green light and a success buzz sounds and then goes off once the tower is placed
15
Challenge 2 Did you get it correct?
import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() while True: if pibrella.button.read(): pibrella.light.green.on() pibrella.buzzer.success() time.sleep(1) pibrella.light.green.off() pibrella.buzzer.off() for a in range(50): pos = mc.player.getPos() x = x.pos y = y.pos z = z.pos block = mc.setBlock(x+3,y+a,z,block)
16
Challenge 2 Did you remember the while True: Why does it need this?
The if pibrella.button.read(): needs indenting. Why? The for a in range(50): line needs indenting again. Why? Why does the pibrella code need to go outside of the for a in range: repeat? if pibrella.button.read(): pibrella.light.green.on() pibrella.buzzer.success() time.sleep(1) pibrella.light.green.off() pibrella.buzzer.off()
17
Get it so that the longer you press the button, the higher the tower
Challenge 3 Get it so that the longer you press the button, the higher the tower HINTS You will need to declare a new variable to store how long the button is pressed. Call it timepressed You will need to set this to equal 0 at the start of the program If the button is pressed then timepressed needs to increase by 1 every 1 second timepressed is the new value for the height of the tower (which was previously 50
18
Challenge 3 Did you get it correct?
import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() timepressed = 0 while True: if pibrella.button.read(): timepressed = timepressed pibrella.light.green.on() pibrella.buzzer.success() time.sleep(1) pibrella.light.green.off() pibrella.buzzer.off() for a in range(timepressed): pos = mc.player.getPos() x = x.pos y = y.pos z = z.pos block = mc.setBlock(x+3,y+a,z,block)
19
The height of the tower which is ‘timepressed’ needs resetting HINT
Challenge 3 The problem is that the tower does not reset when you move to a new location and press the button again The height of the tower which is ‘timepressed’ needs resetting HINT When the button is pressed the tower builds However if the button is not pressed then timepressed (which is the height of the tower) needs resetting back to equal 0 Remember that a button is either on, or else it is off Remember that if and else are conditions that may or may not be true and need a : and indenting afterwards
20
Challenge 3 Did you get it correct?
import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() timepressed = 0 while True: if pibrella.button.read(): timepressed = timepressed pibrella.light.green.on() pibrella.buzzer.success() time.sleep(1) pibrella.light.green.off() pibrella.buzzer.off() for a in range(timepressed): pos = mc.player.getPos() x = x.pos y = y.pos z = z.pos block = mc.setBlock(x+3,y+a,z,block) else: timepressed = 0
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.