Skip to content

Commit 75d8b81

Browse files
committed
1st edition code files
1 parent 676997a commit 75d8b81

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+3465
-2
lines changed

Adventure-Bonus/lift.py

+433
Large diffs are not rendered by default.

Adventure1/HelloMinecraftWorld.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Adventure 1: HelloMinecraftWorld.py
2+
3+
# From the book: "Adventures in Minecraft"
4+
# written by David Whale and Martin O'Hanlon, Wiley, 2014
5+
# http://eu.wiley.com/WileyCDA/WileyTitle/productCd-111894691X.html
6+
7+
import mcpi.minecraft as minecraft
8+
mc = minecraft.Minecraft.create()
9+
mc.postToChat("Hello Minecraft World")

Adventure2/rent.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Adventure 2: rent.py
2+
3+
# From the book: "Adventures in Minecraft"
4+
# written by David Whale and Martin O'Hanlon, Wiley, 2014
5+
# http://eu.wiley.com/WileyCDA/WileyTitle/productCd-111894691X.html
6+
#
7+
# This program is a game using geo-fencing.
8+
# You are challenged to collect objects from a field in the shortest
9+
# time possible. All the time you are in the field you are charged
10+
# rent. You have to collect/destroy blocks by spending the minimum
11+
# amount of rent. If you stay in the field for too long, your player
12+
# is catapulted up into the sky and out of the field, which makes the
13+
# game harder to play and much more fun!
14+
15+
# Import necessary modules
16+
import mcpi.minecraft as minecraft
17+
import time
18+
19+
# Connect to the Minecraft game
20+
mc = minecraft.Minecraft.create()
21+
22+
# Define constants for the 4 coordinates of the geo-fence
23+
X1 = 10
24+
Z1 = 10
25+
X2 = 20
26+
Z2 = 20
27+
28+
# Constants for the place to move through when catapulted!
29+
HOME_X = X2 + 2
30+
HOME_Y = 10 # up in the sky
31+
HOME_Z = Z2 + 2
32+
33+
# A variable to keep a tally of how much rent you have been charged
34+
rent = 0
35+
36+
# A variable to hold the number of seconds the player is in the field
37+
inField = 0
38+
39+
# The main game loop ticks round once every second
40+
while True:
41+
# Slow the program down a bit, this also helps with timing things
42+
time.sleep(1) # the loop runs once every second
43+
44+
# Get the players position
45+
pos = mc.player.getTilePos()
46+
47+
# If the player is in the field, charge him rent
48+
if pos.x>X1 and pos.x<X2 and pos.z>Z1 and pos.z<Z2:
49+
# Charge 1 pound rent every time round the loop (1sec per loop)
50+
rent = rent + 1
51+
# Tell player how much rent they owe
52+
mc.postToChat("You owe rent:" + str(rent))
53+
# Count number of seconds player is in the field (1sec per loop)
54+
inField = inField + 1
55+
else:
56+
# Not inside the field...
57+
inField = 0 # ...so reset the counter to zero
58+
59+
# If player in field for more that 3 seconds...
60+
if inField>3:
61+
mc.postToChat("Too slow!")
62+
# ...catapult player outside of the field
63+
mc.player.setPos(HOME_X, HOME_Y, HOME_Z)
64+
65+
# END

Adventure2/welcomeHome.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Adventure 2: welcomeHome.py
2+
3+
# From the book: "Adventures in Minecraft"
4+
# written by David Whale and Martin O'Hanlon, Wiley, 2014
5+
# http://eu.wiley.com/WileyCDA/WileyTitle/productCd-111894691X.html
6+
#
7+
# This program creates a magic doormat that says "welcome home"
8+
# when you stand on it.
9+
10+
# Import necessary modules
11+
import mcpi.minecraft as minecraft
12+
import time
13+
14+
# Connect to the Minecraft game
15+
mc = minecraft.Minecraft.create()
16+
17+
# The main game loop
18+
while True:
19+
# Short delay to prevent chat filling up too quickly
20+
time.sleep(1)
21+
22+
# Get player position
23+
pos = mc.player.getTilePos()
24+
25+
# Check whether your player is standing on the mat
26+
if pos.x == 10 and pos.z == 12:
27+
mc.postToChat("welcome home")
28+
29+
# END

Adventure2/whereAmI.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Adventure 2: whereAmI.py
2+
3+
# From the book: "Adventures in Minecraft"
4+
# written by David Whale and Martin O'Hanlon, Wiley, 2014
5+
# http://eu.wiley.com/WileyCDA/WileyTitle/productCd-111894691X.html
6+
#
7+
# This program works out where you are in the Minecraft world.
8+
# It prints a message on the Minecraft chat, with your coordinates.
9+
10+
# The Minecraft API has to be imported before it can be used
11+
import mcpi.minecraft as minecraft
12+
13+
# To communicate with a running Minecraft game,
14+
# you need a connection to that game.
15+
mc = minecraft.Minecraft.create()
16+
17+
# Ask the Minecraft game for the position of your player
18+
pos = mc.player.getTilePos()
19+
20+
# Display the coordinates of the players position
21+
mc.postToChat("x="+str(pos.x) + " y="+str(pos.y) +" z="+str(pos.z))
22+
23+
# END

Adventure2/whereAmI2.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Adventure 2: whereAmI2.py
2+
3+
# From the book: "Adventures in Minecraft"
4+
# written by David Whale and Martin O'Hanlon, Wiley, 2014
5+
# http://eu.wiley.com/WileyCDA/WileyTitle/productCd-111894691X.html
6+
#
7+
# This program works out where you are in the Minecraft world.
8+
# It uses a game loop to repeatedly display your position
9+
# on the Minecraft chat.
10+
11+
# The Minecraft API has to be imported before it can be used
12+
import mcpi.minecraft as minecraft
13+
14+
# The time module allows you to insert time delays
15+
import time
16+
17+
# To communicate with a running Minecraft game,
18+
# you need a connection to that game.
19+
mc = minecraft.Minecraft.create()
20+
21+
# A game loop will make sure your game runs forever
22+
while True:
23+
# delay, to prevent the Minecraft chat filling up too quickly
24+
time.sleep(1)
25+
26+
# Ask the Minecraft game for the position of your player
27+
pos = mc.player.getTilePos()
28+
29+
# Display the coordinates of the players position
30+
mc.postToChat("x="+str(pos.x) + " y="+str(pos.y) +" z="+str(pos.z))
31+
32+
# END

Adventure3/block.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Adventure 3: block.py
2+
3+
# From the book: "Adventures in Minecraft"
4+
# written by David Whale and Martin O'Hanlon, Wiley, 2014
5+
# http://eu.wiley.com/WileyCDA/WileyTitle/productCd-111894691X.html
6+
#
7+
# This program builds a block next to your player.
8+
9+
# Import necessary modules
10+
import mcpi.minecraft as minecraft
11+
import mcpi.block as block
12+
13+
# Connect to the Minecraft game
14+
mc = minecraft.Minecraft.create()
15+
16+
# Get the player position
17+
pos = mc.player.getTilePos()
18+
19+
# Create a block in front of the player
20+
mc.setBlock(pos.x+3, pos.y, pos.z, block.STONE.id)
21+
22+
# END

Adventure3/build.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Adventure 3: build.py
2+
3+
# From the book: "Adventures in Minecraft"
4+
# written by David Whale and Martin O'Hanlon, Wiley, 2014
5+
# http://eu.wiley.com/WileyCDA/WileyTitle/productCd-111894691X.html
6+
#
7+
# This program builds a block at an absolute coordinate
8+
9+
# Import necessary modules
10+
import mcpi.minecraft as minecraft
11+
import mcpi.block as block
12+
13+
# Connect to the Minecraft game
14+
mc = minecraft.Minecraft.create()
15+
16+
# Build a block
17+
mc.setBlock(1, 2, 3, block.DIAMOND_BLOCK.id)
18+
19+
# END
20+
21+
22+

Adventure3/buildHouse.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Adventure 3: buildHouse.py
2+
3+
# From the book: "Adventures in Minecraft"
4+
# written by David Whale and Martin O'Hanlon, Wiley, 2014
5+
# http://eu.wiley.com/WileyCDA/WileyTitle/productCd-111894691X.html
6+
#
7+
# This program builds a single house, with a doorway, windows,
8+
# a roof, and a carpet.
9+
10+
# Import necessary modules
11+
import mcpi.minecraft as minecraft
12+
import mcpi.block as block
13+
14+
# Connect to Minecraft
15+
mc = minecraft.Minecraft.create()
16+
17+
# A constant, that sets the size of your house
18+
SIZE = 20
19+
20+
# Get the players position
21+
pos = mc.player.getTilePos()
22+
23+
# Decide where to start building the house, slightly away from player
24+
x = pos.x + 2
25+
y = pos.y
26+
z = pos.z
27+
28+
# Calculate the midpoints of the front face of the house
29+
midx = x+SIZE/2
30+
midy = y+SIZE/2
31+
32+
# Build the outer shell of the house
33+
mc.setBlocks(x, y, z, x+SIZE, y+SIZE, z+SIZE, block.COBBLESTONE.id)
34+
35+
# Carve the insides out with AIR
36+
mc.setBlocks(x+1, y, z+1, x+SIZE-2, y+SIZE-1, z+SIZE-2, block.AIR.id)
37+
38+
# Carve out a space for the doorway
39+
mc.setBlocks(midx-1, y, z, midx+1, y+3, z, block.AIR.id)
40+
41+
# Carve out the left hand window
42+
mc.setBlocks(x+3, y+SIZE-3, z, midx-3, midy+3, z, block.GLASS.id)
43+
44+
# Carve out the right hand window
45+
mc.setBlocks(midx+3, y+SIZE-3, z, x+SIZE-3, midy+3, z, block.GLASS.id)
46+
47+
# Add a wooden roof
48+
mc.setBlocks(x, y+SIZE, z, x+SIZE, y+SIZE, z+SIZE, block.WOOD.id)
49+
50+
# Add a woolen carpet, the colour is 14, which is red.
51+
mc.setBlocks(x+1, y-1, z+1, x+SIZE-2, y-1, z+SIZE-2, block.WOOL.id, 14)
52+
53+
# END
54+

Adventure3/buildHouse2.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Adventure 3: buildHouse2.py
2+
3+
# From the book: "Adventures in Minecraft"
4+
# written by David Whale and Martin O'Hanlon, Wiley, 2014
5+
# http://eu.wiley.com/WileyCDA/WileyTitle/productCd-111894691X.html
6+
#
7+
# This program builds a house with a doorway, windows, a roof
8+
# and a carpet. It defines a house() function that you can easily
9+
# reuse in other programs.
10+
11+
# Import necessary modules
12+
import mcpi.minecraft as minecraft
13+
import mcpi.block as block
14+
15+
# Connect to Minecraft
16+
mc = minecraft.Minecraft.create()
17+
18+
# A constant, that sets the size of your house
19+
SIZE = 20
20+
21+
# define a new function, that builds a house
22+
def house():
23+
# Calculate the midpoints of the front face of the house
24+
midx = x+SIZE/2
25+
midy = y+SIZE/2
26+
27+
# Build the outer shell of the house
28+
mc.setBlocks(x, y, z, x+SIZE, y+SIZE, z+SIZE, block.COBBLESTONE.id)
29+
30+
# Carve the insides out with AIR
31+
mc.setBlocks(x+1, y, z+1, x+SIZE-2, y+SIZE-1, z+SIZE-2, block.AIR.id)
32+
33+
# Carve out a space for the doorway
34+
mc.setBlocks(midx-1, y, z, midx+1, y+3, z, block.AIR.id)
35+
36+
# Carve out the left hand window
37+
mc.setBlocks(x+3, y+SIZE-3, z, midx-3, midy+3, z, block.GLASS.id)
38+
39+
# Carve out the right hand window
40+
mc.setBlocks(midx+3, y+SIZE-3, z, x+SIZE-3, midy+3, z, block.GLASS.id)
41+
42+
# Add a wooden roof
43+
mc.setBlocks(x, y+SIZE, z, x+SIZE, y+SIZE, z+SIZE, block.WOOD.id)
44+
45+
# Add a woolen carpet, the colour is 14, which is red.
46+
mc.setBlocks(x+1, y-1, z+1, x+SIZE-2, y-1, z+SIZE-2, block.WOOL.id, 14)
47+
48+
# Get the players position
49+
pos = mc.player.getPos()
50+
51+
# Decide where to start building the house, slightly away from player
52+
x = pos.x + 2
53+
y = pos.y
54+
z = pos.z
55+
56+
# run the house() function that was defined by def house():
57+
# this will build a house at x,y,z
58+
house()
59+
60+
# END
61+
62+

Adventure3/buildStreet.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Adventure 3: buildStreet.py
2+
3+
# From the book: "Adventures in Minecraft"
4+
# written by David Whale and Martin O'Hanlon, Wiley, 2014
5+
# http://eu.wiley.com/WileyCDA/WileyTitle/productCd-111894691X.html
6+
#
7+
# This program builds a street of identical houses.
8+
# It uses a for loop.
9+
10+
# Import necessary modules
11+
import mcpi.minecraft as minecraft
12+
import mcpi.block as block
13+
14+
# Connect to Minecraft
15+
mc = minecraft.Minecraft.create()
16+
17+
# A constant, that sets the size of your house
18+
SIZE = 20
19+
20+
# define a new function, that builds a house
21+
def house():
22+
# Calculate the midpoints of the front face of the house
23+
midx = x+SIZE/2
24+
midy = y+SIZE/2
25+
26+
# Build the outer shell of the house
27+
mc.setBlocks(x, y, z, x+SIZE, y+SIZE, z+SIZE, block.COBBLESTONE.id)
28+
29+
# Carve the insides out with AIR
30+
mc.setBlocks(x+1, y, z+1, x+SIZE-2, y+SIZE-1, z+SIZE-2, block.AIR.id)
31+
32+
# Carve out a space for the doorway
33+
mc.setBlocks(midx-1, y, z, midx+1, y+3, z, block.AIR.id)
34+
35+
# Carve out the left hand window
36+
mc.setBlocks(x+3, y+SIZE-3, z, midx-3, midy+3, z, block.GLASS.id)
37+
38+
# Carve out the right hand window
39+
mc.setBlocks(midx+3, y+SIZE-3, z, x+SIZE-3, midy+3, z, block.GLASS.id)
40+
41+
# Add a wooden roof
42+
mc.setBlocks(x, y+SIZE, z, x+SIZE, y+SIZE, z+SIZE, block.WOOD.id)
43+
44+
# Add a woolen carpet, the colour is 14, which is red.
45+
mc.setBlocks(x+1, y-1, z+1, x+SIZE-2, y-1, z+SIZE-2, block.WOOL.id, 14)
46+
47+
# Get the players position
48+
pos = mc.player.getTilePos()
49+
50+
# Decide where to start building the house, slightly away from player
51+
x = pos.x + 2
52+
y = pos.y
53+
z = pos.z
54+
55+
# build 5 houses, for a whole street of houses
56+
for h in range(5):
57+
# build one house
58+
house()
59+
# move x by the size of the house just built
60+
x = x + SIZE
61+
62+
# END
63+
64+

0 commit comments

Comments
 (0)