= = Last week's homework, always forget to update
This week is to do a timer game. But because you can never stop in the right place and cause the test to be troublesome--
There is one problem:
It seems that it is not possible to change the output of a string every time a change occurs, but a function that can only be converted to a string in a parameter can display T T at a normal point in time.
I don't know why.
http://www.codeskulptor.org/#user40_Dfn3DhnkwjfR7Mb. py
Import simplegui# DefineGlobalVariablestime=0isrunning=FALSESUCC=0tot=0# define helper function format that converts time#inchTenths of seconds into formattedstringA:BC. Ddef format (t): D= t%Ten; T/=Ten; A= t/ -; B= t% -C= B%Ten; B/=Ten returnSTR (A) +":"+str (B) +str (C) +"."+Str (D) # defineEventHandlers forButtons"Start","Stop","Reset"def start_game ():GlobalTime , isrunning isrunning=True Timer.start () def stop_game ():Globaltot, isrunning, succ timer.stop ()ifIsRunning = =True:tot+=1 ifTime%Ten==0: Succ+=1isrunning=False def Clear ():GlobalTime , succ, tot time=0succ=0tot=0isrunning=False timer.stop () # defineEventHandler forTimer with0.1sec Intervaldef Tick ():Global Time Time+=1# define Draw Handlerdef Draw (canvas): Canvas.draw_text (format time), [ $,215], +," White") Canvas.draw_text (str (SUCC)+'/'+str (TOT), [340, -], -,"Blue") # Create FRAMEF= Simplegui.create_frame ("Timer Game", -, -) Timer= Simplegui.create_timer ( -, tick) start= F.add_button ("Start", Start_game) Stop= F.add_button ("Stop", stop_game) Reset= F.add_button ("Reset", Clear) # RegisterEventHandlersf.set_draw_handler (Draw) # start Framef.start () # Remember to review the grading rubricView Code
Mini-project # 3-"Stopwatch:the Game"