Mini-project description - “Stopwatch: The Game”

Mini-project description - “Stopwatch: The Game”#

# template for "Stopwatch: The Game"
try:
    import simplegui
except ModuleNotFoundError:
    import simplequi as simplegui

WIDTH = 200
HEIGHT = 200

# define global variables
i = 0
stop_count = 0
success_count = 0


# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
def format(t):
    minutes = i // 600
    seconds = i // 10 % 600
    hundred_milliseconds = t % 10
    return f'{minutes}:{seconds:02}.{hundred_milliseconds}'


# define event handlers for buttons; "Start", "Stop", "Reset"
def start():
    t.start()


def stop():
    global stop_count, success_count

    t.stop()
    stop_count += 1

    if i % 10 == 0:
        success_count += 1


def reset():
    global i, stop_count, success_count
    i = 0
    stop_count = 0
    success_count = 0


# define event handler for timer with 0.1 sec interval
def tick():
    global i
    i += 1


# define draw handler
def draw(c):
    c.draw_text(format(i), [10, HEIGHT//4*3], 48, 'yellow')
    c.draw_text(f'{stop_count}/{success_count}', [WIDTH-70, 50], 48, 'yellow')


# create frame
f = simplegui.create_frame('Stopwatch: The Game', WIDTH, HEIGHT)
t = simplegui.create_timer(100, tick)

# register event handlers
f.set_draw_handler(draw)
f.add_button('Start', start, 200)
f.add_button('Stop', stop, 200)
f.add_button('Reset', reset, 200)

# start frame
f.start()

# Please remember to review the grading rubric