SHARE
TWEET

Untitled

a guest Apr 24th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # implementation of card game - Memory
  2.  
  3. #after a pair, got an card flipped --check error
  4.  
  5. import simplegui
  6. import random
  7.  
  8. list1=[]
  9. list2=[]
  10. exposed=[]
  11. turned_card1=-1
  12. turned_card2=-1
  13. counter=0
  14.  
  15. def exposed_generator():
  16.     exposed=[]
  17.     for i in range(0,16):
  18.         exposed.append(False)
  19.     return exposed
  20.    
  21. def list_generator():
  22.     for i in range(0,8):
  23.         list1.append(random.randint(0, 8))
  24.         list2.append(random.randint(0, 8))
  25.     return list1+list2
  26.  
  27. # helper function to initialize globals
  28. def new_game():
  29.     global full_list,state,exposed,counter, turned_card1,turned_card2
  30.     full_list=list_generator()
  31.     exposed=exposed_generator()
  32.     state=0
  33.     turned_card1=-1
  34.     turned_card2=-1
  35.     counter=0
  36.     label.set_text('Turns: 0')
  37.    
  38. # define event handlers
  39. def mouseclick(pos):
  40.     # add game state logic here
  41.     global state, exposed,click_pos,turned_card1,turned_card2,counter
  42.     click_pos=pos[0]//50
  43.    
  44.     if state==0:
  45.         if exposed[click_pos]==False:
  46.             state=1
  47.             exposed[click_pos]=True
  48.             turned_card1=click_pos
  49.             counter+=1
  50.     elif state==1:
  51.         if exposed[click_pos]==False:
  52.             state=2
  53.        
  54.             exposed[click_pos]=True
  55.             turned_card2=click_pos
  56.             counter+=1
  57.     else:
  58.         if exposed[click_pos]==False:
  59.            
  60.             if full_list[turned_card1]==full_list[turned_card2]:
  61.                 #if two cards match, display numbers
  62.                
  63.                 exposed[click_pos]=True
  64.                 turned_card1=click_pos
  65.                 turned_card2=-1
  66.                
  67.             else:                            
  68.                 exposed[turned_card1]=False
  69.                 exposed[turned_card2]=False
  70.                 exposed[click_pos]=True
  71.                 turned_card1=click_pos
  72.                 counter+=1
  73.             state=1
  74.     label.set_text('Turns: '+str(counter))
  75.    
  76. # cards are logically 50x100 pixels in size    
  77. def draw(canvas):
  78.     global full_list,pos1,exposed
  79.     horizontal_position=25
  80.    
  81.        
  82.     for i in range(16):
  83.         if exposed[i]:
  84.             canvas.draw_text(str(full_list[i]), [horizontal_position+i*50,50], 16, 'Red')
  85.         else:
  86.             canvas.draw_polygon([[i*50, 0], [(i+1)*50, 0], [(i+1)*50, 100],[i*50, 100]], 1, "Black", "Green")
  87.  
  88. # create frame and add a button and labels
  89. frame = simplegui.create_frame("Memory", 800, 100)
  90. frame.add_button("Reset", new_game)
  91. label = frame.add_label("Turns: 0")
  92.  
  93. # register event handlers
  94. frame.set_mouseclick_handler(mouseclick)
  95. frame.set_draw_handler(draw)
  96.  
  97. # get things rolling
  98. new_game()
  99. frame.start()
  100.  
  101. # Always remember to review the grading rubric
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top