# "Stopwatch: The Game"
import simplegui
# define global variables
current_time = 0 #for example: 12 represents 1.2 seconds.
total_win = 0
total_try = 0
flag = True
# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
def format(t):
#format_D tenths of seconds
#format_BC seconds
#format_A minutes
format_D = str(t % 10) # tenths of seconds
format_string = str(t)
temp1 = format_string[:(len(format_string)-1)]
if len(temp1) == 0:
format_BC = "00"
format_A = "0"
else:
format_BC = int(temp1) % 60
format_A = str(int(temp1) / 60)
if len(str(format_BC)) == 1:
format_BC = "0" + str(format_BC)
else:
format_BC = str(format_BC)
return format_A + ":" + format_BC + "." + format_D
# define event handlers for buttons; "Start", "Stop", "Reset"
def Start_button_handler():
timer.start()
flag = False
def Stop_button_handler():
timer.stop()
flag = True
if flag:
global total_try
total_try += 1
if current_time % 10 == 0:
global total_win
total_win +=1
def Reset_button_handler():
timer.stop()
global current_time
current_time = 0
global total_try
total_try = 0
global total_win
total_win = 0
# define event handler for timer with 0.1 sec interval
def timer_handler():
global current_time
current_time += 1
# define draw handler
def draw_handler(canvas):
canvas.draw_text(format(current_time), (60, 80), 24, 'White')
canvas.draw_text(str(total_win) + '/' + str(total_try), (110, 20), 20, 'Red')
# create frame
frame = simplegui.create_frame('Timer', 150, 150)
frame.set_draw_handler(draw_handler)
Start_button = frame.add_button('Start', Start_button_handler,50)
Stop_button = frame.add_button('Stop', Stop_button_handler, 50)
Reset_button = frame.add_button('Reset', Reset_button_handler, 50)
timer = simplegui.create_timer(100, timer_handler)
# register event handlers
# start frame
frame.start()
# Please remember to review the grading rubric
Coursera_An Introduction to Interactive Programming in Python_Mini-project # 3 Stopwatch
最新推荐文章于 2021-03-31 14:50:30 发布