参加Coursera课程中的设计“Stopwatch game”
设计要求,在所出现的界面中需要含有以下几种功能:
1,开始计数
2,停止计时
3,重置计时
界面的显示的要求是
1,时间以0.1s逐渐增加,大于60s的进位到分
2,所有时间按照“0:00.0”的格式显示
如下图所示
设计的主要步骤为
1,将时间装换为标准格式的函数(def format(t))
2, 定义 start , stop , reset 函数
3, 定义计时函数(t=t+1)
4,设计 函数frame
5,创建计时函数(0.1s)
6, register event handler (eg, add button, set_draw_canvas)
7, frame.start()
the original code is below
# template for "Stopwatch: The Game"
# define global variables
import simplegui
milsecond=0
minute=0
second=0
t=0
display_str="0:00.0"
# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
'''this is a function that convert time to the format of "0:00.0"'''
def format(t):
global milsecond,sencond,minute
milsecond=t%10 #obtain the last section of time
minute=(t/10)/60#obtain the first section of time
if minute >=1:
second=t/10-minute*60
else:
second=t/10
#obtain the second and if second larger than 60,you should conver to
if second>=0 and second<=9:#if second
second='0'+str(second)
else:
second=str(second)
result=str(minute)+':'+second+'.'+str(milsecond)
return result
# define event handlers for buttons; "Start", "Stop", "Reset"
def start():
timer.start()
def stop():
timer.stop()
def reset():
global t
t=0
# define event handler for timer with 0.1 sec interval
def count():
global t
t+=1
# define draw handler
def draw(canvas):
canvas.draw_text(format(t),(200,200),80,"Red")
# create frame
frame=simplegui.create_frame("Stopwatch game",600,400)
# register event handlers
frame.add_button("Start",start)
frame.add_button("Stop",stop)
frame.add_button("Reset",reset)
frame.set_draw_handler(draw)
timer=simplegui.create_timer(100,count)
# start frame
frame.start()
# Please remember to review the grading rubric