Interactive Python:Mini-project # 3 - "Stopwatch: The Game"

本文介绍了一个使用Python交互式编程实现的迷你项目——Stopwatch游戏。玩家需要构建一个间隔为0.1秒的计时器,用整数记录时间(以十分之一秒为单位)。游戏包含“开始”、“停止”和“重置”按钮,要求在画布中央显示当前时间,并在右上角显示成功停止和总停止次数的计数器。玩家的目标是在整秒时停止计时器以获得分数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

An Introduction to Interactive Programming in Python 

Mini-project development process

  1. Construct a timer with an associated interval of 0.1 seconds whose event handler increments a global integer. (Remember thatcreate_timer takes the interval specified in milliseconds.) This integer will keep track of the time in tenths of seconds. Test your timer by printing this global integer to the console. Use the CodeSkulptor reset button in the blue menu bar to terminate your program and stop the timer and its print statements. Important: Do not use floating point numbers to keep track of tenths of a second! While it's certainly possible to get it working, the imprecision of floating point can make your life miserable. Use an integer instead, i.e., 12 represents 1.2 seconds.
  2. Write the event handler function for the canvas that draws the current time (simply as an integer, you should not worry about formatting it yet) in the middle of the canvas. Remember that you will need to convert the current time into a string using str before drawing it.
  3. Add "Start" and "Stop" buttons whose event handlers start and stop the timer. Next, add a "Reset" button that stops the timer and reset the current time to zero. The stopwatch should be stopped when the frame opens.
  4. Next, write a helper function format(t) that returns a string of the form A:BC.D where A, B, C and D are digits in the range 0-9. Test this function independent of your project. Note that the string returned by your helper function format should always correctly include leading zeros. For example
    • format(0) == 0:00.0
    • format(11) = 0:01.1
    • format(321) = 0:32.1
    • format(613) = 1:01.3
    Hint: Use integer division and remainder (modular arithmetic) to extract various digits for the formatted time from the global integer timer.
  5. Insert a call to the format function into your draw handler to complete the stopwatch. (Note that the stopwatch need only work correctly up to 10 minutes, beyond that its behavior is your choice.)
  6. Finally, to turn your stopwatch into a test of reflexes, add to two numerical counters that keep track of the number of times that you have stopped the watch and how many times you manage to stop the watch on a whole second (1.0, 2.0, 3.0, etc.). These counters should be drawn in the upper right-hand part of the stopwatch canvas in the form "x/y" where x is the number of successful stops and y is number of total stops. My best effort at this simple game is around a 25% success rate.
  7. Add code to ensure that hitting the "Stop" button when the timer is already stopped does not change your score. We suggest that you add a global Boolean variable that is True when the stopwatch is running and False when the stopwatch is stopped. You can then use this value to determine whether to update the score when the "Stop" button is pressed.
  8. Modify "Reset" so as to set these numbers back to zero when clicked.
Steps 1-3 and 5-7 above are relatively straightforward. However, step 4 requires some adept use of integer division and modular arithmetic. So, we again emphasize that you build and debug the helper function  format(t)  separately following the tips in the Code Clinic thread. Following this process will save you time. For an example of a full implementation, we suggest that you watch the video lecture on this mini-project.

代码如下:

# template for "Stopwatch: The Game"
import simplegui
# define global variables
t=0
time=''
suc = 0
all = 0


# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
def format(t):
    global time
    time=str(t//600)+':'+str((t%600)//100)+str((t%100)//10)+':'+str(t%10)
    #print time
    
# define event handlers for buttons; "Start", "Stop", "Reset"
def start():
    timer.start()
def stop():
    global all,suc
    if not timer.is_running():
        return
    timer.stop()
    all+=1
    if t%10 == 0:
        suc+=1
def reset():
    global t,suc,all
    t=0
    suc=0
    all=0


# define event handler for timer with 0.1 sec interval
def count():
    global t
    t+=1


# define draw handler
def draw(c):
    format(t)
    c.draw_text(time, (100, 200), 96, "Red")
    c.draw_text(str(suc)+'/'+str(all), (400, 50), 36, "Red")
    
# create frame
c=simplegui.create_frame("Stop Watch", 500, 500,100)


# register event handlers
c.set_draw_handler(draw)
c.add_button('Start',start,100)
c.add_button('Stop',stop,100)
c.add_button('Reset',reset,100)


timer=simplegui.create_timer(100, count)
# start frame
c.start()
#timer.start()


# Please remember to review the grading rubric

在线版本为 链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值