Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project #3 —"Stopwatch: The Game"
Mini-project description - "Stopwatch: The Game"
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:
Mini-project development process
- Construct a timer with an associated interval of 0.1 seconds whose event handler increments a global integer. (Remember that
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. - Write the event handler function for the canvas that draws the current time (simply as an integer, you should not worry about formatting it yet) in the middle of the canvas. Remember that you will need to convert the current time into a string using
str
before drawing it. - Add "Start" and "Stop" buttons whose event handlers start and stop the timer. Next, add a "Reset" button that stops the timer and reset the current time to zero. The stopwatch should be stopped when the frame opens.
- Next, write a helper function
format(t)
that returns a string of the formA:BC.D
whereA, C
andD
are digits in the range 0-9 andB
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 offormat
into the template.) Note that the string returned by your helper functionformat
should always correctly include leading zeros. For exampleformat(0) = 0:00.0
format(11) = 0:01.1
format(321) = 0:32.1
format(613) = 1:01.3
- Insert a call to the
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.) - Finally, to turn your stopwatch into a test of reflexes, add to two numerical counters that keep track of the number of times that you have stopped the watch and how many times you manage to stop the watch on a whole second (1.0, 2.0, 3.0, etc.). These counters should be drawn in the upper right-hand part of the stopwatch canvas in the form
"x/y"
wherex
is the number of successful stops andy
is number of total stops. My best effort at this simple game is around a 25% success rate. - Add code to ensure that hitting the "Stop" button when the timer is already stopped does not change your score. We suggest that you add a global Boolean variable that is
True
when the stopwatch is running andFalse
when the stopwatch is stopped. You can then use this value to determine whether to update the score when the "Stop" button is pressed. - Modify "Reset" so as to set these counters back to zero when clicked
-
# 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