Hardware Reference
In-Depth Information
CHALLENGE
Can you change the program so that you can press the button at the end of the
game to start a new game rather than having to re-run the program?
Diamond Countdown
The 7-segment display will show the number of diamonds left to collect; the display
will be updated using the display.write function.
To do this, you need to modify the Crafty Crossing program to update the display
whenever the diamonds are created in the arena and whenever a player hits a dia-
mond:
1. After the createDiamonds function has been called in the game loop and the
diamonds have been put in the arena, update the display:
createDiamonds(arenaPos, DIAMONDS[level])
diamondsLeft = DIAMONDS[level]
display.write(str(diamondsLeft))
he display.write() method expects a string to be passed to it, so the num-
ber of diamonds left is converted to a string using str() before being passed to
the function.
2. When a player has hit a diamond and the number of diamonds is reduced by 1,
update the display:
hits = mc.events.pollBlockHits()
for hit in hits:
blockHitType = mc.getBlock(hit.pos.x, hit.pos.y,
hit.pos.z)
if blockHitType == block.DIAMOND_BLOCK.id:
mc.setBlock(hit.pos.x,hit.pos.y, hit.pos.z,
block.AIR.id)
diamondsLeft = diamondsLeft - 1
display.write(str(diamondsLeft))
3. At the end of the program, you should make sure the display is cleared. So,
before the GPIO is cleaned up, add the code to clear the display:
display.clear()
GPIO.cleanup()
 
Search WWH ::




Custom Search