Hacking Minecraft on the Raspberry Pi using Python

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

HACKING MINECRAFT. LET’S OVERCLOCK sudo raspi-config Change 7: Over clock 900Mhz at max Change 8: Select memory share = 128MB.
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
Bug Session Three. Session description In this session, pupils will discover how the Bug software makes drawing shapes easier (this is based on the use.
Simple Python Loops Sec 9-7 Web Design.
Noadswood Science,  To know the basics of Python coding and decoding Monday, September 07, 2015.
An Introduction to Textual Programming
Lists. Lists store information in a certain order. You can add, remove, or rearrange items in a list. You can also pick out information from a list.
Program ultrasonic range sensor in autonomous mode
By the end of this session you should be able to...
Bug Session Three. Session description In this session, pupils will discover how the Bug software makes drawing shapes easier (this is based on the use.
Scratch Programming Lesson 4 Question asking and answering.
Available at: Lesson 3.5 – Program Light Sensor in Autonomous Mode Program Light Sensor in Autonomous Mode.
PROGRAMMING In Lesson 2. STARTER ACTIVITY Complete the starter activity in your python folder – lesson 2 Now we will see how you got on and update your.
Variables. Todays Lesson  In todays lesson you are going to:  Learn to use variables  Learn to ask for user input  Learn to save the users response.
Make a dice challenge! This is a starter activity and should take 5 minutes [ slide 1 ] 1.Log in to your computer 2.Open IDLE 3.Copy the code below in.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
HACKING MINECRAFT.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
Introduction to Minecraft Pi Aims: To know what Minecraft is To learn how to enter code into Minecraft Pi To successfully enter at least 2 Python programs.
HACKING MINECRAFT 1. LET’S OVERCLOCK Type sudo raspi-config Change 7: Over clock 900Mhz at max Change 8: Select memory share = 128MB.
Hacking Minecraft on the Raspberry Pi using Python Lesson 2 1.
Building Blocks CS 1308 Computer Literacy and the Internet.
Turtle Graphics Lesson 2 1. There are 3 homeworks to complete during the six lessons of this unit. Your teacher will let you know when a homework has.
How to make a Line Follow program Using EV3 software and an NXT light sensor.
Vex Robotics program three: using motors and sensors together.
PYTHON WHILE LOOPS. What you know While something is true, repeat your action(s) Example: While you are not facing a wall, walk forward While you are.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Sensor Information: while loops and Boolean Logic.
Login using your own username and password Go to programming -> geany Enter the following program: Open LX Terminal, enter these 2 commands from mcpi.minecraft.
Lesson 4 - Challenges.
Starter Activity Instructions: Complete the starter sheet Planner
Programming Scratch to Control a K’NEX Fairground Ride
If, else, elif.
Objective of the lesson
Hacking Minecraft on the Raspberry Pi using Python
Hacking Minecraft on the Raspberry Pi using Python
Hacking Minecraft on the Raspberry Pi using Python
Programming – Touch Sensors
Frozen Graphics Lesson 3.
BBC Microbit.
Raspberry Pi: External Inputs & Outputs
BBC Microbit.
Sensors and Logic Switches
INTERMEDIATE PROGRAMMING LESSON
Variables Lesson 3.
Raspberry Pi with Pibrella
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
INTERMEDIATE PROGRAMMING LESSON
Recap the basics Lesson 1.
Hacking Minecraft on the Raspberry Pi using Python
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Introduction to Programming
Introduction to Programming
Game Controller Lesson Two.
Using the sensor Lesson 5.
Objective of the lesson
Python 19 Mr. Husch.
PYTHON: BUILDING BLOCKS Sequencing & Selection
Programming In Lesson 4.
Variables Lesson 3.
Recap the basics Lesson 1.
Using the sensor Lesson 5.
Python While Loops.
Introduction to Programming
Introduction to Programming
Python 19 Mr. Husch.
Starter Activities GCSE Python.
Objective of the lesson
Presentation transcript:

Hacking Minecraft on the Raspberry Pi using Python Lesson 5

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)

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

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()

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 remember the capital T and the : Did you get it correct? Did you remember the capital T and the : We need to declare the water (Block ID 9) and ice (Block ID 79) import mcpi.minecraft as minecraft mc = minecraft.Minecraft.create() while True:

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

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

Our current coordinates are x,y,z Did you get it correct? Our current coordinates are x,y,z What are the coordinates of the block one below our feet? 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

the coordinates of the block one below our feet is 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 remember to indent it? Did you get it correct? Did you remember to indent it? Did you remember the : at the end of an if line of code 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:

Now add a line of code to set down an ice block in place of the water Can you explain this line of code? 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)

Press F5 to save and run the program When you walk over water it should now change to ice

co-ordinates. The co-ordinates are given as x, y and 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

Did you get it correct? Did you remember the : and indent correctly 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()