Created
June 25, 2015 18:30
-
-
Save fsjoyti/7fc13fb32d5e87f1c50e to your computer and use it in GitHub Desktop.
Stopwatch game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import simplegui | |
time = 0 | |
successful_stops = 0 | |
total_stops = 0 | |
running = False | |
minutes = 0 | |
tens_of_seconds = 0 | |
seconds_in_excess = 0 | |
tenths_of_seconds = 0 | |
def format(time): | |
global minutes,tens_of_seconds,seconds_in_excess,tenths_of_seconds | |
minutes = time // 600 | |
tens_of_seconds = (time%600)//100 | |
seconds_in_excess= ((time%600)//10)%10 | |
tenths_of_seconds = time % 10 | |
return ""+str(minutes)+":"+str(tens_of_seconds)+""+ str(seconds_in_excess)+"."+str(tenths_of_seconds) | |
def start_button(): | |
global running | |
running = True | |
timer.start(); | |
def stop_button(): | |
global running,successful_stops,total_stops,time,tenths_of_seconds | |
timer.stop() | |
if(running==True): | |
running = False | |
total_stops +=1 | |
if (time%10==0): | |
successful_stops = successful_stops+1 | |
display_score() | |
def reset_button(): | |
global minutes,tens_of_seconds,seconds_in_excess,tenths_of_seconds | |
global running,sucessful_stops,total_stops,time | |
running = False | |
minutes = 0 | |
tens_of_seconds = 0 | |
seconds_in_excess = 0 | |
tenths_of_seconds = 0 | |
sucessful_stops = 0 | |
total_stops = 0 | |
time = 0 | |
timer.stop() | |
def time_handler(): | |
global time | |
time = time + 1 | |
def display_score(): | |
global sucessful_stops,total_stops | |
return ""+str(successful_stops)+"/"+str(total_stops) | |
# Handler to draw on canvas | |
def draw(canvas): | |
canvas.draw_text(format(time), [100,112], 48, "Blue") | |
canvas.draw_text(display_score(), [199,20], 30, "Teal") | |
# Create a frame and assign callbacks to event handlers | |
frame = simplegui.create_frame("Stopwatch game", 350, 200) | |
frame.add_button("Start", start_button) | |
frame.add_button("Stop", stop_button) | |
frame.add_button("Reset", reset_button) | |
frame.set_draw_handler(draw) | |
timer = simplegui.create_timer(100,time_handler) | |
# Start the frame animation | |
frame.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment