''' Mission 9 Remix 4B (Extra Spicy) Button A will start the program Then Button A will "spin" the colors of two rectangles. One function is used and two random colors are generated. Then two the rectangles are displayed. After the "spinning" the final colors display. The spins counter is incremented and the two colors are compared. End the game if they are the same. Button B will reset the spins so you can play again. Also Used button D to end game. ''' from codex import * import random from time import sleep # Create a list of bright colors to avoid dark colors on the display my_colors = [YELLOW, WHITE, ORANGE, PINK, CYAN, MAGENTA, GRAY, GREEN, RED] # Introduction to program def intro(): # Intro display.clear() display.print("COLOR MATCHING") display.print("PROGRAM") display.print("A = spin") display.print("B = restart") display.print("D = Stop") while True: if buttons.was_pressed(BTN_A): break # generates random color to display for each rectangle def color_spin(count): global spins loops = 0 delay = 0.04 display.clear() while loops < count: square_color1 = random.choice(my_colors) square_color2 = random.choice(my_colors) display.fill_rect(0, 0, 118, 240, square_color1) display.fill_rect(120, 0, 118, 240, square_color2) sleep(delay) delay = delay + 0.005 loops = loops + 1 # increment spin and compare two colors spins = spins + 1 if square_color1 == square_color2: ending() def ending(): text = "Spins to same: " + str(spins) display.draw_text(text, 20, 120, color=BLACK, scale=2) def restart(): global spins spins = 0 intro() # Main program intro() spins = 0 while True: if buttons.was_pressed(BTN_A): count = random.randrange(5, 10) color_spin(count) if buttons.was_pressed(BTN_B): restart() if buttons.was_pressed(BTN_D): break # Ending display.clear() display.print("DONE", scale=5)