莱斯大学Python课程Mini-project# 3 - "Stopwatch: The Game"

要求在 这里:把 text drawing in the canvas和timers结合起来创建一个秒表, 上面有"Start", "Stop" 和 "Reset"按钮。

 

八个步骤:

1 创建一个计时器(interval of 0.1 seconds),它的 event handler(教学视频看的英文字幕,也不知道这些东西中文叫什么) 要联系到一个 global integer。这里要使用create_timer,其单位是千分之一秒。 注意: 不要用float,它的不精确性会让你想撞墙, Use an integer instead, i.e., 12 represents 1.2 seconds.

2 写出 canvas 的event handler ,让它在canvas中间显示当前时间。记得要把当前时间转换为  string才能显示出来。

3 添加 "Start" and "Stop" 按钮,它们的 event handlers 开始或结束这个计时器。然后再添加一个 "Reset" 按钮,用来停止计时器并把时间归零。当frame 打开的时候,这个计时器应该停止。

4  写一个 helper function format(t) that returns a string of the form A:BC.D where A, C and D are digits in the range 0-9 and B is in the range 0-5. 

  • format(0) = 0:00.0
  • format(11) = 0:01.1
  • format(321) = 0:32.1
  • format(613) = 1:01.3

提示: Use integer division and remainder (modular arithmetic) to extract various digits for the formatted time from the global integer timer.建议单独编写测试这一步

Insert a call to the format function into your draw handler to complete the stopwatch. (这个计时器只要计时 10分钟就可以)

6 把计时器用来考验反应快慢,添加两个计数器,记录你整秒(比如1.0, 2.0, 3.0秒.)按停的次数 。计数器应该在 canvas左上方,格式是 "x/y", y 是按停的总次数, x 指按对了的次数。 

7 添加一段代码,以确保当计数器已经停止时, 你再按"Stop"不会改变你的分数。 建议你用一个 global Boolean variable, 在计时器停的时候从True 变成。你可以用这个值来决定 在按"Stop"的时候是否更新成绩。

8 修改 "Reset" ,让你按它的时候时间归零。

import math
import simplegui
import random

# globals
current_tenthsec=0

goal=0 # this is X
counter=0 # this is y
message_goal="ready" # to display "x/y" 
running=False

# four event handlers
def timer_handler():
    global current_tenthsec
    current_tenthsec =current_tenthsec+1
    return current_tenthsec

def stop():
    timer.stop() 
    global counter
    global goal
    global message_goal
    global running
    
    if running==True:
      counter+=1  
      if current_tenthsec%10==0:
           goal+=1           
           running=False
            
    message_goal= str(goal)+"/"+str(counter)
    return message_goal
  
def start():
    timer.start()
    global running
    running=True

    
def reset():
    timer.stop() 
    global current_tenthsec

    global message_goal
    current_tenthsec=0
    counter=0
    goal=0

    message_goal="ready"
    return current_tenthsec


    
# returns a string of the form A:BC.D where A, C and D are digits in the range 0-9 and B 
def format(current_tenthsec):
    global message
    if current_tenthsec>=600:
     
       a=int(current_tenthsec/600)
       b=int(current_tenthsec%600/100)

        
    else:  
       a=0 
       b=int(current_tenthsec/100)
       
    c=int((current_tenthsec-a*600-b*100)/10)
    d=current_tenthsec-a*600-b*100-c*10   
    
    return str(a)+":"+str(b)+str(c)+"."+str(d)
    

    
# Handler to draw on canvas
def draw(canvas):
    canvas.draw_text(format(current_tenthsec), [120,100], 30, "Blue")
    canvas.draw_text(message_goal,[20,20],15,"Red")

# Create a frame and assign callbacks to event handlers
frame=simplegui.create_frame("Stopwatch",300,200)
timer = simplegui.create_timer(100, timer_handler)
text=frame.set_draw_handler(draw)
frame.add_button("Stop",stop,100)
frame.add_button("Start",start,100)
frame.add_button("Reset",reset,100)

# Start the frame animation
frame.start()
timer.stop() 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值