欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

作业Week3 "Stopwatch: The Game"

程序员文章站 2022-06-06 08:38:17
...
#!/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
相关标签: python 莱斯大学