Presentation is loading. Please wait.

Presentation is loading. Please wait.

Hacking Minecraft on the Raspberry Pi using Python Lesson 2 1.

Similar presentations


Presentation on theme: "Hacking Minecraft on the Raspberry Pi using Python Lesson 2 1."— Presentation transcript:

1 Hacking Minecraft on the Raspberry Pi using Python Lesson 2 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) 2

3 Objective of the lesson Use Python to control a game called Minecraft All of you will: – Use Python to make a tail of flowers in Minecraft. Most of you will: – Change the flowers to a different kind of block and make them closer or further apart Some of you will: – Make a light blink on and off to show that a block has been placed 3

4 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 will be using time. You will need to add a line of code import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create()

6 Did you get it correct? import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() import time We will need a loop. Add in a line of code

7 Did you get it correct? import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() import time while True: Did you remember the capital T Did you remember the :

8 You now need to get the player’s position by adding the line of code import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() import time while True: pos = mc.player.getPos() It saves the player’s position as pos pos is called a variable What is a variable? Why is pos a variable?

9 Did you get it correct? import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() import time while True: pos = mc.player.getPos() Did you remember to indent after a line of code that ends in :

10 When it gets the player’s position called pos, it saves the player’s position as three separate variables The position along the x axis called pos.x The position along the y axis called pos.y The position along the x axis called pos.z

11 Each block has an ID number Flower has the ID number 38 The function in Minecraft to set down a block is mc.setBlock We therefore need to add the line of code mc.setBlock(pos.x, pos.y, pos.z, 38)

12 Did you get it correct? import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() import time while True: pos = mc.player.getPos() mc.setBlock(pos.x, pos.y,pos.z,38) Did you remember to indent the line of code because it belongs in the loop while True:

13 You now need to make it sleep for 0.2 seconds before looping again and setting down another flower block

14 Did you get it correct? import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() import time while True: pos = mc.player.getPos() mc.setBlock(pos.x, pos.y,pos.z,38) time.sleep(0.2) Did you remember to indent the line of code and put the dot in time.sleep Did you put the 0.2 seconds in brackets

15 Press F5 to save and run the program You should lay down a trail of flowers behind you If you get an error try to troubleshoot it or ask a friend to try to spot your mistake

16 What you have learned while loop A while loop repeats a section of Python code. The True part of the while loop means that it will repeat forever or until the user stops the program. time Using time in Python allows us to use functions that control time. getPos() The getPos() function allows us to find the co-ordinates of the player in the game world. setBlock() The setBlock() function allows us to create blocks in the Minecraft Pi world. It takes four arguments, the first three of which are co-ordinates and the fourth argument is the type of block that we want to create.

17 Challenge 1 Change the block number to a new block. Lay a new trail of blocks Change the times so that they are closer or further apart Click here for a list of Block IDs Challenge 2 Insert some of the following code to make the green light on the Pibrella come on when a block is set down and then go off again before a new block is set down import pibrella pibrella.light.green.on()

18 Did you get it correct? import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() import time while True: pos = mc.player.getPos() mc.setBlock(pos.x, pos.y,pos.z,38) pibrella.light.green.on() time.sleep(0.1) pibrella.light.green.on() time.sleep(0.1)

19 Challenge 3 Variables such as pos.x pos.y pos.z 38 Can be given suitable names that are easier to remember and use later on in your code e.g. x = pos.x y = pos.y z = pos.z block = 38 This is called declaring a variable

20 Challenge 3 You can now replace pos.x with x pos.y with y pos.z with z 38 with block These new names can be used later in the code to make it easier to program

21 Did you get it correct? import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() x = pos.x y = pos.y z = pos.z block = 38 import time while True: pos = mc.player.getPos() mc.setBlock(x, y, z, block) pibrella.light.green.on() time.sleep(0.1) pibrella.light.green.on() time.sleep(0.1)


Download ppt "Hacking Minecraft on the Raspberry Pi using Python Lesson 2 1."

Similar presentations


Ads by Google