Coursera——Mini-project description - "Stopwatch: The Game"

Mini-project description - “Stopwatch: The Game”
详见请看https://www.coursera.org/learn/interactive-python-1/supplement/MoH55/mini-project-description

import simplegui

CANVAS_WIDTH = 300
CANVAS_HEIGHT = 150
success = 0
time = 0
attempts = 0

#define init
def init():
    global time,success,attempts
    time = 0
    success = 0
    attempts = 0
    
#define helper function format that convert time
# in tenths of seconds into formatted string A:BC.D
def format(t):
    minuties = str(t // 600)
    secs = str(t % 600)
    while len(secs)<3:
        secs = "0" + secs
        
    return minuties + ":" + secs[:-1] + "." + secs[-1]

def mark_attempts():
    global attempts,success
    if timer.is_running:
        attempts += 1
        
    if not time % 10 :
        success += 1
        
#define helper function buttons 
def start():
    timer.start()
    
def stop():
    mark_attempts()
    timer.stop()

def reset():
    timer.stop()
    init()
    
    
def tick():
    global time
    time += 1
    
#define helper function draw
def draw(canvas):
	#set the background of canvas
    frame.set_canvas_background("silver")
        
    #draw success / attempts   
    text = str(success) + "/" + str(attempts)
    text_width = frame.get_canvas_textwidth(text,16,"sans-serif")
    pox = CANVAS_WIDTH - text_width - 6
    canvas.draw_text(text,(pox,20),16,"black","sans-serif")
    
    #draw counter
    counter_text = format(time)
    counter_width = frame.get_canvas_textwidth(counter_text,48,
                                               "sans-serif")
    pox = (CANVAS_WIDTH - counter_width) // 2
    canvas.draw_text(counter_text,(pox,(CANVAS_HEIGHT + 48)//2),48,
                     "black","sans-serif")

#create frame
frame = simplegui.create_frame("THE Game",CANVAS_WIDTH,CANVAS_HEIGHT)
timer = simplegui.create_timer(100,tick)
frame.set_draw_handler(draw)
#create buttons
button1 = frame.add_button("Start",start,100)
button2 = frame.add_button("Stop",stop,100)
button3 = frame.add_button("Reset",reset,100)

#start
frame.start()

你可能感兴趣的:(PYthon新手,个人笔记,练习题)