Download presentation
Presentation is loading. Please wait.
Published byIvan Kurniawan Modified over 6 years ago
1
Hacking Minecraft on the Raspberry Pi using Python
Lesson 1
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 teleport to a new location in Minecraft. Most of you will: Choose a new location of your own to teleport to Some of you will: Teleport when using a button and make a light come on to show that you have teleported
5
Minecraft Coordinates x is left and right y is up and down
z is backwards and forwards
6
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()
7
Add in the xyz coordinates of where we will teleport to
import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() x = 10 y = 11 z = 12
8
Now tell the program to teleport there
import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() x = 10 y = 11 z = 12 mc.player.setPos(x, y, z) The way that code is written is called syntax Try to explain the syntax mc.player.setPos(x, y, z)
9
Did you get it correct? mc.player.setPos(x, y, z)
We always start big and then get smaller (like delivering a letter to a house ) e.g. go to Merseyside, then St Helens, then Festive Avenue then number 52, then Mr Benn In code this would be merseyside.sthelens.festiveavenue.52.mrbenn and the dots means ‘next’ In Minecraft (mc for short), the player, set their position, to x,y,z This becomes mc.player.setPos(x, y, z)
10
Press F5 to save and run the program
You should teleport to your new location. If you get an error try to troubleshoot it or ask a friend to try to spot your mistake
11
What you have learned Variable A variable stores a value. For example in our program we created three variables, x, y and z, which stored the values 10, 11 and 12 respectively. You can change the values 10, 11 and 12 to any number that you want. When we used setPos() we were using the variables x, y and z. Integers Integers are whole numbers, for example 10, 347 and 59. We use integers to set the values of variables when we want to use whole numbers. In this program we use the integers 10, 11 and 12. Function A function is a reusable piece of code that performs a specific task. e.g. the pre-written function setPos() to change the position of the player Function arguments Some functions need to be given data in order to work, this data is called an argument e.g. we gave the argument x, y and z to the setPos() in order to tell it where to teleport the player to.
12
Change the x, y and z coordinates to new values.
Challenge 1 Change the x, y and z coordinates to new values. Teleport to your new location Challenge 2 Insert some of the following code to make the green light on the Pibrella come on when you teleport import pibrella pibrella.light.green.on()
13
Did you get it correct? import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() import pibrella x = 10 y = 11 z = 12 mc.player.setPos(x, y, z) pibrella.light.green.on
14
You will need these new lines of code import time time.sleep(3)
Challenge 3 Make the red light on when you run the program but then change to a green light 3 seconds later You will need these new lines of code import time time.sleep(3) pibrella.red.light.off()
15
Did you get it correct? import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() import pibrella import time x = 10 y = 11 z = 12 mc.player.setPos(x, y, z) pibrella.light.red.on() time.sleep(3) pibrella.light.green.on() pibrella.light.red.off()
16
Make the Pibrella button teleport you.
Challenge 4 Make the Pibrella button teleport you. Make the red light until you press the teleport button The red light then goes off and the green light comes on You will need these new lines of code while True: if pibrella.button.read(): Remember capital T on True : at the end of while True loop : at the end of if After lines with a : you need to indent your code
17
Did you get it correct? When do you need a :
import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() import pibrella import time x = 10 y = 11 z = 12 pibrella.light.red.on() while True: if pibrella.button.read() mc.player.setPos(x, y, z) pibrella.light.red.off() pibrella.light.green.on() When do you need a : When do you need indentation Why do you need a while True loop? Why do you need an if
18
You can refine this code
import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() import pibrella import time x = 10 y = 11 z = 12 while True: if pibrella.button.read() mc.player.setPos(x, y, z) pibrella.light.red.off() pibrella.light.green.on() time.sleep(3) pibrella.light.green.off() else: pibrella.light.red.on) Try to explain this code to say why it is better
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.