abbarnes

Memory Card Game [cokeskulptor]

Dec 12th, 2017
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.52 KB | None | 0 0
  1. # implementation of card game - Memory
  2. #http://www.codeskulptor.org/#user41_oSKP1kMsCCysSMt.py
  3.  
  4. import simplegui
  5. import random
  6.  
  7. card_width = 50
  8. click_count = 0
  9.  
  10. # helper function to initialize globals
  11. def new_game():
  12.     global cards, dcard, card1, card2, state, click_count
  13.     card1 = []
  14.     card2 = []
  15.     state = 0
  16.     click_count = 0
  17.     label.set_text("Turns = " + str(click_count))
  18.     cards = [[1,1,False], [2,2,False], [3,3,False], [4,4,False], [5,5,False], [6,6,False], [7,7,False], [8,8,False],
  19.              [9,1,False], [10,2,False], [11,3,False], [12,4,False], [13,5,False], [14,6,False], [15,7,False], [16,8,False]]
  20.     #card list includes 16 cards.  True = exposed, False  = Hidden.  
  21.     #i don't think the first index of each card is actually necessary, but I'm going to leave them in for now.
  22.    
  23.     random.shuffle(cards)
  24.    
  25. # define event handlers
  26. def mouseclick(pos):
  27.     # add game state logic here
  28.     global cards, state, card1, card2, click_count
  29.    
  30.     for card in cards:
  31.         if state == 0:
  32.         #only for initial click of the game.  
  33.             if pos[0] > cards.index(card)*card_width + 0 and pos[0] < cards.index(card)*card_width +card_width and card[2] == False:
  34.                 card[2] = True
  35.                 state = 1
  36.                 card1 = card
  37.                 click_count = click_count
  38.                 print click_count
  39.         elif state == 1:
  40.         #for when only one card is exposed.  
  41.             if pos[0] > cards.index(card)*card_width + 0 and pos[0] < cards.index(card)*card_width +card_width and card[2] == False:
  42.                 card[2] = True
  43.                 state = 2
  44.                 card2 = card
  45.                 click_count = click_count + 1
  46.            
  47.                 label.set_text("Turns = " + str(click_count))
  48.         elif state == 2:
  49.         #for when two cards are exposed
  50.             if pos[0] > cards.index(card)*card_width + 0 and pos[0] < cards.index(card)*card_width +card_width and card[2] == False:
  51.                
  52.                 if card1[1] == card2[1]:
  53.                 #clears card 1 and card 2, leaving them True.  And returns game to state 1
  54.                     state = 1
  55.                     card1 = []
  56.                     card2 = []
  57.                 else:
  58.                 #since cards don't match, they are turned back  over.  Retrurns game to state 1.
  59.                     card1[2] = False
  60.                     card2[2] = False
  61.                     state = 1
  62.                 card[2] = True
  63.                 card1 = card
  64.                 card2 = []
  65.  
  66.                
  67.                        
  68. # cards are logically 50x100 pixels in size    
  69. def draw(canvas):
  70.     for card in cards:
  71.         canvas.draw_text(str(card[1]), [cards.index(card)*card_width + card_width/2.5, 50], 20, 'White')
  72.  
  73.  
  74.         if not card[2]:
  75.             canvas.draw_polygon([[cards.index(card)*card_width , 0], [cards.index(card)*card_width + card_width, 0],
  76.             [cards.index(card)*card_width + card_width , 100], [cards.index(card)*card_width, 100]],
  77.                             1, "White", "Green")
  78.    
  79.    
  80.  
  81. # create frame and add a button and labels
  82. frame = simplegui.create_frame("Memory", 800, 100)
  83. frame.add_button("Reset", new_game)
  84. label = frame.add_label("Turns = " + str(click_count))
  85. label.set_text("Turns = " + str(click_count))
  86.  
  87. # register event handlers
  88. frame.set_mouseclick_handler(mouseclick)
  89. frame.set_draw_handler(draw)
  90.  
  91. # get things rolling
  92. new_game()
  93. frame.start()
  94. print cards
  95.  
  96.  
  97. # Always remember to review the grading rubric
Advertisement
Add Comment
Please, Sign In to add comment