= =上周的作业,一直忘记更新啦
这周是做一个定时器游戏。。然而因为永远不能停在正确的位置导致测试好麻烦 - -
有一个问题是:
好像不能每次发生改变的时候再用改变字符串的形式使输出改变,而只能在参数里调转成字符串的函数才能在正常的时间点显示T T
原因不知。。
http://www.codeskulptor.org/#user40_Dfn3DhnkwjfR7Mb.py
import simplegui # define global variables time = 0 isrunning = False succ = 0 tot = 0 # define helper function format that converts time # in tenths of seconds into formatted string A:BC.D def format(t): D = t % 10; t /= 10; A = t / 60; B = t % 60 C = B % 10; B /= 10 return str(A)+":"+str(B)+str(C)+"."+str(D) # define event handlers for buttons; "Start", "Stop", "Reset" def start_game(): global time, isrunning isrunning = True timer.start() def stop_game(): global tot, isrunning, succ timer.stop() if isrunning == True: tot += 1 if time % 10 == 0 : succ += 1 isrunning = False def Clear(): global time, succ, tot time = 0 succ = 0 tot = 0 isrunning = False timer.stop() # define event handler for timer with 0.1 sec interval def tick(): global time time += 1 # define draw handler def draw(canvas): canvas.draw_text(format(time), [200,215],32,"White") canvas.draw_text(str(succ)+'/'+str(tot), [340,20], 28, "Blue") # create frame f = simplegui.create_frame("Timer Game", 400, 400) timer = simplegui.create_timer(100, tick) start = f.add_button("Start", start_game) stop = f.add_button("Stop", stop_game) reset = f.add_button("Reset", Clear) # register event handlers f.set_draw_handler(draw) # start frame f.start() # Please remember to review the grading rubric