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 4

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 make text print to the screen Most of you will: Use code to make a doormat to tell you when you have arrived home Some of you will: Use a Pibrella to make an electronic doorbell when you have arrived home

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 new line of code mc.postToChat (“Hello World”)
import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() mc.postToChat (“Hello World”)

6 Press F5 to save and run the program
Your message should print to the screen Change the message which prints to the screen.

7 We need to find our position. We can use pos = mc.player.getTilePos()
We now want it to print a message to the screen if you arrive home and stand on the doormat We need to find our position. We can use pos = mc.player.getTilePos() to find the coordinates of the tile below the player. This gives a x,y,z as integers (whole numbers) which is better when tying to match up a player’s position against the position of the doormat. Add this line of code in

8 You can get it to print your position using the following code
pos.x is your position along the x axis pos.y is your position along the y axis pos.z is your position along the z axis str stands for string. This converts the x,y,z numbers into text so that it can be printed to the screen import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() pos = mc.player.getTilePos() mc.postToChat (“Your x position is ” +str(pos.x) + “, your y position is ” +str(pos.y) + (“, your z position is ” +str(pos.z) )

9 You now need it to keep updating your position
You will need to - import time - add a while True: Add a time.sleep command depending on how quickly you want your position to update

10 You can get it to print your position using the following code
import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() import time while True: pos = mc.player.getTilePos() mc.postToChat (“Your x position is, ” +str(pos.x) + “, your y position is ” +str(pos.y) + (“, your z position is ” +str(pos.z) ) time.sleep(2)

11 I have added some code. What will it do?
Build a house and identify the x,y,z coordinates of the tile outside of the door (the doormat). Mine is at x=10,y=0,z=10 I have added some code. What will it do? import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() import time while True: pos = mc.player.getTilePos() mc.postToChat (“Your x position is, ” +str(pos.x) + “, your y position is ” +str(pos.y) + (“, your z position is ” +str(pos.z) ) time.sleep(2) mc.postToChat (“Your are looking for x=10, y=0, z=10”) time.sleep(5)

12 We will need an line of code using ‘if’ to see if we are at the doormat position. To check to see if a value is the same as another value held in a variable, we use == Your y coordinate will automatically match if you are stood on the ground. import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() import time while True: pos = mc.player.getTilePos() mc.postToChat (“Your x position is, ” +str(pos.x) + “, your y position is ” +str(pos.y) + (“, your z position is ” +str(pos.z) ) time.sleep(2) mc.postToChat (“Your are looking for x=10, y=0, z=10”) time.sleep(2) if pos.x ==10 and pos.z==10: mc.postToChat (“Your are home”) else: mc.postToChat (“Are you lost?)

13 postToChat() This displays text in the Minecaft game
What you have learned Strings Strings are a kind of data type called text. They have speech marks around them. Same as == This compares a value to a value held in a variable. e.g. Your age is 12. This is a value. I keep asking you is your age 5,6,7 etc which always returns a false value until I ask is your age 12? and then this returns a true value postToChat() This displays text in the Minecaft game

14 pibrella.buzzer.success()
Challenge 1 We will now get a Pibrella doorbell to sound a ‘success’ tune if you land on the doormat. You need to add in the line pibrella.buzzer.success() import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() import time mc.postToChat (“Your x position is, ” +str(pos.x) + “your y position is, ” +str(pos.y) + (“your z position is ” +str(pos.z) ) time.sleep(2) mc.postToChat (“Your are looking for x=10 and z=10”) time.sleep(2) while True: if pos.x ==10 and pos.z==10: mc.postToChat (“Your are home”) else: mc.postToChat (“Are you lost?)

15 We will need an line of code using ‘if’ to see if we are at the doormat position. To check to see if a value is the same as another value held in a variable, we use == Your y coordinate will automatically match if you are stood on the ground. import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() import time while True: pos = mc.player.getTilePos() mc.postToChat (“Your x position is, ” +str(pos.x) + “, your y position is ” +str(pos.y) + (“, your z position is ” +str(pos.z) ) time.sleep(2) mc.postToChat (“Your are looking for x=10, y=0, z=10”) time.sleep(2) if pos.x ==10 and pos.z==10: mc.postToChat (“Your are home”) pibrella.buzzer.success() else: mc.postToChat (“Are you lost?) pibrella.buzzer.fail() time.sleep(2)

16 Did you get it correct? import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() import time import pibrella mc.postToChat (“Your x position is, ” +str(pos.x) + “your y position is, ” +str(pos.y) + (“your z position is ” +str(pos.z) ) time.sleep(2) mc.postToChat (“Your are looking for x=10 and z=10”) time.sleep(2) while True: if pos.x ==10 and pos.z==10: mc.postToChat (“Your are home”) pibrella.buzzer.success() else: mc.postToChat (“Are you lost?) pibrella.buzzer.off()


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

Similar presentations


Ads by Google