hacking minecraft on the raspberry pi using python lesson 5 1

24
Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

Upload: duane-lee

Post on 19-Jan-2016

266 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

Hacking Minecraft on the Raspberry Pi using Python

Lesson 5

1

Page 2: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

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)

2

Page 3: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

3

Objective of the lessonUse 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

Page 4: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

Type the following into the editorYou 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 minecraftmc = minecraft.Minecraft.create()

Page 5: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

You need to add a loop which keeps checking to see if you are stood on water

import mcpi.minecraft as minecraftmc = minecraft.Minecraft.create()

Page 6: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

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 minecraftmc = minecraft.Minecraft.create()while True:

Page 7: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

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 minecraftmc = minecraft.Minecraft.create()while True: water = 9 ice = 79

Page 8: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

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 minecraftmc = minecraft.Minecraft.create()while True: water = 9 ice = 79 pos = mc.player.getPos()

Page 9: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

Did you get it correct?

Our current coordinates are x,y,zWhat are the coordinates of the block one below our feet?

import mcpi.minecraft as minecraftmc = minecraft.Minecraft.create()while True: water = 9 ice = 79 pos = mc.player.getPos() x = pos.x y = pos.y z = pos.x

Page 10: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

Did you get it correct?the coordinates of the block one below our feet is

(x,y-1,z)

Page 11: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

We can get this block ID by using the line of codeblock = mc.getBlock(x,y-1,z)

We have now declared it as ‘block’

import mcpi.minecraft as minecraftmc = minecraft.Minecraft.create()while True: water = 9 ice = 79 pos = mc.player.getPos() x = pos.x y = pos.y z = pos.x block = mc.getBlock(x,y-1,z)

Page 12: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

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 minecraftmc = minecraft.Minecraft.create()while True: water = 9 ice = 79 pos = mc.player.getPos() x = pos.x y = pos.y z = pos.x block = mc.getBlock(x,y-1,z)

Page 13: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

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 minecraftmc = minecraft.Minecraft.create()while True: water = 9 ice = 79 pos = mc.player.getPos() x = pos.x y = pos.y z = pos.x block = mc.getBlock(x,y-1,z) if block == water:

Page 14: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

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 minecraftmc = minecraft.Minecraft.create()while True: water = 9 ice = 79 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)

Page 15: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

Press F5 to save and run the programWhen you walk over water it should now change

to ice

Page 16: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

What you have learnedgetBlock() The getBlock() function finds the type of a block at certain

co-ordinates. The co-ordinates are given as x, y andz 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 ofcode 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 usean equal to operator with an if statement to check whether

the block below the player is water.

Page 17: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

Challenge 1We 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 minecraftmc = minecraft.Minecraft.create()while True: water = 9 ice = 79 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)

Page 18: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

Challenge 1Did you get it correct?

import mcpi.minecraft as minecraftmc = minecraft.Minecraft.create()import timewhile True: water = 9 ice = 79 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()

Page 19: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

Challenge 1Now 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 minecraftmc = minecraft.Minecraft.create()import time while True: water = 9 ice = 79 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()

Page 20: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

Challenge 1Did you get it correct?

import mcpi.minecraft as minecraftmc = minecraft.Minecraft.create()import time while True: water = 9 ice = 79 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()

Page 21: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

Challenge 2Can you change the code so that when you stand on sand (Block

ID 12) it changes to grass (Block ID 2)

Page 22: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

Challenge 2Did you get it correct?

import mcpi.minecraft as minecraftmc = minecraft.Minecraft.create()import timewhile True: sand = 12 grass = 2 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()

Page 23: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

Challenge 3You 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

Page 24: Hacking Minecraft on the Raspberry Pi using Python Lesson 5 1

Challenge 3Did you get it correct? Did you remember the : and indent correctly

import mcpi.minecraft as minecraftmc = minecraft.Minecraft.create()import time while True: if pibrella.input.a.read() : sand = 12 grass = 2 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()