零基础项目制学习python(二)

参加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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值