作业Week3 "Stopwatch: The Game"

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__ = 'Stopwatch'
__author__ = 'Steve'
__mtime__ = '2017/9/25'
"""

# template for "Stopwatch: The Game"


import SimpleGUICS2Pygame.simpleguics2pygame as simplegui

# define global variables
t = 0
t_color = "White"
times = 0
score = 0
running = False


# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
def format(t):
    D = t % 10
    A = t / 600
    (B, C) = divmod(t % 600 / 10, 10)
    return str(A) + ":" + str(B) + str(C) + "." + str(D)


# define event handlers for buttons; "Start", "Stop", "Reset"
def start():
    global t_color, running
    timer.start()
    t_color = "White"
    running = True


def stop():
    global t_color, score, times, running
    timer.stop()
    # 补上判断timer是否running阶段
    if running == True:
        times += 1
        if t % 10 == 0:
            t_color = "Gold"  # 添加了变色的提示
            score += 1
        running = False


def reset():
    global t, t_color, score, times, running
    t = 0
    timer.stop()
    t_color = "White"
    times = 0
    score = 0
    running = False


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


# define draw handler
def draw(canvas):
    global t_color
    score_message = "Score: " + str(score) + "/" + str(times)
    canvas.draw_text(format(t), [80, 110], 36, t_color)
    canvas.draw_text(score_message, [200, 25], 20, "Yellow")


# create frame
f = simplegui.create_frame("Stopwatch", 300, 200)

# register event handlers
f.add_button("Start", start, 200)
f.add_button("Stop", stop, 200)
f.add_button("Reset", reset, 200)
f.set_draw_handler(draw)
timer = simplegui.create_timer(100, timer_handler)

# start frame
f.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、付费专栏及课程。

余额充值