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_timertakes 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
strbefore 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.DwhereA, CandDare digits in the range 0-9 andBis in the range 0-5. Test this function independent of your project. Note that the string returned by your helper functionformatshould always correctly include leading zeros. For exampleformat(0) = 0:00.0format(11) = 0:01.1format(321) = 0:32.1format(613) = 1:01.3
- Insert a call to the
formatfunction 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"wherexis the number of successful stops andyis 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
Truewhen the stopwatch is running andFalsewhen 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 numbers back to zero when clicked.
format(t) separately.Code:
# Mini-Project 4: The Digital Stop Watch Game!
import simplegui
# Define global variables
mins, sec, count, x, y, z1, z2 = 0, 0, 0, 0, 0, 0, 0
# counting tenths of seconds into formatted string A:BC.D
def format(t):
global count, sec, mins
t = count
if (t == 10):
sec += 1
count = 0
if (sec == 60):
mins += 1
sec = 0
# Event handlers for buttons; "Start", "Stop", "Reset" and "Exit"
def start_timer():
global z1, z2
if z1-z2 == 0:
z1 += 1
timer.start()
def stop_timer():
global x, y, z1, z2
if z1-z2 == 1:
z2 += 1
timer.stop()
y += 1
if count == 0 :
x += 1
def reset_timer():
global count, sec, mins, x, y, z1,z2
timer.stop()
mins, sec, count, x, y,z1,z2 = 0, 0, 0, 0, 0, 0, 0
def exit_timer():
frame.stop()
# Event handler for timer with 0.1 sec interval
def timer_handler():
global count
count += 1
def draw_handler(canvas):
global count, sec, min, x, y
format(count)
canvas.draw_text(str(x)+"/"+str(y), (135,30), 30, "Red")
if (sec < 10):
canvas.draw_text(str(mins)+":"+ "0"+str(sec)+"."+str(count),(60,100),30,"White")
else:
canvas.draw_text(str(mins)+":"+str(sec)+"."+str(count),(60,100),30,"White")
# create frame
frame = simplegui.create_frame("Digital Stopwatch Game! ",200,200)
# register event handlers
timer = simplegui.create_timer(100,timer_handler)
frame.add_button("Start",start_timer,100)
frame.add_button("Stop ",stop_timer,100)
frame.add_button("Reset",reset_timer,100)
frame.add_button("Exit ",exit_timer,100)
frame.set_draw_handler(draw_handler)
# start frame
frame.start()
# http://www.codeskulptor.org/#user22_Cc7jRsc76bWmRuh.py
Result :

Hi;
ReplyDeleteI run the code in IDLE and received an error messsage, it does not recognize "simplegui" in the first statement: import simplegui
I believe simplegui is a third party module. You can probably run the code by taking out the simplegui parts, and run it within an interpreter, with only slight modifications. I'm actually trying that right now.
Delete