def manualLift(): # This function will change the value in lift_requests # so it has to be listed as global here global lift_requests # If there is a request on floor 0 (first button pressed) if lift_requests[0]: # Move the lift up one block moveUp(True) # Clear the lift request, to prevent the lift moving forever lift_requests[0] = False # else if there is a request on floor 1 (second button pressed) elif lift_requests[1]: # Move the lift down one block moveDown(True) # Clear the lift request, to prevent the lift moving forever lift_requests[1] = False # Keep the display updated with the floor number # This is useful so that the service engineer can test the display floor = getFloor(lift_y) if floor != None: display.write(str(floor))
def bomb(x, y, z): mc.setBlock(x+1, y, z+1, block.TNT.id) for t in range(6): display.write(str(5-t)) # clever countdown time.sleep(1) mc.postToChat("BANG!") mc.setBlocks(x-10, y-5, z-10, x+10, y+10, z+10, block.AIR.id)
def bomb(x, y, z): mc.setBlock(x + 1, y, z + 1, block.TNT.id) for t in range(6): display.write(str(5 - t)) time.sleep(1) mc.postToChat("BANG!! La bomba ha estallado") mc.postToChat("TodoElectronica 21") mc.setBlocks(x - 10, y - 5, z - 10, x + 10, y + 10, z + 10, block.AIR.id)
def bomb(x, y, z): # Place a block of TNT mc.setBlock(x + 1, y, z + 1, block.TNT.id) # Count 6 numbers (from 0 to 5) for t in range(6): # Display the number sequence 5,4,3,2,1,0 on the 7-Seg display display.write(str(5 - t)) # Wait so that you can see the number time.sleep(1) mc.postToChat("BANG!") # Just like clearSpace.py, clear a "crater" around the player # The crater is centered around your player mc.setBlocks(x - 10, y - 5, z - 10, x + 10, y + 10, z + 10, block.AIR.id)
def bomb(x, y, z): size = 10 mc.setBlock(x+1, y, z+1, block.TNT.id) for t in range(6): display.write(str(5-t)) # clever countdown time.sleep(1) # tp player if inside blast radius pos = mc.player.getTilePos() if pos.x > x-size and pos.x<x+size and pos.y>(y-size/2) and pos.y<y+size and pos.z>z-size and pos.z<z+size: # tp player above bomb site mc.player.setPos(x,y+size,z) mc.postToChat("BANG!") mc.setBlocks(x-size, y-(size/2), z-size, x+size, y+size, z+size, block.AIR.id)
def bomb(x, y, z): # Place a block of TNT mc.setBlock(x+1, y, z+1, block.TNT.id) # Count 6 numbers (from 0 to 5) for t in range(6): # Display the number sequence 5,4,3,2,1,0 on the 7-Seg display display.write(str(5-t)) # Wait so that you can see the number time.sleep(1) mc.postToChat("BANG!") # Just like clearSpace.py, clear a "crater" around the player # The crater is centered around your player mc.setBlocks(x-10, y-5, z-10, x+10, y+10, z+10, block.AIR.id)
def atFloor(floor): # This function will change the values in these variables # so they must be listed as global here global lift_requests, lift_state # If there is a lift request pending for this floor if lift_requests[floor]: # Set the lift request to False, so the lift doesn't stick here lift_requests[floor] = False # The lift is now stopped (this will be used elsewhere) lift_state = STOPPED # Update the display hardware with the floor number display.write(str(floor)) # Wait a couple of seconds to allow the player to walk on/off time.sleep(2)
def autoLift(): # If the player is in the lift, the movement will have to also # move the player as well. Store this in a Boolean withPlayer # that can be used to alter how the move functions work later withPlayer = playerInLift() # Work out if the lift is at a afloor, and if so, which floor floor = getFloor(lift_y) # If the lift is at a floor if floor != None: # Perform the at-floor processing # which will stop the lift for a while atFloor(floor) # DECIDE WHAT TO DO NEXT # The lift can be in one of 3 states: GOING_UP, GOING_DOWN, STOPPED # Decide what to do for each of those 3 states if lift_state == GOING_UP: # Writing "up" to the display module will display a '^' symbol # which looks a little bit like an up arrow display.write("up") # Move the lift up by one block (with the player too # if the withPlayer variable is True) moveUp(withPlayer) elif lift_state == GOING_DOWN: # Writing "down" to the display module will display a 'v' symbol # which looks a little bit like a down arrow display.write("down") # Move the lift down by one blok (with the player too # if the withPlayer variable is True) moveDown(withPlayer) else: # Anything else, means the lift must be STOPPED # So, choose which floor to travel to next. # chooseNext() will change lift_state to GOING_UP or GOING_DOWN # which will then be processed next time this function is used chooseNext(floor)
DIAMONDS = [3,5,9] TIMEOUTS = [8,30,30] level = 0 points = 0 mc.postToChat("Press the button to start") while GPIO.input(BUTTON): time.sleep(0.1) #game loop while not gameOver: createDiamonds(arenaPos, DIAMONDS[level]) diamondsLeft = DIAMONDS[level] display.write(str(diamondsLeft)) start = time.time() levelComplete = False #level loop while not gameOver and not levelComplete: pos = mc.player.getTilePos() if pos.y < arenaPos.y: mc.player.setPos(arenaPos.x + ARENAX/2, arenaPos.y + 1, arenaPos.z + 1) if pos.z == arenaPos.z + ARENAZ and diamondsLeft == 0: levelComplete = True secondsLeft = TIMEOUTS[level] - (time.time() - start) if secondsLeft < 5:
# testSeg7.py - Test a 7-segment display import anyio.seg7 as display import time # Use this for Raspberry Pi #import RPi.GPIO as GPIO #LED_PINS = [10,22,25,8,7,9,11,15] # Use this for Arduino import anyio.GPIO as GPIO LED_PINS = [7, 6, 14, 16, 10, 8, 9, 15] GPIO.setmode(GPIO.BCM) ON = False # common-anode. Set to True for a common cathode display display.setup(GPIO, LED_PINS, ON) try: while True: for d in range(10): display.write(str(d)) time.sleep(0.5) finally: GPIO.cleanup() # END
# and thus you set the output to False (0V) to turn the LED on. # A common-anode display has its common pin connected to 3.3V # If you set this to True, it drives a common-cathode display # This means all the negative sides of the LEDs are connected together # and thus you set the output to True (3.3V) to turn the LED on. # A common-cathode display has its common pin connected to 0V ON = False # False=common-anode. Set to True for a common-cathode display # Setup the display module by giving it access to your GPIOs, # giving it a list of the pin numbers to use for each segment of the display, # and telling it whether you are using common-anode or common-cathode display.setup(GPIO, LED_PINS, ON) # Game loop try: # if this code fails, jumps to "finally" while True: # Count from 0 to 9 for d in range(10): # The display module turns the numbers 0..9 into patters # and turns the LEDs on and off for you display.write(str(d)) # Slow the program down so you can see it counting on the display time.sleep(0.5) finally: # Gets here if the above program fails (e.g. you press CTRL-C) # Put all of the GPIO hardware in a safe state. This turns off all the LEDs GPIO.cleanup() # END
points = 0 #HARDWARE - START #wait for the button to be pressed mc.postToChat("Press the button to start") while GPIO.input(BUTTON): time.sleep(0.1) #HARDWARE - END #game loop, while not game over while not gameOver: #create the diamonds createDiamonds(arenaPos, DIAMONDS[level]) diamondsLeft = DIAMONDS[level] #HARDWARE - update 7 segment display display.write(str(diamondsLeft)) #position the player at the start of the arena mc.player.setPos(arenaPos.x + 1, arenaPos.y + 1, arenaPos.z + 1) #start the clock start = time.time() #set the level complete flag levelComplete = False #level loop while not gameOver and not levelComplete: #sleep for a bit time.sleep(0.1) #has player hit a diamond?