forked from martinohanlon/making-a-game-with-minecraft-pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WhacAMole - FullCode.py
174 lines (81 loc) · 4.66 KB
/
WhacAMole - FullCode.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
# STEP 1 - Import
# First, we need to import some code that other people have written to let us connect to Minecraft
#import the minecraft modules
import mcpi.minecraft as minecraft
import mcpi.block as block
#import random so you can create lights in random locations
import random
#import time so we can put delays into our program
import time
# STEP 2 - Connect
# Next, we will connect to Minecraft, by setting up a connection with minecraft.Minecraft.create() and naming it "mc":
mc = minecraft.Minecraft.create()
# STEP 3 - Test Post
# To test the connection, try posting something to chat, using mc.postToChat("message")
# STEP 4 - Position
# Use mc.player.getTilePos() to find the player's positiona and save it as a variable called "pos"
pos = mc.player.getTilePos()
# STEP 5 - Build The Board
# We want the game to appear just in front of the player's location.
# You can make the game board out of whatever block material you want, google for a full list of names.
# Choose two materials, one for "good" blocks, and one for "bad". I've chosen COBBLESTONE and MOSS_STONE.
mc.setBlocks(pos.x - 1, pos.y, pos.z + 3,
pos.x + 1, pos.y + 2, pos.z + 3,
block.COBBLESTONE.id)
# STEP 6 - Countdown
# The player needs a warning that the game is about to start
# Use the mc.PostToChat("message") from earlier, along with time.sleep(seconds)
mc.postToChat("Get ready ...")
time.sleep(2)
mc.postToChat("Go")
# STEP 7 - Set Up Variables
# We need to keep track of a few bits of information, so we will create variables for each of them.
# We need to know how many blocks are currently bad (blocksBad) and what score the player has got (points)
blocksBad = 0
points = 0
# STEP 8 - Loop
# Set up a While Loop that will keep running until all the lights are bad (Hint: use blocksBad < 9)
# Remember to indent all code inside the loop
while blocksBad < 9:
# STEP 9 - Hit Blocks
# You now need need to turn off any blocks that have turned bad.
# Most of the code for this section has been given, but make sure you understand most of it and make any needed alterations before moving on.
# poll a list of block hits, then use for to loop through each hitBlock:
for hitBlock in mc.events.pollBlockHits():
# check if the the block in that location was bad
if mc.getBlock(hitBlock.pos.x, hitBlock.pos.y, hitBlock.pos.z) == block.MOSS_STONE.id:
# if it was, set that location back to a good block
mc.setBlock(hitBlock.pos.x, hitBlock.pos.y, hitBlock.pos.z, block.COBBLESTONE.id)
# Add some code to increase score and lower the blocksBad count:
blocksBad = blocksBad - 1
points = points + 1
# STEP 10 - Turn a Block Bad
# Select a random block as our next bad block by choosing random coordinates:
xPos = pos.x + random.randint(-1,1)
yPos = pos.y + random.randint(0,2)
zPos = pos.z + 3
# Use setBlock to make the block at those coordinates into the bad material:
mc.setBlock(xPos, yPos, zPos, block.MOSS_STONE.id)
# increase the blocksBad count:
blocksBad = blocksBad + 1
# STEP 11 - Wait
# At the end of the loop, make the game pause for a second before changing the next block.
# Use time.sleep(seconds) like before. 0.2 is roughly the right amount, but experiment.
time.sleep(0.2)
# STEP 12 - Gameover
# Because the loop has ended (we aren't indenting any more), we know that all of the blocks went bad.
# Display a message to the player, including points scored.
mc.postToChat("Game Over - points = " + str(points))
# WHAT NEXT?
# Think about what cool extra features you might want to add to your game.
# Here are a few ideas:
# Add a message to the loop so that the player can see their score
# The difficulty of the game is set by the waiting before changing another block, currently time.sleep(0.2)
# Inceasing this time will make it easier, decreasing will make it harder.
# Experiment and see what works best for you.
# What if the player gets things wrong and hits the wrong block?
# Can you change the program so that it changes the block to something else?
# This forces the player to think more about what block they are hitting and increases the skill required.
# It is common for video games to start easy and get harder.
# Can you make the game start easier and the more points you score, the harder it gets?
# If you've used Python Functions before, try to rearrange and separate your code so that it all lies in neat functions.