''' Day 3 Remix - Advanced Level Continuous play until BTN_A is pressed ''' from codex import * from time import sleep import random # ---- Global Variables ------- delay = 1 count = 0 keepGoing = True # ---- Functions ----- # Introduction to dice roll high/low game - gives instructions def one_roll(): display.fill_circle(125, 125, 30, WHITE) def two_roll(): display.fill_circle(50, 50, 30, WHITE) display.fill_circle(200, 200, 30, WHITE) def three_roll(): display.fill_circle(50, 50, 30, WHITE) display.fill_circle(125, 125, 30, WHITE) display.fill_circle(200, 200, 30, WHITE) def four_roll(): display.fill_circle(200, 50, 30, WHITE) display.fill_circle(50, 200, 30, WHITE) two_roll() def five_roll(): four_roll() one_roll() def six_roll(): four_roll() display.fill_circle(50, 125, 30, WHITE) display.fill_circle(200, 125, 30, WHITE) def display_roll(num): if num == 1: one_roll() elif num == 2: two_roll() elif num == 3: three_roll() elif num == 4: four_roll() elif num == 5: five_roll() else: six_roll() def intro(): display.draw_text("Roll Dice", scale=3, x=35, y=80) display.draw_text("Game", scale=4, x=35, y=120) sleep(delay) display.clear() display.draw_text("Press U for HIGHER", scale=2, x=10, y=80) display.draw_text("Press D for LOWER", scale=2, x=10, y=120) display.draw_text("Press B to start", scale = 2, color=RED, x=35, y=180) sleep(delay*3) # Displays winning or losing message def win_lose(count): if count == 4: display.clear() display.draw_text("You WON", scale=4, color=BLUE, x=30, y=80) else: display.clear() display.draw_text("You LOST", scale=4, color=RED, x=30, y=80) # Displays game over message (Done) def ending(): display.clear() display.draw_text("DONE", scale=5, color=RED, x=55, y=100) sleep(delay*2) display.clear() pixels.set(0, BLACK) pixels.set(1, BLACK) pixels.set(2, BLACK) pixels.set(3, BLACK) # Resets all variables and pixels for new high/low game def reset(): global count, keep_going count = 0 keep_going = True pixels.set(0, BLACK) pixels.set(1, BLACK) pixels.set(2, BLACK) pixels.set(3, BLACK) display.clear() # Receives the two dice rolls and determines if a point is awarded def check_rolls(a, b, num2): global count, keep_going display.clear() sleep(delay) display_roll(num2) if a <= b: pixels.set(count, GREEN) sleep(delay) count = count + 1 if count == 4: keep_going = False else: sleep(delay) keep_going = False # ----- MAIN PROGRAM ---- # Start GAME with intro and then loop intro() while True: # Start game with button B if buttons.was_pressed(BTN_B): # Reset the board for each game reset() # Select first random number num1 = random.randrange(6) + 1 display_roll(num1) sleep(delay) while keep_going: # Get the next random dice roll num2 = random.randrange(6) + 1 # Check to see if the U button was pressed for a higher number if buttons.was_pressed(BTN_U): check_rolls(num1, num2, num2) num1 = num2 # Check to see if the D button was pressed for a lower number elif buttons.was_pressed(BTN_D): check_rolls(num2, num1, num2) num1 = num2 # Win/Loss message win_lose(count) # Play until A is pressed if buttons.was_pressed(BTN_A): break # Ending message ending()