Hacking Minecraft on the Raspberry Pi using Python

Hacking Minecraft on the Raspberry Pi using Python

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 Objective of the lesson Use 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 3 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() You need to add a loop which keeps checking to see if you are stood on water import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() Did you get it correct? Did you remember the capital T and the : import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() while True:

We need to declare the water (Block ID 9) and ice (Block ID 79) Did you get it correct? Did you remember to indent after a line of code ending in : import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() while True: water = 9 ice = 79 We can now use the words water and ice to

represent these blocks We now need to get the players position. We will declare it as pos import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() while True: water = 9 ice = 79 pos = mc.player.getPos() We can now declare the players pos.x, pos.y and pos.z as just x,y and z to make our coding easier

Did you get it correct? import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() while True: water = 9 ice = 79 pos = mc.player.getPos() x = pos.x y = pos.y z = pos.x Our current coordinates are x,y,z What are the coordinates of the block one below our feet?

Did you get it correct? the coordinates of the block one below our feet is (x,y-1,z) We can get this block ID by using the line of code block = mc.getBlock(x,y-1,z) We have now declared it as block import mcpi.minecraft as minecraft mc = 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) 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 minecraft mc = 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) Did you get it correct? import mcpi.minecraft as minecraft mc = 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: Did you remember to indent it? Did you remember the : at the end of an if line of code Now add a line of code to set down an ice block in place of the water import mcpi.minecraft as minecraft mc = 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) Can you explain this line of code? Press F5 to save and run the program

When you walk over water it should now change to ice What you have learned getBlock() The getBlock() function finds the type of a block at certain co-ordinates. The co-ordinates are given as x, y and z 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 of code 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 use an equal to operator with an if statement to check whether the block below the player is water. Challenge 1 We 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 minecraft mc = 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) Challenge 1 Did you get it correct? import mcpi.minecraft as minecraft mc = 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()

Challenge 1 Now 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 minecraft mc = 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() Challenge 1 Did you get it correct? import mcpi.minecraft as minecraft mc = 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()

Challenge 2 Can you change the code so that when you stand on sand (Block ID 12) it changes to grass (Block ID 2) Challenge 2 Did you get it correct? import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() import time while 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() Challenge 3

You 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 Challenge 3 Did you get it correct? Did you remember the : and indent correctly import mcpi.minecraft as minecraft mc = 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()

Recently Viewed Presentations

  • Intelligent Process Automation in Audit Abigail Zhang Continuous

    Intelligent Process Automation in Audit Abigail Zhang Continuous

    Currently the workflow/process automation are predefined, meaning, …. And I call it PPA. And recently we have a new technique, RPA, to facilitate PPA. Our current work. Limitations: each and every situation. Can we make it more flexible? In terms...
  • Fusion is our Future: Readiness of the Fusion

    Fusion is our Future: Readiness of the Fusion

    Industry 4.0. The 4th industrial revolution (Industry 4.0) is on its way. Trend to use automation and data exchange technologies (cyber-physical systems, the Internet of things, cloud computing and cognitive computing) to perform industry activities
  • DESIGNING VISUALLY ACCESSIBLE CITIES AND COMMUNITIES OPPI CONFERENCE

    DESIGNING VISUALLY ACCESSIBLE CITIES AND COMMUNITIES OPPI CONFERENCE

    OPPI CONFERENCE Devin Causley MCIP RPP Oct 06, 2015 Connecting People to Places What is nearby that might be of interest? Shops, services, tourism What is coming up next along their path? Accessibility, tourism, economic development share complementary objectives.
  • สถิติที่ใช้ในการวิจัย

    สถิติที่ใช้ในการวิจัย

    สถิติที่ใช้ในการวิจัย Pearson Correlation เป็นการหาความสัมพันธ์ระหว่างตัวแปร 2 ตัวที่อยู่ในมาตราการวัดระดับ Interval หรือ Ratio Scale ค่าที่ได้เรียกว่า ...
  • Unit 1, Week 1

    Unit 1, Week 1

    I detested walking across the muddy field in my good shoes. 2. En español, to detest quiere decir "disgustar mucho, detestar." Cuando alguien detesta algo, no le gusta hacerlo o tenerlo cerca. Detesto atravesar el campo lodoso en mis zapatos...
  • GALILEO GALILEI (1564-1642)  ITALIAN PHYSICIST MATHEMATICIAN ASTRONOMER PHILOSOPHER

    GALILEO GALILEI (1564-1642) ITALIAN PHYSICIST MATHEMATICIAN ASTRONOMER PHILOSOPHER

    on the left, ptolemy's theory (geocentric) shows that venus will always be in a crescent phase. on the right, copernicus' theory (heliocentric) shows that venus will go through a complete set of phases. note that the size of venus changes...
  • Number Systems - University of Chicago

    Number Systems - University of Chicago

    10100000000000000000000 1.1012 = 1.62510 Excess Notation To include + and - exponents, "excess" notation is used Single precision: excess 127 Double precision: excess 1023 The value of the exponent stored is larger than the actual exponent E.g., excess 127, Exponent...
  • Science- Plants

    Science- Plants

    2 types of seeds. Monocot seeds. Mono = 1. Monocot seeds only have 1 leaf attached to the embryo. The food around the seed is called endosperm. This kind of seed does not split into 2 when the seed opens