Coursera_An Introduction to Interactive Programming in Python_Mini-project # 3 Stopwatch

来源:互联网 发布:义乌司法拍卖淘宝网站 编辑:程序博客网 时间:2024/06/09 23:45
# "Stopwatch: The Game"import simplegui# define global variablescurrent_time = 0  #for example: 12 represents 1.2 seconds.total_win = 0total_try = 0flag = True# define helper function format that converts time# in tenths of seconds into formatted string A:BC.Ddef format(t):    #format_D   tenths of seconds    #format_BC  seconds    #format_A   minutes    format_D = str(t % 10)  # tenths of seconds    format_string = str(t)    temp1 = format_string[:(len(format_string)-1)]        if len(temp1) == 0:        format_BC = "00"        format_A = "0"    else:        format_BC = int(temp1) % 60        format_A = str(int(temp1) / 60)        if len(str(format_BC)) == 1:            format_BC = "0" + str(format_BC)        else:            format_BC = str(format_BC)                return format_A + ":" + format_BC + "." + format_D    # define event handlers for buttons; "Start", "Stop", "Reset"def Start_button_handler():    timer.start()    flag = False    def Stop_button_handler():     timer.stop()      flag = True     if flag:        global total_try        total_try += 1        if current_time % 10 == 0:            global total_win            total_win +=1             def Reset_button_handler():    timer.stop()     global current_time    current_time = 0    global total_try    total_try = 0    global total_win    total_win = 0        # define event handler for timer with 0.1 sec intervaldef timer_handler():    global current_time    current_time += 1# define draw handlerdef draw_handler(canvas):    canvas.draw_text(format(current_time), (60, 80), 24, 'White')    canvas.draw_text(str(total_win) + '/' + str(total_try), (110, 20), 20, 'Red')# create frameframe = simplegui.create_frame('Timer', 150, 150)frame.set_draw_handler(draw_handler)Start_button = frame.add_button('Start', Start_button_handler,50)Stop_button = frame.add_button('Stop', Stop_button_handler, 50)Reset_button = frame.add_button('Reset', Reset_button_handler, 50)timer = simplegui.create_timer(100, timer_handler)# register event handlers        # start frameframe.start()# Please remember to review the grading rubric

0 0
原创粉丝点击