标签:
Our mini-project for this week will focus on combining text drawing in the canvas with timers to build a simple digital stopwatch that keeps track of the time in tenths of a second. The stopwatch should contain "Start", "Stop" and "Reset" buttons. To help guide you through this project, we suggest that you download the provided program template for this mini-project and build your stopwatch program as follows:
create_timer
takes the interval specified in milliseconds.) This integer will keep track of the time in tenths of seconds. Test your timer by printing this global integer to the console. Use the CodeSkulptor reset button in the blue menu bar to terminate your program and stop the timer and its print statements. Important: Do not use floating point numbers to keep track of tenths of a second! While it‘s certainly possible to get it working, the imprecision of floating point can make your life miserable. Use an integer instead, i.e., 12 represents 1.2 seconds.str
before drawing it.format(t)
that returns a string of the form A:BC.D
where A, C
and D
are digits in the range 0-9 and B
is in the range 0-5. Test this function independent of your project using this testing template http://www.codeskulptor.org/#examples-format_template.py. (Just cut and paste your definition of format
into the template.) Note that the string returned by your helper function format
should always correctly include leading zeros. For example
format(0) = 0:00.0
format(11) = 0:01.1
format(321) = 0:32.1
format(613) = 1:01.3
format
function into your draw handler to complete the stopwatch. (Note that the stopwatch need only work correctly up to 10 minutes, beyond that its behavior is your choice.)"x/y"
where x
is the number of successful stops and y
is number of total stops. My best effort at this simple game is around a 25% success rate.True
when the stopwatch is running and False
when the stopwatch is stopped. You can then use this value to determine whether to update the score when the "Stop" button is pressed.# template for "Stopwatch: The Game" import simplegui # define global variables t = 0 t_str = "0:00.0" position = [60, 100] width = 200 height = 200 interval = 100 flag = False stop_num = 0 win_num = 0 score_str = str(win_num) + "/" + str(stop_num) # define helper function format that converts time # in tenths of seconds into formatted string A:BC.D def format(t): tens_second = t % 10 t = t // 10 second = t % 60 minute = t // 60 second_str = str(second) if second < 10: second_str = "0" + str(second) format_t = str(minute) + ":" + second_str + "." + str(tens_second) return format_t def update_score(): global stop_num global win_num global score_str global flag if not timer.is_running(): if flag: flag = False stop_num += 1 if t % 10 == 0: win_num += 1 else: flag = True score_str = str(win_num) + "/" + str(stop_num) def reset_score(): global flag global stop_num global win_num global score_str flag = False stop_num = 0 win_num = 0 score_str = str(win_num) + "/" + str(stop_num) # define event handlers for buttons; "Start", "Stop", "Reset" def start_handler(): timer.start() def stop_handler(): timer.stop() def reset_handler(): timer.stop() global t global t_str reset_score() # define event handler for timer with 0.1 sec interval def timer_handler(): global t global t_str t = t + 1 t_str = format(t) def timer_score_handler(): update_score() # define draw handler def draw_handler(canvas): canvas.draw_text(t_str, position, 36, "White") canvas.draw_text(score_str, [160, 20], 16, "green") # create frame frame = simplegui.create_frame("Stopwatch", width, height) # register event handlers frame.add_button("Start", start_handler) frame.add_button("Stop", stop_handler) frame.add_button("Reset", reset_handler) frame.set_draw_handler(draw_handler) timer = simplegui.create_timer(interval, timer_handler) timer_score = simplegui.create_timer(interval, timer_score_handler) # start frame frame.start() timer_score.start() # Please remember to review the grading rubric
标签:
原文地址:http://www.cnblogs.com/tonony1/p/4795651.html