-
Notifications
You must be signed in to change notification settings - Fork 0
/
target_minigame.py
359 lines (253 loc) · 9.24 KB
/
target_minigame.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
from mcpi.minecraft import Minecraft
from mcpi import block
from mcpi.vec3 import Vec3
from random import randint
from utils import getCurrentTime, floatToInt
import math
from sensors import Button, LEDLight
# For most of this game, the Z coordinate is used in place of the X because
# it allows the player to play the game without manually turning very much
mc = Minecraft.create()
def sendMessage(message):
mc.postToChat(message)
def allowDestruction(isAllowed):
mc.setting("world_immutable", not isAllowed)
def removeBlock(x, y, z):
mc.setBlock(x, y, z, block.AIR.id)
class Target:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
self.dir = Vec3(1, 0, 0)
self.speed = 1
self.blockType = block.WOOD.id
# Used for setting a delay in target move speed
self.moveTime = getCurrentTime()
self.timeUntilMove = 1000
def canMove(self):
timeSinceMove = gameInfo.gameTime - self.moveTime
return (timeSinceMove >= self.timeUntilMove)
def move(self):
removeBlock(self.x, self.y, self.z)
self.z += self.dir.x * self.speed
self.y += self.dir.y * self.speed
# Keep the block within the game board bounds
self.checkSwitchDir()
mc.setBlock(self.x, self.y, self.z, self.blockType)
def checkSwitchDir(self):
if (self.y >= gameInfo.gameBoard.y):
self.y = gameInfo.gameBoard.y
self.dir.y *= -1
elif (self.y <= gameInfo.gameBoard.bottom):
self.y = gameInfo.gameBoard.bottom
self.dir.y *= -1
if (self.z >= gameInfo.gameBoard.right):
self.z = gameInfo.gameBoard.right
self.dir.x *= -1
elif (self.z <= gameInfo.gameBoard.z):
self.z = gameInfo.gameBoard.z
self.dir.x *= -1
def wasHit(self, blockPos):
pos = Vec3(self.x, self.y, self.z)
return (pos == blockPos)
def remove(self):
removeBlock(self.x, self.y, self.z)
class AttackBlock:
def __init__(self, x, y , z):
self.pos = Vec3(x, y, z)
self.dir = Vec3(1, 0, 0)
self.blockType = block.GOLD_ORE.id
self.moveTime = getCurrentTime()
self.timeUntilMove = 1000
def canMove(self):
timeSinceMove = gameInfo.gameTime - self.moveTime
return (timeSinceMove >= self.timeUntilMove)
def move(self):
self.remove()
self.pos.x += self.dir.x
mc.setBlock(self.pos.x, self.pos.y, self.pos.z, self.blockType)
def remove(self):
removeBlock(self.pos.x, self.pos.y, self.pos.z)
class PlayerInfo:
def __init__(self):
self.player = mc.player
self.score = 0
self.attackTime = getCurrentTime()
self.timeUntilAttack = 1000
self.attackBlocks = []
def getPos(self):
x, y, z = self.player.getPos()
x = floatToInt(x)
y = floatToInt(y)
z = floatToInt(z)
return Vec3(x, y, z)
def setPos(self, x, y, z):
self.player.setPos(x, y, z)
def canAttack(self):
timeSinceAttack = gameInfo.gameTime - self.attackTime
# Prevent attacks if the user is away from the game board
facingGameBoard = gameInfo.gameBoard.isInside(self.getPos())
return (timeSinceAttack >= self.timeUntilAttack \
and facingGameBoard == True)
def attack(self):
x, y, z = self.getPos()
x = floatToInt(x)
y = floatToInt(y)
z = floatToInt(z)
attackBlock = AttackBlock(x, y, z)
self.attackBlocks.append(attackBlock)
# Reset attack timer
self.attackTime = getCurrentTime()
class GameInfo:
def __init__(self, winScore=None):
if winScore is None:
winScore = 10
self.gameStarted = False
self.gameTime = getCurrentTime()
self.maxTargets = 1
self.targets = []
self.winScore = winScore
self.gameBoard = Rect(0, 0, 0, 0, 0)
def hitTarget(self, target):
target.remove()
self.targets.remove(target)
player.score += 1
sendMessage("Score: %i" % (player.score))
# Blink the LED
ledLight.startBlinking()
createTarget()
def checkPlayerWin(self):
if (player.score >= self.winScore):
# Stop the game
self.gameStarted = False
# Send the win message
sendMessage("You win!!")
# Allow destruction in the game world
allowDestruction(True)
class Rect:
def __init__(self, x, y, z, width, height):
self.x = x
self.y = y
self.z = z
self.width = width
self.height = height
self.depth = 1
@property
def bottom(self):
return (self.y - self.height)
@property
def right(self):
return (self.z + self.width)
def isInside(self, pos):
insideZ = (pos.z >= self.z and pos.z <= self.right)
insideY = (pos.y <= self.y and pos.y >= self.bottom)
return (insideZ and insideY)
def isInsideWithX(self, pos):
insideX = (pos.x == self.x)
insideZ = (pos.z >= self.z and pos.z <= self.right)
insideY = (pos.y <= self.y and pos.y >= self.bottom)
return (insideX and insideZ and insideY)
gameInfo = GameInfo()
player = PlayerInfo()
ledLight = LEDLight(17, 4)
button = Button(22)
def createGameWalls():
global gameInfo
x, y, z = player.getPos()
radius = 12
wallHeight = 6
startX = floatToInt(x) + radius
startY = floatToInt(y)
startZ = floatToInt(z)
gameInfo.gameBoard.x = startX
gameInfo.gameBoard.y = startY + wallHeight
gameInfo.gameBoard.z = startZ - radius
gameInfo.gameBoard.width = radius * 2
gameInfo.gameBoard.height = wallHeight
# range() is exclusive on the upper limit
for i in range(0, wallHeight + 1):
for j in range(-radius, radius + 1):
mc.setBlock(startX, startY + i, startZ + j, block.STONE.id)
def createGameStartSwitch():
x, y, z = player.getPos()
switchDistance = 6
hDistance = 2
mc.setBlock(x + switchDistance, y, z + hDistance, block.TNT.id, 1)
def checkGameBlocksHit():
for blockEvent in mc.events.pollBlockHits():
x, y, z = blockEvent.pos
blockType = mc.getBlock(x, y, z)
def initGameMode():
global gameInfo
gameInfo = GameInfo(3)
# Turn off the LED if it's on
ledLight.setLightStatus(False)
allowDestruction(True)
createGameStartSwitch()
# Tell the user how to start playing
sendMessage("Look for the TNT near you. Hit it to start target practice!")
while gameInfo.gameStarted == False:
for blockEvent in mc.events.pollBlockHits():
x, y, z = blockEvent.pos
blockType = mc.getBlock(x, y, z)
if (blockType == block.TNT.id):
removeBlock(x, y, z)
startGame()
def createTarget():
global gameInfo
# Get a random start position on the game board
randX = randint(gameInfo.gameBoard.z, gameInfo.gameBoard.right)
randY = randint(gameInfo.gameBoard.bottom, gameInfo.gameBoard.y)
target = Target(gameInfo.gameBoard.x - 1, randY, randX)
# Get a random direction for the target
dirX = 0
dirY = 0
# Make sure the block will be moving
while (dirX == 0 and dirY == 0):
dirX = randint(-1, 1)
dirY = randint(-1, 1)
target.dir = Vec3(dirX, dirY, 0)
mc.setBlock(target.x, target.y, target.z, target.blockType)
gameInfo.targets.append(target)
def gameLoop():
while gameInfo.gameStarted == True:
gameInfo.gameTime = getCurrentTime()
checkGameBlocksHit()
if (len(gameInfo.targets) < gameInfo.maxTargets):
createTarget()
for target in gameInfo.targets:
if (target.canMove() == True):
target.move()
target.moveTime = getCurrentTime()
if (button.pressed() == True and player.canAttack() == True):
player.attack()
# Uncomment below two lines for auto-attack
#if (player.canAttack() == True):
#player.attack()
for attackBlock in player.attackBlocks:
attackBlock.move()
if (gameInfo.gameBoard.isInsideWithX(attackBlock.pos) == True):
player.attackBlocks.remove(attackBlock)
attackBlock.remove()
mc.setBlock(attackBlock.pos.x, attackBlock.pos.y, attackBlock.pos.z, block.STONE.id)
for target in gameInfo.targets:
if target.wasHit(attackBlock.pos) == True:
gameInfo.hitTarget(target)
gameInfo.checkPlayerWin()
break
if (ledLight.canBlink(gameInfo.gameTime) == True):
ledLight.blink()
def startGame():
gameInfo.gameStarted = True
# Prevent the user from destroying the game wall
allowDestruction(False)
# Move the player up to an "arena"
#x, y, z = player.getPos()
#player.setPos(x, y, z)
createGameWalls()
sendMessage("Target practice has begun!")
gameLoop()
# Start the game mode
if __name__ == "__main__":
initGameMode()