Stopwatch “python”

Well as for this week, we were to create some sort of stopwatch game.
Player gets a point when, they hit “stop” button if it stops on seconds.
A bit even more trickier, but challenge was accepted and completed…Hell Yeah.

#http://www.codeskulptor.org/#user38_TSo5mURks3_2.py
#http://www.codeskulptor.org/#user38_TSo5mURks3_3.py
#or http://www.codeskulptor.org/#user38_nJvMd8w50P_0.py
# Mini-project #3 - "Stopwatch: The Game"
# Created by Mpho "!nerd" Mphego - Marion Island SANSA Engineer 2014-2015
#
# 'Introduction to Interactive Programming in Python' Course
# RICE University - https://class.coursera.org/interactivepython-005
# by Joe Warren, John Greiner, Stephen Wong, Scott Rixner
# 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.

try:
    import simplegui
except:
    import SimpleGUICS2Pygame.simpleguics2pygame as simplegui
    # To run simplegui in idle python, install SimpleGUICS2Pygame module
    # download module : https://pypi.python.org/pypi/SimpleGUICS2Pygame

# define global variables
A_string, B_string, C_string, D_string, count, score, over_all_score = 0, 0, 0, 0, 0, 0, 0
started = "No"
clear = "\n" * 100
# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
def format(t):
    global started, count, D_string, B_string, C_string, A_string
    t = count
    if started == "Yes":
        t = D_string
        D_string += 1
        if D_string >= 10: #.1 seconds
            if D_string == 10:
                C_string += 1
                if C_string == 10: # seconds
                    B_string += 1
                    if B_string >= 6:
                        A_string += 1
                        B_string = 0
                    C_string = 0
                D_string = 0
    return str(A_string) + ":" + str(B_string) + str(C_string) + "." + str(D_string) 
    
# define event handlers for buttons; "Start", "Stop", "Reset"
def start_button():
    global started#, count
    started = "No"
    #print "Started Button Pressed",started
    if started:
        timer.start()
        started = "Yes"
        print "Start Button Pressed"
        #format(count)
        #print ".10s Timer is Started"#,timer.is_running()
        
def score_board():
    global score, over_all_score
    if C_string > 0 and D_string == 0:
        score += 1
    #    print "Good"
    #else:
        #if score > 0:
        #    score -= 1
     #   print "You are Loosing"
    
def stop_button():
    global started, A_string, B_string, C_string, D_string, count, score, over_all_score
    if started == "Yes":
        timer.stop()
        started = "No"
        over_all_score += 1
        score_board()
        print "Stop Button Pressed"
        #print ".10s Timer is Stopped"#,timer.is_running()

def reset_button():
    global started, count, D_string, B_string, C_string , A_string, score, over_all_score
    A_string, B_string, C_string, D_string, count, score, over_all_score = 0, 0, 0, 0, 0, 0, 0
    timer.stop()
    print "Timer Restarted"#,timer.is_running()

def exit_button():
    frame.stop()
    timer.stop()
    print clear

# define event handler for timer with 0.1 sec interval
def timer_handler():
    global started, count
    #print "counter running",started
    if started == "Yes":
        count += 1
    format(count)
    
# define draw handler
def draw_handler(canvas):
    global score, over_all_score
    canvas.draw_text(str(A_string) + ":" + str(B_string) + 
                     str(C_string) + "." + str(D_string) , 
                     (100, 130), 50, 'White')
    canvas.draw_text("Score: " + str(score) + "/" + str(over_all_score), (210, 20), 20, 'Red')
    
# create frame
frame = simplegui.create_frame('StopWatch Game!', 300, 200)
timer = simplegui.create_timer(100, timer_handler)
# register event handlers
frame.set_draw_handler(draw_handler)
frame.add_button('Start', start_button,70)
frame.add_button('Stop', stop_button,70)
frame.add_button('Reset', reset_button, 70)
frame.add_button('Exit', exit_button, 50)
# start frame
frame.start()
timer.start()
print "Global Timer is running:",timer.is_running()
# Please remember to review the grading rubric

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s