1 / 32

Hacking Minecraft on the Raspberry Pi using Python

Learn how to use Python to control Minecraft and create a warehouse block. Use Python code to hollow out the warehouse and customize the block types. Import MCPI Minecraft and use the setBlocks function to create the warehouse.

conger
Download Presentation

Hacking Minecraft on the Raspberry Pi using Python

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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 a large warehouse block • Most of you will: • Use code to hollow out the warehouse block to make a shell • Some of you will: • Change the block type being placed and hollowed out • Add warehouse when you press a button

  4. import mcpi.minecraft as minecraftmc = 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)

  5. import mcpi.minecraft as minecraftmc = minecraft.Minecraft.create() You will be using time. You will need to add a line of code

  6. import mcpi.minecraft as minecraftmc = minecraft.Minecraft.create()import time We will use block 4 for the warehouse which is Cobblestone. I want to use the word cobblestone instead of 4. I need to declare this as a variable. Write a line of code to do this Did you get it correct?

  7. import mcpi.minecraft as minecraftmc = minecraft.Minecraft.create()cobblestone = 4 Did you get it correct? We will use block 0 for the inside of the warehouse which is Air. I want to use the word air instead of 0. I need to declare this as a variable. Write a line of code to do this

  8. import mcpi.minecraft as minecraftmc = minecraft.Minecraft.create()cobblestone = 4air = 0 I now need to tell Minecraft where I want to place my warehouse. The bottom left corner of the warehouse will be at x = 10 y = 11 z = 12 Code this in Did you get it correct?

  9. import mcpi.minecraft as minecraftmc = minecraft.Minecraft.create()cobblestone = 4air = 0x = 10y = 11z = 12 Did you get it correct? I now need to tell Minecraft where I want the top right of my warehouse. I will call these coordinates x2, y2, z2.

  10. If the bottom left corner of the warehouse is x = 10 y = 11 z = 12 and the top right coordinate for the warehouse are called x2, y2 and z2 and it is 10 blocks wide, 5 blocks high and 8 blocks deep What is x2, y2 and z2

  11. Did you get it correct? x = 10 y = 11 z = 12 x2 = 20 y = 16 z = 20 Code this in

  12. import mcpi.minecraft as minecraftmc = minecraft.Minecraft.create()cobblestone = 4air = 0x = 10y = 11z = 12x2 = 20y2= 16z2 = 20 Did you get it correct? You now need to add in a line of code to set these blocks down in the correct position mc.setBlocks(x, y, z, x2, y2, z2, cobblestone)

  13. import mcpi.minecraft as minecraftmc = minecraft.Minecraft.create()cobblestone = 4air = 0x = 10y = 11z = 12x2 = 20y2= 16z2 = 20mc.setBlocks(x, y, z, x2, y2, z2, cobblestone) Did you get it correct? Can you explain this line of code? mc.setBlocks(x, y, z, x2, y2, z2, cobblestone)

  14. What are these coordinates? x, y, z You now need to hollow out the warehouse If the bottom left coordinates of the corner of the warehouse are x, y, z What is are coordinates of the corner of air inside it? HINT – all of the coordinates go up by 1

  15. x+1 y+1 z+1 x, y, z Did you get it correct

  16. What are these coordinates? x2, y2, z2 Nearly these If the top right coordinates of the corner of the warehouse are x2, y2, z2 What is are coordinates of the corner of air inside it? HINT – all of the coordinates go down by 1

  17. x2, y2, z2 x2-1 y2-1 z2-1 Did you get it correct

  18. import mcpi.minecraft as minecraftmc = minecraft.Minecraft.create()cobblestone = 4air = 0x = 10y = 11z = 12x2 = 20y2= 16z2 = 20mc.setBlocks(x, y, z, x2, y2, z2, cobblestone)mc.setBlocks(x+1, y+1, z+1, x2-1, y2-1, z2-1, air) Let us code this in Can you explain this line of code? mc.setBlocks(x+1, y+1, z+1, x2-1, y2-1, z2-1, air)

  19. Press F5 to save and run the program You should make a warehouse of cobblestone. Steve can break through with his sword to see that it just a shell with air inside

  20. What you have learned Maths operators Addition, subtraction, multiplication and division are all possible in Python. Using them in Python allows you to change the values of variables such as the corners of the warehouse setBlocks() The setBlocks() function sets co-ordinates where lots of blocks can all be set down

  21. Challenge 1 Change the block numbers. Try glass instead of cobblestone (Block ID 20) filled with lava (Block ID 11) instead of air Click here for a list of Block IDs

  22. import mcpi.minecraft as minecraftmc = minecraft.Minecraft.create()glass = 20lava = 11x = 10y = 11z = 12x2 = 20y2= 16z2 = 20mc.setBlocks(x, y, z, x2, y2, z2, glass)mc.setBlocks(x+1, y+1, z+1, x2-1, y2-1, z2-1, lava) Did you get it correct?

  23. Challenge 2 Let the program do the size calculations for you e.g. if you know that the warehouse is 10 wide then x2 = x+10 e.g. if you know that the warehouse is 5 high then y2 = y+5 e.g. if you know that the warehouse is 8 deep then z2 = z+8 Change your code

  24. import mcpi.minecraft as minecraftmc = minecraft.Minecraft.create()glass = 20lave = 11x = 10y = 11z = 12x2 = x+10y2= y+5z2 = z+8mc.setBlocks(x, y, z, x2, y2, z2, glass)mc.setBlocks(x+1, y+1, z+1, x2-1, y2-1, z2-1, lava) Did you get it correct?

  25. Challenge 3 Now get the program to put the blocks down when you press the pibrella button We need to add a while True: to check keep checking the button We need to add a If pibrella.button.read(): to see if the button is pressed We need indentation after lines of code with :

  26. import mcpi.minecraft as minecraftmc = minecraft.Minecraft.create()glass = 20lava = 11x = 10y = 11z = 12x2 = x+10y2= y+5z2 = z+8while True: if pibrella.button.read()mc.setBlocks(x, y, z, x2, y2, z2, glass)mc.setBlocks(x+1, y+1, z+1, x2-1, y2-1, z2-1, lava) Did you get it correct?

  27. Challenge 4 Now get the program to put the light the green light when the blocks are put down and then go off 1 seconds later

  28. import mcpi.minecraft as minecraftmc = minecraft.Minecraft.create()import timeglass = 20lava = 11x = 10y = 11z = 12x2 = x+10y2= y+5z2 = z+8while True: if pibrella.button.read() pibrella.light.green.on()mc.setBlocks(x, y, z, x2, y2, z2, glass)mc.setBlocks(x+1, y+1, z+1, x2-1, y2-1, z2-1, lava) time.sleep(1) pibrella.light.green.off() Did you get it correct?

  29. Challenge 5 Get the warehouse to be built wherever you are rather than at a set position You now need to get the player’s position by adding the line of code pos = mc.player.getPos() It saves the player’s position as pos And saves the players coordinates as pos.x pos.y pos.z We could declare these by adding in the lines x = pos.x y = pos.y z = pos.z

  30. import mcpi.minecraft as minecraftmc = minecraft.Minecraft.create()import timeglass = 20lava = 11while True: if pibrella.button.read()pos = mc.player.getPos() x = pos.x y=pos.y z=pos.zx2 = x+10 y2= y+5 z2 = z+8pibrella.light.green.on()mc.setBlocks(x, y, z, x2, y2, z2, glass)mc.setBlocks(x+1, y+1, z+1, x2-1, y2-1, z2-1, lava)time.sleep(1) pibrella.light.green.off() Did you get it correct? Why did I move the x,y,z coordinates into the loop?

  31. Challenge 6 Clear some space before you place your blocks. Clear a space 100 blocks wide, 100 high and 100 deep by filling it with air (Block ID 0)

  32. import mcpi.minecraft as minecraftmc = minecraft.Minecraft.create()import timeglass = 20lava = 11air = 0while True: if pibrella.button.read()pos = mc.player.getPos() x = pos.x y=pos.y z=pos.zmc.setBlocks(x,y,z, x2+100, y2+100, z2+100, air)x2 = x+10 y2= y+5 z2 = z+8pibrella.light.green.on()mc.setBlocks(x,y,z, x2, y2, z2, glass)mc.setBlocks(x+1, y+1, z+1, x2-1, y2-1, z2-1, lava)time.sleep(1) pibrella.light.green.off() Did you get it correct?

More Related