Presentation is loading. Please wait.

Presentation is loading. Please wait.

Hacking Minecraft on the Raspberry Pi using Python

Similar presentations


Presentation on theme: "Hacking Minecraft on the Raspberry Pi using Python"— Presentation transcript:

1 Hacking Minecraft on the Raspberry Pi using Python
Lesson 5

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: Use Python to freeze water when you walk on it Most of you will: Use a Pibrella to make a light and buzzer sound when you freeze the water Change the block types changed Some of you will: Change the block from sand to grass only when the Pibrella moisture sensor is in water

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 import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create()
You need to add a loop which keeps checking to see if you are stood on water import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create()

6 Did you remember the capital T and the :
Did you get it correct? Did you remember the capital T and the : We need to declare the water (Block ID 9) and ice (Block ID 79) import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() while True:

7 Did you remember to indent after a line of code ending in :
Did you get it correct? Did you remember to indent after a line of code ending in : We can now use the words water and ice to represent these blocks import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() while True: water = ice = 79

8 We now need to get the players position. We will declare it as pos
We can now declare the player’s pos.x, pos.y and pos.z as just x,y and z to make our coding easier import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() while True: water = ice = pos = mc.player.getPos()

9 Our current coordinates are x,y,z
Did you get it correct? Our current coordinates are x,y,z What are the coordinates of the block one below our feet? import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() while True: water = ice = pos = mc.player.getPos() x = pos.x y = pos.y z = pos.x

10 the coordinates of the block one below our feet is
Did you get it correct? the coordinates of the block one below our feet is (x,y-1,z)

11 We can get this block ID by using the line of code
block = mc.getBlock(x,y-1,z) We have now declared it as ‘block’ import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() while True: water = ice = pos = mc.player.getPos() x = pos.x y = pos.y z = pos.x block = mc.getBlock(x,y-1,z)

12 We will now need to add a line of code using ‘if’ to see if the block under our feet, now declared as ‘block’ is the same as == to the water block ID now declared as ‘water’ import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() while True: water = ice = pos = mc.player.getPos() x = pos.x y = pos.y z = pos.x block = mc.getBlock(x,y-1,z)

13 Did you remember to indent it?
Did you get it correct? Did you remember to indent it? Did you remember the : at the end of an if line of code import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() while True: water = ice = pos = mc.player.getPos() x = pos.x y = pos.y z = pos.x block = mc.getBlock(x,y-1,z) if block == water:

14 Now add a line of code to set down an ice block in place of the water
Can you explain this line of code? import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() while True: water = ice = pos = mc.player.getPos() x = pos.x y = pos.y z = pos.x block = mc.getBlock(x,y-1,z) if block == water: mc.setBlock(x,y-1,z,ice)

15 Press F5 to save and run the program
When you walk over water it should now change to ice

16 co-ordinates. The co-ordinates are given as x, y and
What you have learned getBlock() The getBlock() function finds the type of a block at certain co-ordinates. The co-ordinates are given as x, y and z variables. The function returns the block type at those co-ordinates. if statement An if statement will only run a section of code when a condition is True. When the condition is False, the section of code will not run. For example in our program the block below the player will only turn to ice if it is water. Equal to (==) The equal to operator checks whether one value is the same as the other. In our program we use an equal to operator with an if statement to check whether the block below the player is water.

17 Challenge 1 We will now get a Pibrella green light to come on if a water block is changed to ice. The green light must go off again 0.1 seconds later import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() while True: water = ice = pos = mc.player.getPos() x = pos.x y = pos.y z = pos.x block = mc.getBlock(x,y-1,z) if block == water: mc.setBlock(x,y-1,z,ice)

18 Challenge 1 Did you get it correct?
import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() import time while True: water = ice = pos = mc.player.getPos() x = pos.x y = pos.y z = pos.x block = mc.getBlock(x,y-1,z) if block == water: mc.setBlock(x,y-1,z,ice) pibrella.light.green.on() time.sleep(0.1) pibrella.light.green.off()

19 Challenge 1 Now make the buzzer sound at the same time with a frequency of 1000 to represent water freezing and then go off import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() import time while True: water = ice = pos = mc.player.getPos() x = pos.x y = pos.y z = pos.x block = mc.getBlock(x,y-1,z) if block == water: mc.setBlock(x,y-1,z,ice) pibrella.light.green.on() time.sleep(0.1) pibrella.light.green.off()

20 Challenge 1 Did you get it correct?
import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() import time while True: water = ice = pos = mc.player.getPos() x = pos.x y = pos.y z = pos.x block = mc.getBlock(x,y-1,z) if block == water: mc.setBlock(x,y-1,z,ice) pibrella.light.green.on() minecraft.buzzer.buzz(1000) time.sleep(0.1) pibrella.light.green.off() pibrella.buzzer.off()

21 Challenge 2 Can you change the code so that when you stand on sand (Block ID 12) it changes to grass (Block ID 2)

22 Challenge 2 Did you get it correct?
import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() import time while True: sand = grass = pos = mc.player.getPos() x = pos.x y = pos.y z = pos.x block = mc.getBlock(x,y-1,z) if block == sand: mc.setBlock(x,y-1,z,grass) pibrella.light.green.on() minecraft.buzzer.buzz(1000) time.sleep(0.1) pibrella.light.green.off() pibrella.buzzer.off()

23 Challenge 3 You need the moisture sensor to change sand to grass. Add a moisture sensor to the Pibrella on input A. Change the code so that the sand only changes to grass if the moisture sensor is in water

24 Did you get it correct? Did you remember the : and indent correctly
Challenge 3 Did you get it correct? Did you remember the : and indent correctly import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() import time while True: if pibrella.input.a.read() : sand = grass = pos = mc.player.getPos() x = pos.x y = pos.y z = pos.x block = mc.getBlock(x,y-1,z) if block == sand: mc.setBlock(x,y-1,z,grass) pibrella.light.green.on() minecraft.buzzer.buzz(1000) time.sleep(0.1) pibrella.light.green.off() pibrella.buzzer.off()


Download ppt "Hacking Minecraft on the Raspberry Pi using Python"

Similar presentations


Ads by Google