mini project : stopwatch

是在coursea上一门课程的作业,挺简单,还是有点意思的。老外的教学方式很值得国内高校学习。

任务也蛮简单的,就是写下面一个界面,和平常用的秒表功能相似。



我的代码如下(http://www.codeskulptor.org/#user12_CqTdPE3RLq_0.py):

# template for "Stopwatch: The Game"

import simplegui

# define global variables
# how much tenths second have passed
total_time = 0
win_number = 0
total_number = 0
display_string = "0:00.0"
running = False

# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
def format(t):    
    result = ""
    tenth = t%10
    seconds = ((t-tenth)/10)%60
    minutes = ((t-tenth)/10 - seconds)/60
    minutes = minutes%10
    result += str(minutes)+":"
    if seconds < 10:
        result += "0"+str(seconds)
    else:
        result += str(seconds)
    result += "."+str(tenth)
    return result
    
# define event handlers for buttons;
# start watch handler
def start_watch():
    global running
    running = True
    timer.start()
    return 0

# stop watch handler
def stop_watch():
    global running
    global total_number
    global win_number
    global total_time
    
    if running == False:
        return 0
    if total_time%10 == 0:
        win_number += 1
    total_number += 1
    timer.stop()
    return 0

# reset watch handler
def reset_watch():
    
    global total_number
    global win_number
    global total_time
    global display_string
    global running
    
    total_number = 0
    win_number = 0
    total_time = 0
    display_string = "0:00.0"
    if running == True:
        running = False
        timer.stop()
    return 0

# define event handler for timer with 0.1 sec interval
def timer_process():
    global running
    global total_time
    global display_string
    
    if running == True:
        total_time += 1
        display_string = format(total_time)
    return 0

# define draw handler
def draw_frame(canvas):
    global total_time
    global win_number
    global total_number
    canvas.draw_text(format(total_time), [130, 150], 50, "White")
    canvas.draw_text(str(win_number)+"/"+str(total_number), [350, 30], 30, "White")
    return 0
    
# create frame
frame = simplegui.create_frame("Stopwatch", 400, 300)

# set draw handler for frame
frame.set_draw_handler(draw_frame)

# create a new timer
timer = simplegui.create_timer(100, timer_process)

# add butttons to frame
start_button = frame.add_button("Start", start_watch, 100)
stop_button = frame.add_button("Stop", stop_watch, 100)
reset_button = frame.add_button("Reset", reset_watch, 100)

# register event handlers


# start frame
frame.start()

# Please remember to review the grading rubric


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Stopwatch是一个计时器工具类,可以用来计算程序部分代码的执行时间。使用Stopwatch可以更方便地计算耗时,而不需要使用System.currentTimeMillis()来手动计算。以下是Stopwatch的用法示例: 1. 创建Stopwatch实例: StopWatch stopWatch = new StopWatch("测试"); 2. 开始计时: stopWatch.start("任务1"); 3. 执行任务代码: // 执行任务1的代码 4. 停止计时: stopWatch.stop(); 5. 获取任务耗时: StopWatch.TaskInfo\[\] taskInfo = stopWatch.getTaskInfo(); for (StopWatch.TaskInfo info : taskInfo) { System.out.println(info.getTimeMillis()); } 6. 获取总体耗时: long totalTime = stopWatch.getTotalTimeMillis(); 7. 获取上一个任务的耗时: long lastTaskTime = stopWatch.getLastTaskTimeMillis(); 8. 格式化输出计时结果: String formattedTime = stopWatch.prettyPrint(); 9. 检查计时器是否在运行中: boolean isRunning = stopWatch.isRunning(); 请注意,Stopwatch实例一次只能开启一个任务,不能同时start多个任务。必须在一个任务stop之后才能开启新的任务。如果需要同时开启多个任务,需要创建不同的Stopwatch实例。 以上是Stopwatch的基本用法,你可以根据需要进行相应的调整和扩展。 #### 引用[.reference_title] - *1* [Stopwatch用法](https://blog.csdn.net/qq_38261544/article/details/120333259)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [springboot学习(七十四) spring中时钟计时器StopWatch的使用](https://blog.csdn.net/u011943534/article/details/128219115)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值