Lesson 4 1 Starter Switch on your Raspberry Pi Open Minecraft Open Idle not Idle 3 Click on FilegtNew File This opens up the Idle editor where you can write and edit your Python code ID: 718841
Download Presentation The PPT/PDF document "Hacking Minecraft on the Raspberry Pi us..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Hacking Minecraft on the Raspberry Pi using Python
Lesson 4
1Slide2
Starter
Switch on your Raspberry Pi. Open MinecraftOpen Idle (not Idle 3)
Click on File>New FileThis opens up the Idle editor where you can write and edit your Python codeOpen Minecraft>Create New World (Minecraft must be open for you to hack it)
2Slide3
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
3Slide4
Type the following into the editor
You always use these lines first in your Minecraft codeThis connects your code to Minecraft so that you can hack it. Careful, Python code is case sensitiveRemember to have Minecraft open)
import
mcpi.minecraft as minecraft
mc
= minecraft.Minecraft.create()Slide5
You need to add a new line of codemc.postToChat
(“Hello World”)
import
mcpi.minecraft as minecraft
mc
= minecraft.Minecraft.create
()
mc.postToChat
(“Hello World”)Slide6
Press F5 to save and run the programYour message should print to the screen
Change the message which prints to the screen.Slide7
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()t
o 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 inSlide8
You can get it to print your position using the following code
pos.x
is your position along the x axispos.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
) )Slide9
You now need it to keep updating your positionYou will need to
- import time - add a while True:Add a time.sleep command depending on how quickly you want your position to updateSlide10
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)Slide11
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)Slide12
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?)Slide13
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.
Y
our 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
gameSlide14
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
p
ibrella.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?)Slide15
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)Slide16
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
()