# Inspired by # https://projects.raspberrypi.org/en/projects/target-practice # and ported from Trinket # https://trinket.io/embed/python/f686c82d8a # https://trinket.io/python/f686c82d8a # to CodeSkulptor import random, simplegui # radii of circles outer = 85 inner = 55 middle = 15 centre_x = 200 centre_y = 200 # the mouse position doesn't matter, # only the position of the arrow (arrow_x, arrow_y) # at the time when the mouse was clicked def mouse_handler_click(position): if hit_distance <= middle: print('You hit the middle, 500 points!') elif hit_distance <= inner: print('You hit the inner circle, 200 points!') elif hit_distance <= outer: print('You hit the outer circle, 50 points!') else: print('You missed! No points!') def timer_handler(): global hit_distance # Can be used in other functions global arrow_x, arrow_y arrow_x = random.randint(100, 300) # Store a random number between 100 and 300 arrow_y = random.randint(100, 300) # Store a random number between 100 and 300 hit_distance = ((arrow_x - centre_x)**2 + (arrow_y - centre_y)**2)**0.5 def draw(canvas): # Things to do in every frame # sky - rectangle 400 x 250 canvas.draw_line((0, 125), (400, 125), 250, 'lightblue') # grass - rectangle 400 x 150 canvas.draw_line((0, 325), (400, 325), 150, 'lightgreen') # stand - triangle canvas.draw_polygon([[150, 350], [200, 150], [250, 350]], 5, 'brown', 'brown') # target canvas.draw_circle([centre_x, centre_y], outer, 1, 'blue', 'blue') canvas.draw_circle([centre_x, centre_y], inner, 1, 'red', 'red') canvas.draw_circle([centre_x, centre_y], middle, 1, 'yellow', 'yellow') # arrow # canvas.draw_circle(center_point, radius, line_width, line_color, fill_color) canvas.draw_circle([arrow_x, arrow_y], 8, 1, 'brown', 'brown') def main(): # Create a frame and assign callbacks to event handlers frame = simplegui.create_frame("Target Game", 400, 400) frame.set_canvas_background('white') frame.set_draw_handler(draw) frame.set_mouseclick_handler(mouse_handler_click) timer = simplegui.create_timer(700, timer_handler) # milliseconds timer.start() timer_handler() # set initial arrow position # Start the frame animation frame.start() if __name__ == '__main__': main()