Created
February 15, 2016 10:10
-
-
Save ntoll/d0493fcab5bb68af607e to your computer and use it in GitHub Desktop.
A brightness acuity game. See copious comments in-line. Runs on the BBC micro:bit.
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
# Light acuity game. A pixel will change on either the | |
# left or the right. Click the appropriate button within | |
# an ever decreasing amount of time to increase your score. | |
# The game is over when you get it wrong or don't answer | |
# in time. | |
# Based on an idea by Dave Gibbs. | |
# By Nicholas Tollervey, Feb. 2016. Released to the public domain. | |
from microbit import * | |
import random | |
def change_pixel(): | |
# Randomly selects a pixel on the left or the right and changes | |
# its intensity. Returns the x coordinate. | |
x = random.choice([0,1,3,4]) # Don't select the middle column | |
y = random.randint(0, 4) # Y row can be any row within range 0, 4 | |
# These lines ensure there's always a change of intensity from | |
# the current value. | |
current_intensity = display.get_pixel(x, y) | |
valid_intensity = list(range(0, 10)) | |
valid_intensity.remove(current_intensity) | |
# Remove the intensity values either side of the current one so | |
# that there's enough of a difference in contrast. | |
if current_intensity < 9: | |
valid_intensity.remove(current_intensity + 1) | |
if current_intensity > 0: | |
valid_intensity.remove(current_intensity - 1) | |
# Select a new intensity and update the display. | |
new_intensity = random.choice(valid_intensity) | |
display.set_pixel(x, y, new_intensity) | |
# Return x so the game can judge which button to detect. | |
return x | |
def play(): | |
start_image = Image("99999:" * 5) # all pixels at full intensity | |
wait = 2000 # you have 2 seconds to get it right (at the start) | |
score = 0 | |
# play the game! | |
display.show(start_image) | |
sleep(wait) | |
# Game loop | |
while True: | |
wait -= 100 # Speed up the wait time. | |
x = change_pixel() # Make a change. | |
sleep(wait) # Wait a while... | |
if x < 2 and button_a.was_pressed(): | |
# Button A was pressed and change was on left. | |
score += 1 # Correct! | |
elif x > 2 and button_b.was_pressed(): | |
# Button B was pressed and change was on right. | |
score += 1 # Correct! | |
else: | |
# All other cases = end of game. | |
break | |
display.scroll("Score = {}".format(score)) | |
play() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment