''' Mission 10 - Reaction Tester Spicy solution - 3B Make the reaction tester into a game by using two images. Award a point if the correct button is pressed, depending on what image is displayed. As an extra challenge, end when they player misses. This solution uses a list of two images. It can be done with two variables as well. This solution also includes the functions for intro and countdown. ''' from codex import * import random import time my_images = [pics.TIARA, pics.HOUSE] def wait_button(): while True: global button if buttons.was_pressed(BTN_A): button = "A" break elif buttons.was_pressed(BTN_B): button = "B" break # -- add intro function def intro(): display.print("Reaction Tester") display.print("TIARA = A") display.print("HOUSE = B") display.print() display.print("Press A to start") wait_button() display.clear() def countdown(): # clear screen and countdown display.clear() display.print("3", scale=6) time.sleep(1) display.print("2", scale=6) time.sleep(1) display.print("1", scale=6) time.sleep(1) display.clear() # -- call intro AVG = 200 # average of reaction times (can vary) score = 0 intro() while True: display.print("Press Button A or B") wait_button() countdown() # get random delay time ms = random.randrange(1000, 5000) delay_time = ms / 1000 time.sleep(delay_time) # turn pixels GREEN buttons.was_pressed(BTN_A) buttons.was_pressed(BTN_B) image = random.choice(my_images) display.show(image) # get start and end time start_time = time.ticks_ms() wait_button() end_time = time.ticks_ms() reaction_time = time.ticks_diff(end_time, start_time) if image == pics.TIARA and button == "A": score = score + 1 elif image == pics.HOUSE and button == "B": score = score + 1 else: break display.print("Reaction Time: ") display.print(reaction_time) display.print("Score: " + str(score)) display.print() # -- ending message display.clear() display.print("Final Score") display.print(score)