Skip to content

Instantly share code, notes, and snippets.

@ivanko22
Created October 5, 2016 14:57
Show Gist options
  • Save ivanko22/923b7d03904a960806496ae0275717ed to your computer and use it in GitHub Desktop.
Save ivanko22/923b7d03904a960806496ae0275717ed to your computer and use it in GitHub Desktop.
# template for "Stopwatch: The Game"
# define global variables
import simplegui
count = 0
count2 = 0
count3 = 0
started = False
started2 = False
# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
def format(x):
minut = x / 600
sec = x / 10 % 60 / 10
sec2 = x / 10 % 60 % 10
msec = x % 10
watch = str(minut) + ":" + str(sec) + str(sec2) + "." + str(msec)
return watch
# define event handlers for buttons; "Start", "Stop", "Reset"
def start():
global started, started2
started = True
started2 = True
timer.start()
def stop():
global count, count2, count3, started, started2
if started2:
if count % 10 == 0:
count2 += 1
started2 = False
timer.stop()
else:
if started:
count3 += 1
started = False
timer.stop()
def reset():
global count, count2, count3
count = 0
count2 = 0
count3 = 0
timer.stop()
# define event handler for timer with 0.1 sec interval
def timer():
global count
count += 1
# define draw handler
def draw(canvas):
canvas.draw_text(str(format(count)), [90, 230], 86, "Green")
canvas.draw_text(str(count2) + " / ", [240, 70], 32, "Lime")
canvas.draw_text(str(count3), [280, 70], 32, "Red")
# create frame
frame = simplegui.create_frame("Swatch", 400, 400)
# register event handlers
frame.set_draw_handler(draw)
frame.add_button("Start", start, 75)
frame.add_button("Stop", stop, 75)
frame.add_button("Reset", reset, 75)
timer = simplegui.create_timer(100, timer)
frame.start()
# Please remember to review the grading rubric
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment