Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # template for "Stopwatch: The Game"
- import simplegui
- # define global variables
- tenths_of_seconds = 0
- minutes = 0
- seconds = 0
- tens_of_seconds = 0
- width = 500
- height = 500
- total = 0
- x = 0
- y = 0
- z = 0.0
- # define helper function format that converts time
- # in tenths of seconds into formatted string A:BC.D
- def format(t):
- global tenths_of_seconds, minutes, tens_of_seconds, seconds
- minutes = t//600
- tenths_of_seconds = t%10
- seconds = ((t//10)%60)//10
- tens_of_seconds = ((t//10)%60)%10
- return str(minutes) + ":" + str(seconds) + str(tens_of_seconds)+ "." + str(tenths_of_seconds)
- # define event handlers for buttons; "Start", "Stop", "Reset"
- def start():
- timer.start()
- def stop():
- global x, y, tenths_of_seconds, z
- if timer.is_running():
- x += 1
- if tenths_of_seconds == 0:
- y += 1
- z = (float(y)/float(x))*100
- timer.stop()
- def reset():
- timer.stop()
- global total, x, y, z
- total = 0
- x = 0
- y = 0
- z = 0.0
- # define event handler for timer with 0.1 sec interval
- def t_h():
- global total
- total += 1
- # define draw handler
- def draw_handler(canvas):
- canvas.draw_text(format(total), [200, height/2], 64, "White")
- canvas.draw_text(str(x) + "/" + str(y), [445, 50], 24, "Yellow")
- canvas.draw_text("Win percentage: " + str(round(z, 2)) + "%", [130, 450], 29, "Magenta")
- # create frame
- frame = simplegui.create_frame("Stopwatch: The Game", width, height)
- # register event handlers
- frame.set_draw_handler(draw_handler)
- timer = simplegui.create_timer(100, t_h)
- button1 = frame.add_button("Start", start, 100)
- frame.add_label(" ")
- button1 = frame.add_button("Stop", stop, 100)
- frame.add_label(" ")
- button2 = frame.add_button("Reset", reset, 100)
- # start frame
- frame.start()
Add Comment
Please, Sign In to add comment