Created
March 7, 2016 13:12
-
-
Save lurch/58acd14ddf0cbf3d667a to your computer and use it in GitHub Desktop.
Very simple whack-a-mole type game for the Pimoroni DrumHAT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import cap1xxx | |
import random | |
import time | |
""" | |
4 3 2 | |
5 7 1 | |
6 0 | |
""" | |
dh = cap1xxx.Cap1188( | |
i2c_addr=0x2c, | |
alert_pin=25) | |
ledmap = [ | |
5, | |
4, | |
3, | |
2, | |
1, | |
0, | |
6, | |
7 | |
] | |
score = 0 | |
targets = set(range(8)) | |
target = random.choice(list(targets)) | |
def handle_press(event): | |
global target, score | |
if event.channel == target: | |
score += 1 | |
dh.set_led_state(ledmap[target], False) | |
def handle_release(event): | |
global target, targets | |
if event.channel == target: | |
# sometimes the LED doesn't switch off in handle_press | |
dh.set_led_state(ledmap[target], False) | |
# don't select the same target twice in a row | |
target = random.choice(list(targets.difference([target]))) | |
dh.set_led_state(ledmap[target], True) | |
for x in range(8): | |
dh.on(x,event='press', handler=handle_press) | |
dh.on(x,event='release', handler=handle_release) | |
dh._write_byte(cap1xxx.R_LED_LINKING, 0b000000) | |
print("3...") | |
time.sleep(1) | |
print("2...") | |
time.sleep(1) | |
print("1...") | |
time.sleep(1) | |
print("Go!") | |
start_time = time.time() | |
dh.set_led_state(ledmap[target], True) | |
while time.time() - start_time < 30: | |
time.sleep(0.1) | |
dh.set_led_state(ledmap[target], False) | |
print("You scored %d" % score) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment