Advertisement
kalpin

Matching Pairs

Aug 8th, 2020
1,202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.16 KB | None | 0 0
  1. import os
  2. from random import shuffle, randint
  3. from guizero import App, Box, Picture, PushButton, Text, TextBox, info, yesno
  4.  
  5. # global variables
  6. GRID_SIZE   = 6 # must be even number
  7. score       = 0
  8. TimeLeft    = 30
  9. emojis_dir  = "emojis"
  10. emojis      = []
  11. pictures    = []
  12. buttons = []
  13. TIME_BETWEEN = 1000
  14.  
  15. # Event Handlers
  16.  
  17. def btnClicked(button,filename):
  18.     global buttons, score
  19.     btn1.clicks += 1
  20.     button.image = filename
  21.     buttons.append(button)
  22.     if btn1.clicks == 2:
  23.         if buttons[0].image == buttons[1].image: # match found
  24.             score += 1
  25.             lblScore.value = f"Score: {score}"
  26.             btn1.clicks = 0
  27.             buttons = []
  28.         else:
  29.             btn1.timeLeft = TIME_BETWEEN
  30.             btn1.repeat(100,myTimer)
  31.        
  32. def myTimer():
  33.     global buttons
  34.     btn1.timeLeft -= 100
  35.     if btn1.timeLeft <= 0:
  36.         btn1.cancel(myTimer)
  37.         for btn in buttons:
  38.             btn.image = 'blank.png'
  39.         buttons = []
  40.         btn1.clicks = 0
  41.  
  42. # Helper Functions
  43.  
  44. def setupRound():
  45.     global buttons, pictures, emojis, score
  46.    
  47.     # load emoji filenames
  48.     emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  49.     # shuffle the emojis
  50.     shuffle(emojis)
  51.  
  52.  
  53.     # generate correct numbers of emojiis for game
  54.     size = GRID_SIZE*GRID_SIZE
  55.     images = ['']*size
  56.     used = [False]*size
  57.     for i in range(size // 2):
  58.         image = emojis.pop()
  59.         for j in range(2):  # do this twice so each image is in the grid twice
  60.             pos = randint(0,size-1)
  61.             while used[pos] == True:
  62.                 pos = randint(0,size-1)
  63.             images[pos] = image
  64.             used[pos] = True
  65.     # for each picture button assign blank image and set up event handler to dislay correct image
  66.     for i in range(len(pictures)):
  67.         pictures[i].image = 'blank.png'
  68.         pictures[i].update_command(btnClicked, [pictures[i],images[i]])
  69.        
  70. # .......................................................................................  
  71.  
  72. # setup the app
  73. app = App("emoji match", width = GRID_SIZE*100, height = GRID_SIZE*80, bg = 'white')
  74. btn1 = PushButton(app,text='',visible = False)
  75. btn1.clicks = 0
  76. btn1.timeLeft = TIME_BETWEEN
  77.  
  78.  
  79. # create a box to house the grids
  80. gameBox = Box(app)
  81. space = Box(gameBox, width = "fill", height = 60, align='top')
  82. lblInstruct = Text(space, text = 'Find the matching pairs!',height = 3)
  83.  
  84. # create a box to house the pictures
  85. picturesBox = Box(gameBox, layout="grid",align='left')
  86.  
  87.  
  88. # create a box for score
  89. scoreBox = Box(app, width = 'fill', height = 50, align='bottom')
  90. lblScore = Text(scoreBox, text = '', width = 'fill', height = 'fill', color = 'blue')
  91. lblScore.bg = 'light yellow'
  92. lblScore.value = f"Score: {score}"
  93.  
  94. # create PushButtons with a different grid coordinate and add to the list
  95. for x in range(0,GRID_SIZE):
  96.     for y in range(0,GRID_SIZE):
  97.         # put the pictures and buttons into the lists
  98.         button = PushButton(picturesBox, grid=[x,y])
  99.         pictures.append(button)
  100.  
  101.  
  102. # call round setup
  103. setupRound()
  104.  
  105. # Display app
  106. app.display()
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement