''' Mission 11 - Spirit Level - Remix 2B - Medium The remix challenge is to use both the x and y value for tilt. Lines will be vertical and horizontal. ''' from codex import * from time import sleep import math # center of display (240 x 240) CENTER = 120 # draw two lines on the screen # leave space in the middle for the circle def set_screen(): display.fill(WHITE) display.draw_line(0, CENTER, 105, CENTER, BLACK) display.draw_line(135, CENTER, 239, CENTER, BLACK) display.draw_line(CENTER, 0, CENTER, 105, BLUE) display.draw_line(CENTER, 135, CENTER, 239, BLUE) def scale_degrees(): global degrees_x, degrees_y # read from the accelerometer and get x value val = accel.read() tilt_x = val[0] tilt_y = val[1] # change tilt_x reading to degrees scaled_x = (tilt_x / 16384) scaled_x = min(max(scaled_x, -1), 1) degrees_x = math.asin(scaled_x) * 180 / math.pi degrees_x = int(degrees_x) # change tilt_y reading to degrees scaled_y = (tilt_y / 16384) scaled_y = min(max(scaled_y, -1), 1) degrees_y = math.asin(scaled_y) * 180 / math.pi degrees_y = int(degrees_y) def intro(): display.print("Spirit Level") display.print("") display.print("A to start") display.print("B to quit") while True: if buttons.was_pressed(BTN_A): break # define a variable to use for the circle's position x = CENTER y = CENTER score = 0 intro() set_screen() # start an infinite loop while True: scale_degrees() # erase current circle, recalculate, and draw new circle display.draw_text(str(score), x=20, y=20, scale=3, color=WHITE) display.draw_circle(x, y, 15, WHITE) x = CENTER + degrees_x y = CENTER + degrees_y if x == 80 or x == 160 or y == 80 or y == 160: score = score + 1 display.draw_circle(x, y, 15, ORANGE) display.draw_text(str(score), x=20, y=20, scale=3, color=RED) sleep(0.1) if x < 50 or x > 180 or y < 50 or y > 180: break if score == 4: break # ending display.clear() if score == 4: display.print("You win!", scale=4) elif score == 0: display.print("You hit a wall.") display.print("Game over") else: display.print("Your score: ") display.print(score)