Skip to content

Instantly share code, notes, and snippets.

@anirudhjayaraman
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anirudhjayaraman/99dacd3081e81a9174fa to your computer and use it in GitHub Desktop.
Save anirudhjayaraman/99dacd3081e81a9174fa to your computer and use it in GitHub Desktop.
Code for IIPP Mini Project [Stopwatch]
# "Stopwatch: The Game" created by Anirudh Jayaraman
# dated Wed Jun 17, 2015 12:18 PM IST
import simplegui
# define global variables
canvas_width = 850; canvas_height = 300; button_size = 100
timer_interval = 100; current_time = 0; played = 0; won = 0
running = False
rules_first_line = "Number of successful stops at a whole second"
rules_second_line = "versus the total number of stops"
# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
def format(t):
D = t % 10; C = (((t - t%10)/10)%60) % 10
B = (((t - t%10)/10)%60) / 10; A = ((t - t%10)/10)/60
return str(A) + ":" + str(B) + str(C) + "." + str(D)
# define event handlers for buttons
def start():
global current_time, played, won, score, running
timer.start(); running = True
def stop():
global current_time, won, played, running
t = current_time
if running == True:
timer.stop(); played += 1; running = False
if t % 10 == 0: won += 1
def reset():
global current_time, played, won, running; timer.stop()
current_time = 0; played = 0; won = 0; running = False
# define event handler for timer with 0.1 sec interval
def timer_handler():
global current_time; current_time +=1
# define draw handler
def draw(canvas):
global score, current_time, rules
score = str(won) + "/" + str(played)
canvas.draw_text(format(current_time), [250,250], 150, "Aqua")
canvas.draw_text(score, [700, 55], 55, "White")
canvas.draw_text(rules_first_line, [20,30], 18, "White")
canvas.draw_text(rules_second_line, [20,60], 18, "White")
# create frame
frame = simplegui.create_frame("Stopwatch", canvas_width, canvas_height)
# register event handlers
start_button = frame.add_button("Start", start, button_size)
stop_button = frame.add_button("Stop", stop, button_size)
reset_button = frame.add_button("Reset", reset, button_size)
frame.set_draw_handler(draw)
timer = simplegui.create_timer(timer_interval, timer_handler)
# start frame
frame.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment