Advertisement
felaris

Untitled

Apr 3rd, 2020
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. import os
  2. from random import shuffle
  3. from guizero import App, Box, Picture, PushButton,Text,info
  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. dax = shuffle(emojis)
  11.  
  12. # setup the app
  13. app = App("emoji match")
  14. tools = Box(app,width='fill')
  15. # create a box to house the grids
  16. game_box = Box(app)
  17. # create a box to house the pictures
  18. pictures_box = Box(game_box, layout="grid")
  19. # create a box to house the buttons
  20. buttons_box = Box(game_box, layout="grid")
  21.  
  22. # create the an empty lists to add the buttons and pictures to
  23. buttons = []
  24. pictures = []
  25. # create 9 PushButtons with a different grid coordinate and add to the list
  26. for x in range(1,4):
  27.     for y in range(1,4):
  28.         # put the pictures and buttons into the lists
  29.         picture = Picture(pictures_box, grid=[x,y])
  30.         pictures.append(picture)
  31.  
  32.         button = PushButton(buttons_box, grid=[x,y])
  33.         buttons.append(button)
  34.  
  35. # for each picture and button in the list assign an emoji to its image feature
  36. for picture in pictures:
  37.     # make the picture a random emoji
  38.     picture.image = emojis.pop()
  39.  
  40. for button in buttons:
  41.       # make the image feature of the PushButton a random emoji
  42.       button.image = emojis.pop()
  43.      
  44.      
  45.      
  46.      
  47.      
  48.  
  49.  
  50. from random import randint
  51.  
  52. # choose a new emoji
  53. matched_emoji = emojis.pop()
  54.  
  55. # select a number at random
  56. random_picture = randint(0,8)
  57. # change the image feature of the Picture with this index in the list of pictures to the new emoji
  58. pictures[random_picture].image = matched_emoji
  59.  
  60. random_button = randint(0,8)
  61. # change the image feature of the PushButton with this index in the list of buttons to the new emoji
  62. buttons[random_button].image = matched_emoji
  63.  
  64.  
  65. def exit_app():
  66.     app.destroy()
  67. result = Text(app,size=23)
  68.  
  69.  
  70.  
  71. def match_emoji(matched):
  72.     if matched:
  73.         result.value = "correct"
  74.     else:
  75.         result.value = "incorrect"
  76.  
  77.  
  78.    
  79.  
  80.  
  81.  
  82. for button in buttons:
  83.     # make the button a random emoji
  84.     button.image = emojis.pop()
  85.     # set the command to be called and pass False, as these emoji wont be the matching ones
  86.     button.update_command(match_emoji, [False])
  87.    
  88. exit_button = PushButton(tools, text="Exit", command=exit_app,align='right')
  89. # set the command to be called and pass True, as this is the matching emoji
  90. buttons[random_button].update_command(match_emoji, [True])
  91.  
  92.  
  93.  
  94.  
  95. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement