Mini-project description-"Stopwatch:the Game"
Our mini-project for this week would focus on combining text drawing in the canvas with timers to build a simple digital st Opwatch that keeps track of the time in tenths of a second. The stopwatch should contain "Start", "Stop" and "Reset" buttons. Through this project, we suggest the 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_timertakes the interval specified in milliseconds.) This integer would 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 statemen ts. 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., represents 1.2 seconds.
Write the event handler function for the canvas this draws the current time (simply as a integer, you should not worry AB Out formatting it yet) in the middle of the canvas. Remember that you'll need to convert the current time into a string usingstrbefore 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 is stopped when the frame opens.
Next, write a helper functionformat(t)That returns a string of the formA:BC.DwhereA, CandDis digits in the range 0-9 andBis in the range 0-5. Test This function independent of your project using the This testing template http://www.codeskulptor.org/#examples-format_t emplate.py. (Just cut and paste your definition offormatinto the template.) Note that the string returned by your helper functionformatShould 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
Hint:Use integer division and remainder (modular arithmetic) to extract various digits for the formatted time from the global I Nteger timer.
Insert a call to theformatfunction into your draw handler to complete the stopwatch. (Note that the stopwatch need only work correctly up to ten minutes, beyond that it behavior is your choice.)
Finally, to turn your stopwatch into a test of reflexes, add to B numerical counters that keep track of the number of TI Mes that we 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"where is thexn Umber of successful stops and is number of totalystops. My best effort at this simple game is around a 25% success.
ADD code to ensure this hitting the "Stop" button when the timer was already stopped does not a change your score. We suggest that's add a global Boolean variable that's when the stopwatch are running and when theTrueFalsestopwatch 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
http://www.codeskulptor.org/#user40_VuvumcScXK0k0FO. py
Coursera-an Introduction to Interactive programming in Python (Part 1)-mini-project #3-"Stopwatch:the Game"