Advertisement
brendan-stanford

emoji-matcher

Oct 18th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. import os
  2. from random import shuffle, choice, randint
  3. from guizero import App, Box, Picture, PushButton
  4.  
  5. # set the path to the emoji folder on your computer
  6. emojis_dir = "emojis"
  7. # create a list of the locations of the emoji images
  8. emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  9. # shuffle the emojis
  10. shuffle(emojis)
  11.  
  12. #create the app and put it in a box
  13. app = App("emoji match")
  14. game_box = Box(app)
  15.  
  16. # create a box to house the grid
  17. pictures_box = Box(game_box, layout="grid")
  18. buttons_box = Box(game_box, layout="grid")
  19.  
  20. # create an empty list to which pictures will be added
  21. pictures = []
  22. buttons = []
  23.  
  24. #use a loop from 1-3 (y) within a loop of 1-3 (x) to create a 3x3 grid
  25. for x in range(0,3):
  26. for y in range(0,3):
  27. # put the pictures into the list
  28. picture = Picture(pictures_box, grid=[x,y])
  29. pictures.append(picture)
  30.  
  31. #put picture buttons in grid
  32. button = PushButton(buttons_box, grid = [x,y])
  33. buttons.append(button)
  34.  
  35. #Isolate a single emoji from list to match in pictures and buttons lists
  36. matched_emoji = emojis.pop()
  37.  
  38. #choose a random position to insert the picture in the pictures list
  39. random_picture = randint(0,8)
  40.  
  41. #choose a random position to insert the picture in the buttons list
  42. random_button = randint(0,8)
  43.  
  44. # for each picture in the list
  45. for picture in pictures:
  46. #if the current picture is the randomly chosen match, insert the matched_emoji
  47. if picture == pictures[random_picture]:
  48. picture.image = matched_emoji
  49. else:
  50. # make the picture a random emoji
  51. picture.image = emojis.pop()
  52.  
  53. #for each button in list
  54. for button in buttons:
  55. #if current button is the randomly chosen match, insert the matched_emoji
  56. if button == buttons[random_button]:
  57. button.image = matched_emoji
  58. else:
  59. #insert a random emoji
  60. button.image = emojis.pop()
  61.  
  62. #start app
  63. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement