Download Minecraft Programming with Python Page Minecraft Lessons Basics

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Minecraft Programming with Python
Page 1
Minecraft Lessons
Basics of Python

IDLE is the editor, where you write your programs

Python is an interpreted language. It’s compiled into machine language at runtime in the Python Shell. So
anything you ‘print’ prints out here by default/

It’s case-sensitive so Print, print and PRINT all mean different things

Program – series of instructions to the computer

Comments in the code are marked at the start of the line with the # pound sign.

Syntax – rules of the programming language

Errors happen. Unlike other jobs, we learn to fix errors, which we call debugging
Start the Buckkit Server
1.
Open Windows Explorer. That’s the program that’s used for locating your files.
2.
Go to your USB drive.
3.
Go to the folder called StFrancis
4.
Open the Adventures folder.
5.
Click on StartBukkit.bat
Open Minecraft
1.
Open Windows Explorer. That’s the program that’s used for locating your files.
2.
Go to your USB drive.
3.
Go to the folder called StFrancis
4.
Go to the Minecraft folder
5.
Go to the Games folder
6.
Click on the launcher.jar file
7.
Create a new profile – Name it Profile 1.6.4. Make sure to pick MC 1.6.4!
Join your Bukkit Server


Use Direct Connect and type the localhost.
o
Or, type the IP address you want to use 192.168.1.1
o
Make sure you use your computers IP address number
/ is the command line




/gamemode creative
/weather clear 1200
/time set 0 = daytime
/time set 24000 = night
Can set yourself as OP. Type: op mapple2
© 2015 Kalata, K All Rights Reserved
March, 2015
Lessons based on O’Hanlon and Whale’s book, Adventures in Minecraft
Minecraft Programming with Python
Page 2
Adventure 1 HelloMinecraftWorld.py
1.
Open IDLE. Type this. Save this file as HelloWorld
import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()
mc.postToChat("Hello Minecraft World")
You could have used an IP
address
mc = minecraft.
Minecraft.create
("192.168.1.1", 4711)
2.
Go to Run in the menu and then select -> Run Module or press F5
3.
Stop the Bukkit Minecraft server – Control-C or go to Shell menu then pick Restart Shell
© 2015 Kalata, K All Rights Reserved
March, 2015
Lessons based on O’Hanlon and Whale’s book, Adventures in Minecraft
Minecraft Programming with Python
Page 3
Adventure 1 GeoFence Programs
Coordinates are a set of numbers that can be used to locate players
When you play you can see your coordinates with F3 key
+Y
+X
+Z
 up
 east
 south
-Y
-X
-Z
 down
 west
 north
When a user logs in, their coordinates are automatically shown.
ACTIVITY
Let’s plot some player’s coordinates.
1.
Everyone go into the game.
2.
In one minute you will be frozen.
3.
Check your coordinates (Press F3)
Quick Reference Table
Importing the Minecraft API
import mcpi.minecraft as minecraft
Creating a connection to Minecraft
mc
=
minecraft.Minecraft.create()
© 2015 Kalata, K All Rights Reserved
March, 2015
Lessons based on O’Hanlon and Whale’s book, Adventures in Minecraft
Minecraft Programming with Python
Page 4
Variables and Printing and Positioning

Variable is a location in the computer memory. You can assign a value to a variable, like a number that
might be used in a calculation. You can also use a string. A message is just a ‘string” of character letters or
numbers.

You can also call built-in functions. That means someone else wrote the code for us.

For example, pos.x means go get me the x coordinates, of where the player is located.

You can combine things too. So you can assign, the x coordinates to a variable. Why? It’s much easier to
remember a variable name than a line of code!

You can print out values and variables and other things

You can print out a message, along with other information, like your players coordinates

Sometimes when a number is printed they use str to make sure that the number is a ‘string’ of characters, like
the alphabet characters. After all you don’t want to really try to add X+ and 24. The calculator might go bizerk!
print(value)
print(“Hello”)
print(10)
a=10
print(a)
b=”Hello”
print(b)
print(pos.x)
x = pos.x
print(x)
print("x= "+ pos.x)
print("x= "+str(pos.x))
Note that you don’t need to tell the variable what kind of information it will contain (number, string). We call that
process ‘declaring’ the variable. But it’s not required with Python.
The + plus sign is used for concatenating bringing together two string messages.
© 2015 Kalata, K All Rights Reserved
March, 2015
Lessons based on O’Hanlon and Whale’s book, Adventures in Minecraft
Minecraft Programming with Python
Page 5
Adventure 2 whereAmI.py


getTilePos – returns the coordinates of the block the player is standing on.
Coordinates are just numbers, with decimal portions.
ACTIVITY
Type this in IDLE. Save this file.
import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()
mc.postToChat("Hello Minecraft World")
Type this
pos = mc.player.getTilePos()
print(pos.x)
print(pos.y)
print(pos.z)
print("x=" + str(pos.x) +
" y=" + str(pos.y) +
" z=" + str(pos.z))
mc.postToChat("x=" + str(pos.x)
z="+str(pos.z))
+"
y=" + str(pos.y)
+"
Go to Run -> Run Module or press F5.
 It runs 1 time. You will see that the coordinates are posted, in the chat window, and in the output
window.
This is what you see in the Python window
Quick Reference Table
Getting the player’s tile position
pos
=
Posting a message to the Minecraft chat
mc.player.getTilePos() x = pos.x
y = pos.y
mc.postToChat("Hello Minecraft")
z = pos.z
Setting the player’s tile position
x = 5
y = 3
z = 7
mc.player.setTilePos(x, y, z)
© 2015 Kalata, K All Rights Reserved
March, 2015
Lessons based on O’Hanlon and Whale’s book, Adventures in Minecraft
Minecraft Programming with Python
Page 6
Adventure 2 whereAmI2.py






You can make this an infinite loop, so it runs and doesn’t stop! Use looping methods, to repeat the
code over and over. We only stop when a condition is true or false. In this case, until they sleep, they
will display the position.
You ‘indent’ the line, inside the loop.
Use a colon, to nest statements in a group like the while loop
Time.Sleep is used to wait 1 second and then write out the word tick.
Remember, Control-C in the Python window will stops the code and the server!
You can restart it, go to the Python menu and choose Shell then Restart Shell
Type this in IDLE. Save this file. import mcpi.minecraft as minecraft
# Import necessary modules
import time
mc = minecraft.Minecraft.create()
mc.postToChat("Hello Minecraft World")
while True:
time.sleep(1)
pos
=
mc.player.getTilePos()
print(pos.x)
print(pos.y)
print(pos.z)
print("x=" + str(pos.x) +
" y=" + str(pos.y)
" z=" + str(pos.z))
+
mc.postToChat(
"x=" + str(pos.x) +
" y=" + str(pos.y) +
" z="+str(pos.z))
Go to Run -> Run Module or press F5.
© 2015 Kalata, K All Rights Reserved
March, 2015
Lessons based on O’Hanlon and Whale’s book, Adventures in Minecraft
Minecraft Programming with Python
Page 7
Adventure 2 whereAreYou.py
We can do this for individual players!
To connect to a particular player use:
mc = minecraft.Minecraft.create(name="Mapple")
# get the entity id for a named player
entityId = getPlayerEntityId("pinkprofesssor")
print entityId
Other things you can do
# get coordinates of x,y,z as one group or unit, for the player
unitVector = mc.player.getDirection()
or
mc.entity(entityId).getDirection()
entityId = mc.getPlayerEntityId("pinkprofesssor")
Other things you can do
mc.entity.setPos(entityIds[0], x, y, z)
mc.entity.getPos(entityIds[0])
mc.entity.setTilePos(entityIds[0], x, y, z)
mc.entity.getTilePos(entityIds[0])
Yes you can get more information with entityIds = mc.getPlayerEntityIds()
while True: p = mc.player.getTilePos() if p.x>10 and p.x<20 and p.z>10 and p.z<20: mc.postToChat("in the zone!")
© 2015 Kalata, K All Rights Reserved
March, 2015
Lessons based on O’Hanlon and Whale’s book, Adventures in Minecraft
Minecraft Programming with Python
Page 8
WelcomeHome.py – AKA Magic Doormat





If and Else statements are a way to run some lines of code but not others.
Parenthesis are not required
Check to see if the player is in a certain place or on a certain block.
If the condition is true, then, it runs the code.
Else means that if it’s not true, then run the code after the word else.
a = 5;
if a>10:
print("big")
else
print("small")
b = 10;
if b>10:
print("big")
else
print("small")
You can also check other conditions, like between two numbers using “AND”, as well as “OR”
So the first and last numbers don’t count!
a=8
if a>5 and a<10:
print("between")
a=3
if a>5 and a<10:
print("between")
Type this in IDLE. Save this file.
# Import necessary modules
import mcpi.minecraft as minecraft
import time
# Connect to the Minecraft game
mc = minecraft.Minecraft.create()
while True:
# Short delay to prevent chat filling up too quickly
time.sleep(1)
pos = mc.player.getTilePos()
if pos.x == 10 and pos.z == 12:
mc.postToChat("welcome home")
You don’t need the Z coordinates, for up and down.
Talking Mat
https://twitter.com/whaleygeek/status/537527016569176064
If you see some error messages, like NULL, or Not defined, they are different than an empty string. Basically the
computer doesn’t know what it has in the variable.
Constants are variables that don’t change their value. They are in upper case.
GeoFencing – using coordinates to fence around an object.
© 2015 Kalata, K All Rights Reserved
March, 2015
Lessons based on O’Hanlon and Whale’s book, Adventures in Minecraft
Minecraft Programming with Python
Page 9
Rent.py - GeoFencing to Charge Rent



Need the coordinates of your field. Smallest and biggest x and y
If you display a number you have to convert it to a string using
str(number)
3 seconds to get out of the field then catapult them into
another location with SetPos
Type this and save it as Rent.py
import mcpi.minecraft as minecraft
import time
mc = minecraft.Minecraft.create()
# Define constants for the 4 coordinates of the geo-fence
X1 = 10
Z1 = 10
X2 = 20
Z2 = 20
# Constants for the place to move through when catapulted!
HOME_X = X2 + 2
HOME_Y = 10 # up in the sky
HOME_Z = Z2 + 2
Notice that you can use
absolute coordinates, the x, y,
z values or relative values
(like x + 2)
# A variable to keep a tally of how much rent you have been charged
rent = 0
# A variable to hold the number of seconds the player is in the field
inField = 0
# The main game loop ticks round once every second
while True:
# Slow the program down a bit, this also helps with timing things
time.sleep(1) # the loop runs once every second
# Get the players position
pos = mc.player.getTilePos()
# If the player is in the field, charge him rent
if pos.x>X1 and pos.x<X2 and pos.z>Z1 and pos.z<Z2:
# Charge $1 rent every time round the loop (1sec per loop)
rent = rent + 1
# Tell player how much rent they owe
mc.postToChat("You owe rent:" + str(rent))
# Count number of seconds player is in the field (1sec per loop)
inField = inField + 1
else:
# Not inside the field...
inField = 0 # ...so reset the counter to zero
# If player in field for more that 3 seconds...
if inField>3:
mc.postToChat("Too slow!")
# ...catapult player outside of the field
mc.player.setPos(HOME_X, HOME_Y, HOME_Z)
© 2015 Kalata, K All Rights Reserved
March, 2015
Lessons based on O’Hanlon and Whale’s book, Adventures in Minecraft
Minecraft Programming with Python
Page 10
Dice.py - Creating Blocks



Blocks have ID numbers and names, that are
constant and assigned.
Some blocks like Glowing Obsidian only work on
the Raspberry Pi and not PC/MAC
SetBlocks with relative or absolute coordinates
Block Lists:
 Block.Stone
 Block.Air
Create a new file and save as dice.py to create 6 blocks
Need to import the block library
# This program builds the face of a dice near
your player.
# Import necessary modules
import mcpi.minecraft as minecraft
import mcpi.block as block
Block id numbers on the Minecraft wiki:
http://minecraft.gamepedia.com/Blocks
Minecraft extra data values are taken from:
http://minecraft.gamepedia.com/Data_values
# Connect to the Minecraft game
mc = minecraft.Minecraft.create()
# Get the player position
pos = mc.player.getTilePos()
# Create 6 blocks in front of the player, that look like a dice
mc.setBlock(pos.x+3, pos.y, pos.z, block.STONE.id)
mc.setBlock(pos.x+3, pos.y+2, pos.z, block.STONE.id)
mc.setBlock(pos.x+3, pos.y+4, pos.z, block.STONE.id)
mc.setBlock(pos.x+3, pos.y, pos.z+4, block.STONE.id)
mc.setBlock(pos.x+3, pos.y+2, pos.z+4, block.STONE.id)
mc.setBlock(pos.x+3, pos.y+4, pos.z+4, block.STONE.id)
Building a simple loop
Control loops – For Loops
1.
Open the Python Shell window.
2.
Type this in
for a in range(10):
print(a)


Press enter
Press enter twice
So you only loop a limited number of times.
You give it a range, like 0-9, but not the upper number 9,
not upper bound!
Tower.py - Building a Huge Toper with a For Loop
© 2015 Kalata, K All Rights Reserved
March, 2015
Lessons based on O’Hanlon and Whale’s book, Adventures in Minecraft
Minecraft Programming with Python
Open Idle and type this and save as Tower.py.
Need to import the block library
The building can’t be taller than the world!
Page 11
# Each block is at height: y+a
import mcpi.minecraft as minecraft
import mcpi.block as block
mc = minecraft.Minecraft.create()
pos = mc.player.getTilePos()
for a in range(50):
mc.setBlock(pos.x+3, pos.y+a, pos.z,
block.STONE.id)
ClearSpace.py




Clears space to do some building
Based on the direction you are facing!
Set bottom left and top right coordinates for a cube
Ask how big, using raw_input


SetBlock() – sets one block
SetBlocks() – sets a whole 3D area


Stone =1
Air = 0
import mcpi.minecraft as minecraft
import mcpi.block as block
mc = minecraft.Minecraft.create()
pos = mc.player.getTilePos()
size = 50
mc.setBlocks(
pos.x, pos.y, pos.z,
pos.x+size, pos.y+size, pos.z+size,
block.AIR.id)
ClearSpace.py


Ask how big, using raw_input from the keyboard – works in Python 2 not Python 3!
Int(size) converts the input, a string, to an integer, a number!
import mcpi.minecraft as minecraft
import mcpi.block as block
mc = minecraft.Minecraft.create()
pos = mc.player.getTilePos()
size = int(raw_input("size of area to clear? "))
mc.setBlocks(
pos.x, pos.y, pos.z,
pos.x+size, pos.y+size, pos.z+size,
block.AIR.id)
© 2015 Kalata, K All Rights Reserved
March, 2015
Lessons based on O’Hanlon and Whale’s book, Adventures in Minecraft
Minecraft Programming with Python
Page 12
BuildHouse.py - Building a House – Hollow Structure – Great math problem!
Build the house on paper
import mcpi.minecraft as minecraft
import mcpi.block as block
# Connect to Minecraft
mc = minecraft.Minecraft.create()
# A constant, that sets the size of your house
SIZE = 20
# Get the players position
pos = mc.player.getTilePos()
# Decide where to start building the house, slightly
away from player by 2 blocks so you don’t build on
him!
x = pos.x + 2
y = pos.y
z = pos.z
# Calculate the midpoints of the front face of the house
midx = x+SIZE/2
midy = y+SIZE/2
# Build the outer shell of the house
mc.setBlocks(x, y, z, x+SIZE, y+SIZE, z+SIZE,
block.COBBLESTONE.id)
# Carve the insides out with AIR
mc.setBlocks(x+1, y, z+1, x+SIZE-2, y+SIZE-1, z+SIZE-2, block.AIR.id)
# doorway
mc.setBlocks(midx-1, y, z, midx+1, y+3, z, block.AIR.id)
Watch a tutorial on how to build
your house at
h t t p : / / www.wiley.com/go/
adventuresinminecraft and choose
the Adventure 3 Video.
# left hand window
mc.setBlocks(x+3, y+SIZE-3, z, midx-3, midy+3, z, block.GLASS.id)
# right hand window
mc.setBlocks(midx+3, y+SIZE-3, z, x+SIZE-3, midy+3, z, block.GLASS.id)
# wooden roof
mc.setBlocks(x, y+SIZE, z, x+SIZE, y+SIZE, z+SIZE, block.WOOD.id)
# woolen carpet, the colour is 14, which is red.
mc.setBlocks(x+1, y-1, z+1, x+SIZE-2, y-1, z+SIZE-2, block.WOOL.id, 14)
Wool has extra
data, 14, which
is the color of
the wool. Data
passed is called
a parameter.
© 2015 Kalata, K All Rights Reserved
March, 2015
Lessons based on O’Hanlon and Whale’s book, Adventures in Minecraft
Minecraft Programming with Python
Page 13
Functions




Functions are just a bunch or commands grouped together, so you can easily call them at a later time
Split a big program into smaller, reusable programs
PostToChat() is a function that is built into the program. You can also build your own functions.
Python at the command line indents the command lines for you. Don’t forget the colon!
def myname():
print("my name is David")
print("I am a computer programmer") print("I love Minecraft programming")
Then call the function
myname()
Local and Global Variables
BuildHouse2.py
import mcpi.minecraft as minecraft
import mcpi.block as block
mc = minecraft.Minecraft.create()
# A constant, that sets the size of your house
SIZE = 20
Global, can be used anywhere in
the program.
Local, can only be used where it’s
in the program, like inside the
function
def house():
# Calculate the midpoints of the front face of the house
midx = x+SIZE/2
midy = y+SIZE/2
# Build the outer shell of the house
mc.setBlocks(x, y, z, x+SIZE, y+SIZE, z+SIZE, block.COBBLESTONE.id)
# Carve the insides out with AIR
mc.setBlocks(x+1, y, z+1, x+SIZE-2, y+SIZE-1, z+SIZE-2, block.AIR.id)
# Carve out a space for the doorway
mc.setBlocks(midx-1, y, z, midx+1, y+3, z, block.AIR.id)
# Carve out the left and right hand windows
mc.setBlocks(x+3, y+SIZE-3, z, midx-3, midy+3, z, block.GLASS.id)
mc.setBlocks(midx+3, y+SIZE-3, z, x+SIZE-3, midy+3, z, block.GLASS.id)
# Add a wooden roof and carpet as you did before
mc.setBlocks(x, y+SIZE, z, x+SIZE, y+SIZE, z+SIZE, block.WOOD.id)
mc.setBlocks(x+1, y-1, z+1, x+SIZE-2, y-1, z+SIZE-2, block.WOOL.id, 14)
# Get the players position
pos = mc.player.getPos()
# Decide where to start building the house, slightly away from player
x = pos.x + 2
y = pos.y
z = pos.z
# build a house at x,y,z
house()
© 2015 Kalata, K All Rights Reserved
March, 2015
Lessons based on O’Hanlon and Whale’s book, Adventures in Minecraft
Minecraft Programming with Python
Page 14
BuildStreet.py – Repeating the Function
Building a row of houses is completed by simply looping through the function many times. You could also do this with
the tower example or add a layer of bedrock first.
import mcpi.minecraft as minecraft
import mcpi.block as block
mc = minecraft.Minecraft.create()
SIZE = 20
def house():
midx = x+SIZE/2
midy = y+SIZE/2
# Build the outer shell of the house and carve out the air inside
mc.setBlocks(x, y, z, x+SIZE, y+SIZE, z+SIZE, block.COBBLESTONE.id)
mc.setBlocks(x+1, y, z+1, x+SIZE-2, y+SIZE-1, z+SIZE-2, block.AIR.id)
# Carve out a space for the doorway, windows, and roof and carpet
mc.setBlocks(midx-1, y, z, midx+1, y+3, z, block.AIR.id)
mc.setBlocks(x+3, y+SIZE-3, z, midx-3, midy+3, z, block.GLASS.id)
mc.setBlocks(midx+3, y+SIZE-3, z, x+SIZE-3, midy+3, z, block.GLASS.id)
mc.setBlocks(x, y+SIZE, z, x+SIZE, y+SIZE, z+SIZE, block.WOOD.id)
mc.setBlocks(x+1, y-1, z+1, x+SIZE-2, y-1, z+SIZE-2, block.WOOL.id, 14)
# Get the players position and build slightly away from player
pos = mc.player.getTilePos()
x = pos.x + 2
y = pos.y
z = pos.z
# build 5 houses, for a whole street of houses
for h in range(5):
# build one house
house()
# move x by the size of the house just built
x = x + SIZE
© 2015 Kalata, K All Rights Reserved
March, 2015
Lessons based on O’Hanlon and Whale’s book, Adventures in Minecraft
Minecraft Programming with Python
Page 15
Random Numbers
Pseudo-random number between 2 numbers, like 1 and 100.
Import the namespace random.
Use random.randint
import random
print(random.randint(1,100))
0 white
1 orange
2 magenta
3 light blue
4 yellow
5 lime
6 pink
7 grey
for n in range(50):
8 light grey
9 cyan
10 purple
11 blue
12 brown
13 green
14 red
15 black
print(random.randint(1,100))
BuildStreet2.py - Random interior carpets
import mcpi.minecraft as minecraft
import mcpi.block as block
import random
mc = minecraft.Minecraft.create()
SIZE = 20
def house():
midx = x+SIZE/2
midy = y+SIZE/2
# Build the outer shell of the house and carve out the air inside
mc.setBlocks(x, y, z, x+SIZE, y+SIZE, z+SIZE, block.COBBLESTONE.id)
mc.setBlocks(x+1, y, z+1, x+SIZE-2, y+SIZE-1, z+SIZE-2, block.AIR.id)
# Carve out a space for the doorway, windows, and roof and carpet
mc.setBlocks(midx-1, y, z, midx+1, y+3, z, block.AIR.id)
mc.setBlocks(x+3, y+SIZE-3, z, midx-3, midy+3, z, block.GLASS.id)
mc.setBlocks(midx+3, y+SIZE-3, z, x+SIZE-3, midy+3, z, block.GLASS.id)
mc.setBlocks(x, y+SIZE, z, x+SIZE, y+SIZE, z+SIZE, block.WOOD.id)
mc.setBlocks(x+1, y-1, z+1, x+SIZE-2, y-1, z+SIZE-2, block.WOOL.id, 14)
# Add a woolen carpet, the colour is 14, which is red.
mc.setBlocks(x+1, y-1, z+1, x+SIZE-2, y-1, z+SIZE-2, block.WOOL.id, 14)
# Add a random carpet
c = random.randint(0, 15)
mc.setBlocks(x+1, y+1, z+1, x+SIZE-1, y+1, z+SIZE-1, block.WOOL.id, c)
# Get the players position and build slightly away from player
pos = mc.player.getTilePos()
x = pos.x + 2
y = pos.y
z = pos.z
# build 5 houses, for a whole street of houses
for h in range(5):
house()
# move x by the size of the house just built
x = x + SIZE
© 2015 Kalata, K All Rights Reserved
March, 2015
Lessons based on O’Hanlon and Whale’s book, Adventures in Minecraft
Minecraft Programming with Python
Page 16
Quick Reference Table
Importing and using the block name
constants
Setting/changing a block at a
position
import mcpi.block as block
b = block.DIRT.id
mc.setBlock(5, 3, 2, block.DIRT.id)
Setting/changing lots of blocks in one go
Useful block types for houses
mc.setBlocks(0,0,0,5,5,5,block.DIRT.id)
block.AIR.id
block.STONE.id
block.COBBLESTONE.id
block.GLASS.id
block.WOOD.id
block.WOOL.id
block.SLATE.id
SafeFeet.py - Fine Out What You are Standing On

y-1 is the block below you
import mcpi.minecraft as minecraft
import mcpi.block as block
import time
mc = minecraft.Minecraft.create()
def safeFeet():
# Get the players position
pos = mc.player.getTilePos()
# Get the block directly below your player
b = mc.getBlock(pos.x, pos.y-1, pos.z)
# Is the player safe?
if b == block.AIR.id
or b == block.WATER_STATIONARY.id
or b == block.WATER_FLOWING.id:
mc.postToChat("not safe")
else:
mc.postToChat("safe")
# Game loop
while True:
# Run the game loop once every half a second
time.sleep(0.5)
# Check if your feet are safe
safeFeet()
© 2015 Kalata, K All Rights Reserved
March, 2015
Lessons based on O’Hanlon and Whale’s book, Adventures in Minecraft
Minecraft Programming with Python
Page 17
MagicBridge.py – Using OR statements
You can creep using Shift key to move slowly
import mcpi.minecraft as minecraft
import mcpi.block as block
mc = minecraft.Minecraft.create()
def buildBridge():
# Get the players position
pos = mc.player.getTilePos()
# Get the block directly below your player
b = mc.getBlock(pos.x, pos.y-1, pos.z)
# Is the player unsafe?
if b == block.AIR.id or b ==
block.WATER_FLOWING.id or b==block.WATER_STATIONARY.id:
# If unsafe, build a glass block directly under the player
mc.setBlock(pos.x, pos.y-1, pos.z, block.GLASS.id)
# Game loop
while True:
# There is no delay here, this loop needs to run really fast
# otherwise your bridge might not build quick enough and you will fall off
buildBridge()
© 2015 Kalata, K All Rights Reserved
March, 2015
Lessons based on O’Hanlon and Whale’s book, Adventures in Minecraft
Minecraft Programming with Python
Page 18
Appendix
Help – Commands for Command Line Window
Useful commands such as op, gamemode, time, weather, tp
http://minecraft.gamepedia.com/commands?cookieSetup=true
© 2015 Kalata, K All Rights Reserved
March, 2015
Lessons based on O’Hanlon and Whale’s book, Adventures in Minecraft
Minecraft Programming with Python
Page 19
Structure of the Minecraft API
http://www.stuffaboutcode.com/p/minecraft-api-reference.html
minecraft.py





Class Minecraft - main class for connecting and interacting with the game
Class camera - changing camera angle and postion
Class player - getting and changing the players position and setting
Class entity - getting and changing entities position and setting
Class events - retreiving events which have occured in the game
block.py

Class Block - definition of a block, specifically its type
event.py

Class BlockEvent - definition of a block event, specifically what event, what block and what player
vec3.py

Class Vec3 - generic class for managing a 3 dimension vector (i.e. x,y,z)
connection.py - internal module used by the api
util.py - internal module used by the api
© 2015 Kalata, K All Rights Reserved
March, 2015
Lessons based on O’Hanlon and Whale’s book, Adventures in Minecraft
Minecraft Programming with Python
Page 20
What you will need for Adventures in Minecraft




Download the Starter Kit from Wiley. wiley.com/go/adventuresinminecraft
Bukkit server with Minecraft API - 192.168.1.73
o Default Port mc = minecraft.Minecraft.create("192.168.1.1", 4711)
o “The API works by connecting to the 'server' which maintains the Minecraft world and issuing
commands to change the world in real-time. The api specification is included with the the
game, ~/mcpi/api/mcpi_protocol_spec.txt.” ~/mcpi/api/[java/python]
o http://www.stuffaboutcode.com/2013/01/raspberry-pi-minecraft-api-basics.html
o http://www.stuffaboutcode.com/2013/04/minecraft-pi-edition-api-tutorial.html
Minecraft and the Internet – or a network
Python 2 with IDLE editor (Python 2 versus 3 - Python2 uses print “hello” for outputting, and name =
raw_input(“name?”) for inputting. Python3 uses print(“hello”) for outputting, and name = input(“name?”) for
inputting.)
Cool resources










Quick Reference http://www.stuffaboutcode.com/p/adventures-in-minecraft.html
Recipe Cards - http://www.raspberrypi.org/minecraft-pi-recipe-cards-to-download-and-keep/
Python Flash cards http://blog.whaleygeek.co.uk/python-flashcards/
Reference for Python in MCPI http://www.tecoed.co.uk/uploads/1/4/2/4/14249012/minecraft_python.pdf
http://techedonline.org/adventures-in-minecraft-pt-1-maybe/
Teacher Book – Code Academy https://arghbox.files.wordpress.com/2013/06/teacheredition.pdf
Learn to Program - Flying Cows http://media.pragprog.com/titles/ahmine/build.pdf and
http://media.pragprog.com/titles/ahmine2/build.pdf
David Whale - Twitter https://twitter.com/whaleygeek/status/559339292762599424
Walk Through http://www.stuffaboutcode.com/2013/04/minecraft-pi-edition-api-tutorial.html
API Tutorial http://www.stuffaboutcode.com/2013/04/minecraft-pi-edition-api-tutorial.html
Canary Starter Kits






https://drive.google.com/a/ohanlonweb.com/folderview?id=0BwqjqhNUlUf1TjRGNXR6U3Uza0k&usp=shari
ng#list
Canarymod starter kits for Minecraft 1.7.10, which you can download from here. Note - these are provided
without any warranty. They are exactly the same as the starter kits available on companion site just Minecraft
1.6.4 changed to 1.7.10 and Bukkit changed to Canarymod.
Run as root using the command: sudo ./start_server.command
CanaryMod help http://canarymod.net/books/canarymod-admin-guide/setting-your-server
Change Permissions – chmod 777 StartCanary.command
o http://hints.macworld.com/article.php?story=20001231152532966
Whaley blogs
o http://blog.whaleygeek.co.uk/adventures-in-minecraft/
o http://blog.whaleygeek.co.uk/minecraft-pi-with-python/
© 2015 Kalata, K All Rights Reserved
March, 2015
Lessons based on O’Hanlon and Whale’s book, Adventures in Minecraft