Memory – Card game

I have been spending quite some time learning python for the past few months from text processing to game development using simplegui. And I must say for a person that always thought in C, I find Python completely straight forward and an easy but high level language.
As my journey to Python continues, This weeks project was a memory game.
Although it does have a couple of bugs in it, for instance keeping the high score and and and…

# 'Introduction to Interactive Programming in Python' Course
# RICE University - https://class.coursera.org/interactivepython-005
# by Joe Warren, John Greiner, Stephen Wong, Scott Rixner
# Mini-project for this week is the implementation of a card game - Memory.
#
# Concentration, also known as Memory, 
# is a card game in which all of the cards are laid face down on a surface 
# and two cards are flipped face up over each turn. 
# The object of the game is to turn over pairs of matching cards. 
# Concentration can be played with any number of players or as solitaire
# and is an especially good game for young children, 
# though adults may find it challenging and stimulating as well

__author__ = "Mpho Mphego"
__version__ = "$Revision: 1.14 $"
__date__ = "$Date: 2014/10/25 21:57 $"
__copyright__ = "Copyright (c) 2014 Mpho Mphego"
__url__ = "http://www.codeskulptor.org/#user38_QZuBkN6aVN_14.py"
__license__ = "Python"

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
import random


def init():
    """ Initializing Global variables"""
    global range_of_cards, exposed, exited, count, Turns, reveal_card
    global click_counter, game_won, restart_game, restart_count, counter#, Best_turn, Best_time
    counter = 0
    Turns = 0
    click_counter = 0
    count = 0
    restart_count = 0
    exited = False
    game_won = False
    restart_game = False
    

def new_game():
    global range_of_cards, exposed, exited, count, Turns, reveal_card
    global click_counter, game_won, restart_game, restart_count, counter, Best_turn, Best_time
    
    init()
    range_of_cards = [ i for i in range(8)] + [ i for i in range(8)]
    random.shuffle(range_of_cards)
    """Cheat"""
    #print range_of_cards
    exposed = [False for i in range(16)]
    reveal_card = list()
    timer.stop()
    """Game still needs some work on keeping the hi score and best time"""
    Best_turn = "0"
    Best_time = "0:00.0"
    
def mouseclick(pos):
    global click_counter, exposed, Turns, game_won
  
    if click_counter == 0:
        reveal_card.append(pos[0]//50)
        exposed[pos[0]//50] = True
        click_counter += 1
        Turns = 1
        timer.start()
        
    elif click_counter == 1:
        if not (pos[0]//50 in reveal_card):
            reveal_card.append(pos[0]//50)
            click_counter = 2
        
        exposed[pos[0]//50] = True
        
    else:
        if not (pos[0]//50 in reveal_card):
            if range_of_cards[reveal_card[-1]]!=range_of_cards[reveal_card[-2]]:
                exposed[reveal_card[-1]]=False
                exposed[reveal_card[-2]]=False
                reveal_card.pop()
                reveal_card.pop()
            click_counter = 1
            Turns += 1
            exposed[pos[0]//50] = True
            reveal_card.append(pos[0]//50)
      
    if exposed == [True for i in range(16)]:
        game_won = True
        #counter = 0
        timer.stop()
    else:
        counter = 0
        timer.start()

    return Turns 
    
def draw(canvas):
    global range_of_cards, exposed, exited, count, Turns
    global click_counter, game_won, restart_game, restart_count, newgame
    global counter, Best_turn, Best_time
    
    label.set_text("Turns = " + str(Turns))
    if exited == False:
        label2.set_text("Timer: " + format(counter))
#    if Best_turn > str(Turns):
#        label3.set_text("High Score = " + str(Best_turn))
#        label4.set_text("Best Time: " + str(Best_time))
                
    for i in range(16):        
        canvas.draw_polyline([[50*(i%50 + .5) , 80], [50*(i%50 + .5) , 170], [50*(i%50 + .5), 100]], 45, 'White')            
        canvas.draw_text("Time:" + format(counter), (550, 50), 50, 'Black')
        canvas.draw_text("No. of Turns: " + str(Turns), (10, 50), 50, 'Black')
        
        if exposed[i]:
            canvas.draw_polyline([[50*(i%50 + .5) , 80], [50*(i%50 + .5) , 170], [50*(i%50 + .5), 100]], 45, 'Green')            
            canvas.draw_text(str(range_of_cards[i]), (50*(i%50 + .2), 140), 54, 'White', 'monospace')    
            if exposed == [True for i in range(16)]:
                game_won = True
                
    #if counter > 4 :game_won = True;timer.stop() #debugging
    if game_won == True:
        for i in range(16):
            restart_count += 1
            canvas.draw_polyline([[50*i , 0], [50*(i+1) , 120], [50*i, 100]], 200, 'White')                       
            canvas.draw_text("Congradulations!!!", (185, 60), 35, 'Black', 'monospace')
            canvas.draw_text("You won the game", (210, 90), 35, 'Black', 'monospace')
            canvas.draw_text("In " + str(Turns) + " Turns.", (250, 120), 35, 'Black', 'monospace')
            canvas.draw_text("Time: " + format(counter), (225, 150), 35, 'Black', 'monospace')
            canvas.draw_text(str(__copyright__), (615, 175), 12, 'Red', 'sans-serif')
            game_won = False
            Best_time = format(counter)
            Best_turn = str(Turns)
            
            if restart_count > 10000:
                new_game()
                restart_count = 0                               
        
    if exited == True:
        #print timer.is_running()
        if timer.is_running() == False: timer.start()
            
        for i in range(16):
            canvas.draw_polyline([[50*i , 0], [50*(i+1) , 120], [50*i, 100]], 200, 'White')                       
            canvas.draw_text("Game created by " + str(__author__), (30, 55), 45, 'Black', 'monospace')
            canvas.draw_text("Thank you for playing", (100, 100), 45, 'Black', 'monospace')
            canvas.draw_text("Please wait...", (250, 150), 35, 'Red', 'monospace')
        
        #print "Please wait ", counter # debugging
        if counter > 30: 
            frame.stop()
            timer.stop()
    #print Best_turn
    #print Best_time
                               
def tick():
    global counter
    counter += 1
    #print counter
    
def format(value):
    A=value//600
    B=((value//10)%60)//10
    C=((value//10)%60)%10
    D=value%10
    #print str(A)+":" + str(B) +str(C)+ "."+str(D)
    return str(A)+":" + str(B) +str(C)+ "."+str(D)
   
def exit_game():
    global exited, count
    exited = True    
     
# create frame and add a button and labels
frame = simplegui.create_frame("Memory", 800, 180)
timer = simplegui.create_timer(100, tick)
frame.set_canvas_background('Green')
frame.add_button("Restart Game", new_game)
frame.add_button("Exit Game", exit_game)
label = frame.add_label("Turns = 0")
label2 = frame.add_label("Time: 0:00.0")
label3 = frame.add_label("High Score = 0")
label4 = frame.add_label("Best Time: 0:00.0")

# register event handlers
frame.set_mouseclick_handler(mouseclick)
frame.set_draw_handler(draw)
#frame.set_draw_handler(draw_handler)
# get things rolling
new_game()
frame.start()
#timer.start()

# Always remember to review the grading rubric
Advertisements