-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathintro.py
329 lines (200 loc) · 7.13 KB
/
intro.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# Welcome! This is a Python program file
import time
import mcpi.minecraft as minecraft
import mcpi.block as block
import random
mc = minecraft.Minecraft.create()
# The lines that start with a hash (#) are comments
# They are for you to read and are ignored by Python
# When you see 'GO!', save and run the file to see the output
# When you see a line starting with # follow the instructions
# Some lines are python code with a # in front
# This means they're commented out - remove the # to uncomment
# Do one challenge at a time, save and run after each one!
# You can comment out exercises you finish to clear the output
# from previous exercises.
# 1. This is the print statement that prints to the Python Shell
print("Hello world")
# GO!
# You can also print to the Minecraft Chat
#mc.postToChat("Hello Minecraft World")
# GO!
# 2. This is a variable
message = "Level Two"
# Add a line below to print this variable
# GO!
# 3. The variable above is called a string
# You can use single or double quotes (but must close them)
# You can ask Python what type a variable is. Try uncommenting the next line:
#print(type(message))
# GO!
# 4. Another type of variable is an integer (a whole number)
a = 123
b = 654
c = a + b
# Try printing the value of c below to see the answer
# GO!
# 5. You can use other operators like subtract (-) and multiply (*)
# Try some below by replacing the word with the correct operator
#print a times b
#print b minus a
#print 12 times 4
#print 103 add 999
#print 100 divide 25
# GO!
# 6. Variables keep their value until you change it
a = 100
#print(a) # think - should this be 123 or 100?
c = 50
#print(c) # think - should this be 50 or 777?
d = 10 + a - c
#print(d) # think - what should this be now?
# GO!
# 7. You can also use '+' to add together two strings
greeting = 'Hi '
name = '' # enter your name in this string
message = greeting + name
#mc.postToChat(message)
# GO!
# 8. Try adding a number and a string together and you get an error:
#age = # enter your age here (as a number)
#mc.postToChat(name + ' is ' + age + ' years old')
# GO!
# See the error? You can't mix types like that.
# But see how it tells you which line was the error?
# Now comment out that line so there is no error
# 9. We can convert numbers to strings like this:
#mc.postToChat(name + ' is ' + str(age) + ' years old')
# GO!
# No error this time, I hope?
# Or we could just make sure we enter it as a string:
# age = # enter your age here, as a string
# mc.postToChat(name + ' is ' + age + ' years old')
# GO!
# No error this time, I hope?
# You can also print the position of your player:
pos = mc.player.getTilePos()
#mc.postToChat(pos.x)
#mc.postToChat(pos.y)
#mc.postToChat(pos.z)
# The player's position would be easier to read on one line.
# Fix the following code:
#mc.postToChat("x=" + x + " y=" + y + " z=" + z)
# GO!
# Hint: you can simplify the code by getting x, y, z in one go!
# Use the command below to replace the code above.
#x, y, z = mc.player.getTilePos()
# GO!
# 10. Another variable type is called a boolean
# This means either True or False
raspberry_pi_is_fun = True
raspberry_pi_is_expensive = False
# We can also compare two variables using == (equal) or != (not equal)
steves_age = 15
#your_age = # fill in your age
#print(your_age == steves_age) # this prints either True or False
#print(your_age != steves_age) # and what does this print?
# GO!
# 11. We can use less than and greater than too -- these are < and >
#steve_is_older = steves_age > your_age
#print(steve_is_older) # do you expect True or False?
# GO!
# 12. We can ask questions before printing with an if statement
#south = pos.z > 0
#if south:
# message = "You are in the South"
#else:
# message = "You are in the North"
#mc.postToChat(message) # what do you expect to see here?
# GO!
# Try moving from North to South and running the code above
# We can ask many questions using elif (else if) and use 'not'
# to mean the opposite.
#south = pos.z > 0
#north = not south
#west = pos.x operator 0 # fix me using less than
#east = pos.x operator 0 # fix me using greater than (and equal to)
#if north and east:
# message = "You are in the North East"
#elif north and west:
# message = "You are in the North West"
#elif south and east:
# message = "You are in the South East"
#elif south and west:
# message = "You are in the South West"
#mc.postToChat(message)
# GO!
# 13. You can create your own commands, or functions using def(ine)
def whereAmI():
mc.postToChat('You are here.')
# Try 'calling' the function:
#whereAmI()
# GO!
# Now move your North-South-East-West code inside the function.
# Remember to indent! (and also to update position inside the loop)
# Can you call the function many times? (Try from the Shell as well)
# 14. How can we keep the program running continuously while we play?
#while True:
# whereAmI()
# GO!
# CTRL+C to exit ('while True' will run forever!)
# The loop runs too fast! Can we slow it down?
# Try using the time.sleep(seconds) function inside the loop.
# GO!
# 15. Can we make the loop exit on a 'condition'?
# How about when the player is at position 0, 0, 0 ?
# Hint: keep looping while x, y, x not equal to 0, 0, 0
# GO!
#while condition:
# whereAmI()
# 16. We can also loop over numbers
# What do you expect the code below to do? What numbers are printed?
# Can you print
#for a in range(0, 10):
# print(a)
# GO!
# 17. It's Minecraft! Let's start building
#mc.setBlock(0, 0, 0, block.STONE.id)
# GO!
# It's not very useful building a block far away from you!
# Can you set the block close to your current position?
# GO!
# 18. Loops + Block = Awesome!
# Let's build a wall (or tower)
#for x in range (start, end): # replace start and end
# mc.setBlock(x, y, z, block.BRICK_BLOCK.id) # fix a value for y and z
# GO!
# Now fix x and loop y to make a tower!
# GO!
# 19. Let's experiment with different types of blocks.
# Press CTRL+SPACE after typing 'block.', or see the list here:
# http://www.stuffaboutcode.com/p/minecraft-api-reference.html
# Note: some blocks have special properties, e.g.
#mc.setBlock(0, 0, 0, block.TNT.id, 1) # '1' means active TNT. Hit it!
#for color in range(0,16):
# mc.setBlock(0, 0, 0, block.WOOL.id, color) # change color
# time.sleep(0.5)
# GO!
# Challenge: Can you make a wall of changing colors?
# Advanced: Try random colors using random.randint(0, 15)
# GO!
# 20. You can keep many items in a type of variable called a list
blocks = [block.STONE.id, block.COBBLESTONE.id, block.GRAVEL.id,
block.SANDSTONE.id, block.MOSS_STONE.id]
#for block in blocks:
# print block # what do you expect to see?
# GO!
# A wall with each kind of block:
#x = # where do you want to start?
#for block in blocks:
# mc.setBlock(x, 10, 10, block) # what do you expect to see?
# x = x + 1
# GO!
# Let's build a wall using layers of these blocks.
#y = # where do you want to start?
#z = # fix z to somewhere close to you
#for block in blocks:
# for x in range (start, end): # replace start and end
# mc.setBlock(x, y, z, block) # change y as you go higher
# GO!
### Congratulations! Your done! On to more programming! ###