Advertisement
brendan-stanford

Emoji-match-basic

Oct 20th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. import os
  2. from random import shuffle, choice, randint
  3. from guizero import App, Box, Picture, PushButton, Text
  4.  
  5. #create the app and put it in a box and create seperate boxes for display and choice
  6. app = App()
  7. pic_box = Box(app, align = "left")
  8. button_box = Box(app, align = "right")
  9.  
  10. #create text to display result
  11. result = Text(app)
  12.  
  13. #display whether used choice matched
  14. def match_emoji(matched):
  15. if matched:
  16. result.value = "YES!"
  17. else:
  18. result.value = "NO!"
  19.  
  20. # create a box to house the grid
  21. pictures_box = Box(pic_box, layout="grid")
  22. buttons_box = Box(button_box, layout="grid")
  23.  
  24. # create an empty list to which pictures will be added
  25. pictures = []
  26. buttons = []
  27.  
  28. #use a loop from 1-3 (y) within a loop of 1-3 (x) to create a 3x3 grid
  29. for x in range(0,3):
  30. for y in range(0,3):
  31. # put the pictures into the list
  32. picture = Picture(pictures_box, grid=[x,y])
  33. pictures.append(picture)
  34.  
  35. #put picture buttons in grid
  36. button = PushButton(buttons_box, grid = [x,y])
  37. buttons.append(button)
  38.  
  39. #Function to setup new game
  40. def setup_game():
  41. #import emojis library and set the path to the emoji folder on your computer
  42. emojis_dir = "emojis"
  43. #create a list of the locations of the emoji images
  44. emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  45. #shuffle the emojis
  46. shuffle(emojis)
  47.  
  48. #clear result text
  49. result.value = " "
  50. #Isolate a single emoji from list to match in pictures and buttons lists
  51. matched_emoji = emojis.pop()
  52.  
  53. #choose a random position to insert the picture in the pictures list
  54. random_picture = randint(0,8)
  55.  
  56. #choose a random position to insert the picture in the buttons list
  57. random_button = randint(0,8)
  58.  
  59. # for each picture in the list
  60. for picture in pictures:
  61. #if the current picture is the randomly chosen match, insert the matched_emoji
  62. if picture == pictures[random_picture]:
  63. picture.image = matched_emoji
  64. else:
  65. # make the picture a random emoji
  66. picture.image = emojis.pop()
  67.  
  68. #for each button in list
  69. for button in buttons:
  70. #if current button is the randomly chosen match, insert the matched_emoji
  71. if button == buttons[random_button]:
  72. button.image = matched_emoji
  73. button.update_command(match_emoji, [True])
  74. else:
  75. #insert a random emoji
  76. button.image = emojis.pop()
  77. button.update_command(match_emoji, [False])
  78.  
  79. #end game function
  80. def quit_game():
  81. app.destroy()
  82.  
  83. #Create a button to and and another to reset the game
  84. end_game = PushButton(app, command = quit_game, text = "Quit", align = "bottom")
  85. reset_game = PushButton(app, command = setup_game, text = "Reset", align = "bottom")
  86.  
  87. #setup first round of emojis
  88. setup_game()
  89.  
  90. #start app
  91. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement