<file_sep># "Stopwatch: The Game"

# Import modules
import simplegui

# define global variables
display_text = "0:00.0"
interval = 100
timer_count = 0
a = 0
b = 0
c = 0
success_stops = 0
failed_stops = 0
total_stops = 0
results = "0 / 0"

# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
def format(t):  
    return get_min(t) + ":" + get_sec(t) + "." + get_milli_sec(t)

# get milli seconds
def get_milli_sec(t):
    global c
    t = str(t)
    c = int(t[-1])
    return t[-1]

# get seconds
def get_sec(t):
    t = str(t)
    if len(t) == 2 and int(t) < 600:
        b =  t[0]
    elif len(t) == 3 and int(t) < 600:
        b = t[:2]
    elif int(t) > 600:
        b = str((int(t) % 600) // 10)
    else:
        b = "00"
    return pad_sec(b)

# get minutes
def get_min(t):
    if t >= 600:
        a = str(t // 600)
    else:
        a = "0"        
    return a

# pad out seconds to 2 digits
def pad_sec(t):
    if len(t)== 0:
        t = "00"
    elif len(t) == 1:
        t = "0" + t
    else:
        t = t
    return t
    
# define event handlers for buttons; "Start", "Stop", "Reset"
def button_start():
    timer.start()

def button_stop():
    if timer.is_running():
        timer.stop()        
        global timer_count, success_stops, failed_stops, total_stops, results
        #print timer_count, get_milli_sec(timer_count)
        total_stops += 1        
        if int(get_milli_sec(timer_count)) == 0:
            success_stops += 1
        else:
            failed_stops += 1     
        results = str(success_stops) + " / " + str(total_stops)

def button_reset():
    timer.stop()
    global timer_count, a, b, c, success_stops, failed_stops, results, total_stops
    timer_count = 0
    a = 0
    b = 0
    c = 0
    failed_stops = 0 
    success_stops = 0
    total_stops = 0
    results = "0 / 0"
    #print "\nNew Game\n"

# define event handler for timer with 0.1 sec interval
def tick():
    global timer_count
    timer_count += 1

# define draw handler
def draw(canvas):
    canvas.draw_text(format(timer_count), [60, 110], 36, "White")
    canvas.draw_text(results, [140, 25], 24, "Green")
    
# create frame
frame = simplegui.create_frame("Stopwatch: The Game", 200, 200)

# register event handlers
frame.set_draw_handler(draw)
timer = simplegui.create_timer(interval, tick)
frame.add_button("Start", button_start, 100)
frame.add_button("Stop", button_stop, 100)
frame.add_button("Reset", button_reset, 100)

# start frame
frame.start()
<file_sep># "Guess the number" mini-project
import simplegui
import random
import math

# initialize global variables used in your code
num_range = 100
random_number = 0
guesses_left = 0

# define event handlers for control panel
def init():
    range100()
    f.start()
    
def range100():
    # button that changes range to range [0,100) and restarts
    global random_number, guesses_left, num_range
    num_range = 100
    random_number, guesses_left = random.randrange(0, 100), 7
    print "New game. Range is from 0 to 100"
    print "Number of guesses remaining: ", guesses_left, "\n"
    
def range1000():
    # button that changes range to range [0,1000) and restarts
    global random_number, guesses_left, num_range
    num_range = 1000
    random_number, guesses_left = random.randrange(0, 1000), 10
    print "New game. Range is from 0 to 1000"
    print "Number of guesses remaining: ", guesses_left, "\n"
    
def within_range(guess):
    # make sure user is entering a number that falls within the
    # specified range
    global num_range
    if int(guess) >= 0 and int(guess) < num_range:
        return True
    else:
        return False
    
def get_input(guess):
    # main game logic goes here	
    global guesses_left, random_number
    guesses_left -= 1
    print "Guess entered: ", guess
    print "Number of remaining guesses is ", guesses_left
    
    if int(guess) > random_number and within_range(guess) and guesses_left != 0:
        print "Lower!\n"
    elif int(guess) < random_number and within_range(guess) and guesses_left != 0:
        print "Higher!\n"
    elif int(guess) == random_number:
        print "You got it right!\n"
        range100()
    elif guesses_left == 0 and int(guess) != random_number:
        print "You ran out of guesses, the correct number is ", random_number, "\n"
        range100()
    else:
        print "Guess does not fall within range, Try Again.\n"
    
    
    
# create frame
f = simplegui.create_frame("Guess the number", 200, 200)

# register event handlers for control elements
f.add_button("Range is [0,100)", range100, 200)
f.add_button("Range is [0,1000)", range1000, 200)
f.add_input("Enter a guess", get_input, 200)

# start frame
init()<file_sep># implementation of card game - Memory
import simplegui
import random

list_cards = []
list_status = []
cards_clicked = []
num_moves = 0
# helper function to initialize globals
def init():
    global list_cards, list_status, num_moves
    temp_list1 = range(0,8)
    temp_list2 = range(0,8)
    list_cards = temp_list1 + temp_list2
    random.shuffle(list_cards)
    list_status = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    del cards_clicked[:]
    num_moves = 0
     
# define event handlers
def mouseclick(pos):
    global list_cards, list_status, cards_clicked, num_moves
    # add game state logic here
    if num_moves == 0:
        num_moves += 1
        
    if list_status[pos[0] // 50] == 0:
        list_status[pos[0] // 50] = 1
        cards_clicked.append(pos[0] // 50)
        
    if len(cards_clicked) == 3:
        num_moves += 1
        if list_cards[cards_clicked[0]] == list_cards[cards_clicked[1]]:
            list_status[cards_clicked[0]] = 2
            list_status[cards_clicked[1]] = 2
            del cards_clicked[:2]
        else:
            list_status[cards_clicked[0]] = 0
            list_status[cards_clicked[1]] = 0
            del cards_clicked[:2]
                        
# cards are logically 50x100 pixels in size    
def draw(canvas):
    global list_cards, list_status, num_moves
    position = 15
    cover = 0
    check = 0
    box = 0
    for x in list_cards:
        if list_status[check] != 0:
            canvas.draw_text(str(x), (position, 65), 48, "Red")
        else:
            canvas.draw_polygon([[box, 0], [box + 50, 0], [box + 50, 100], [box,100]], 1, "white", "green")
        position += 50
        check += 1
        box += 50
        label.set_text("Moves = " + str(num_moves))

# create frame and add a button and labels
frame = simplegui.create_frame("Memory", 800, 100)
frame.add_button("Restart", init)
label = frame.add_label("Moves = 0")

# initialize global variables
init()

# register event handlers
frame.set_mouseclick_handler(mouseclick)
frame.set_draw_handler(draw)

# get things rolling
frame.start()<file_sep>This repository contains Projects from Coursera: An Introduction to Interactive Programming in Python