作业Week3 "Stopwatch: The Game"

来源:互联网 发布:c语言实现http请求 编辑:程序博客网 时间:2024/05/24 13:28
#!/usr/bin/env python# -*- coding: utf-8 -*-"""__title__ = 'Stopwatch'__author__ = 'Steve'__mtime__ = '2017/9/25'"""# template for "Stopwatch: The Game"import SimpleGUICS2Pygame.simpleguics2pygame as simplegui# define global variablest = 0t_color = "White"times = 0score = 0running = False# define helper function format that converts time# in tenths of seconds into formatted string A:BC.Ddef format(t):    D = t % 10    A = t / 600    (B, C) = divmod(t % 600 / 10, 10)    return str(A) + ":" + str(B) + str(C) + "." + str(D)# define event handlers for buttons; "Start", "Stop", "Reset"def start():    global t_color, running    timer.start()    t_color = "White"    running = Truedef stop():    global t_color, score, times, running    timer.stop()    # 补上判断timer是否running阶段    if running == True:        times += 1        if t % 10 == 0:            t_color = "Gold"  # 添加了变色的提示            score += 1        running = Falsedef reset():    global t, t_color, score, times, running    t = 0    timer.stop()    t_color = "White"    times = 0    score = 0    running = False# define event handler for timer with 0.1 sec intervaldef timer_handler():    global t    t += 1# define draw handlerdef draw(canvas):    global t_color    score_message = "Score: " + str(score) + "/" + str(times)    canvas.draw_text(format(t), [80, 110], 36, t_color)    canvas.draw_text(score_message, [200, 25], 20, "Yellow")# create framef = simplegui.create_frame("Stopwatch", 300, 200)# register event handlersf.add_button("Start", start, 200)f.add_button("Stop", stop, 200)f.add_button("Reset", reset, 200)f.set_draw_handler(draw)timer = simplegui.create_timer(100, timer_handler)# start framef.start()# Please remember to review the grading rubric