maroph

emojis_game1.py

Dec 13th, 2019
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.07 KB | None | 0 0
  1. from guizero import App, Box, Picture, PushButton
  2. import os
  3. from random import randint
  4. from random import shuffle
  5.  
  6.  
  7. # set the path to the emoji folder on your computer
  8. emojis_dir = "emojis"
  9.  
  10. # create a list of the locations of the emoji images
  11. emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  12.  
  13. # shuffle the emojis
  14. shuffle(emojis)
  15.  
  16. app = App("Emoji Match Game")
  17.  
  18. game_box = Box(app, border=True)
  19.  
  20. pictures_box = Box(game_box, layout="grid", border=True)
  21. buttons_box = Box(game_box, layout="grid", border=True)
  22.  
  23. # create a empty lists to which pictures and buttons will be added
  24. # we use the property visible to distinguish between set and not
  25. # set image properties
  26. # visible False: image not yet set
  27. pictures = []
  28. buttons = []
  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.         picture.visible = False
  34.         pictures.append(picture)
  35.         # put the buttons into the list
  36.         button = PushButton(buttons_box, grid=[x, y])
  37.         button.visible = False
  38.         buttons.append(button)
  39.  
  40.  
  41. # select the matching emoji
  42. matched_emoji = emojis.pop()
  43. # select a random picture index for this emoji
  44. idx_picture = randint(0,8)
  45. # select a random button index for this emoji
  46. idx_button = randint(0,8)
  47.  
  48. # set the chosen picture's image to the matched_emoji
  49. pictures[idx_picture].image = matched_emoji
  50. pictures[idx_picture].visible = True
  51. # set the chosen button's image to the matched_emoji
  52. buttons[idx_button].image = matched_emoji
  53. buttons[idx_button].visible = True
  54.  
  55.  
  56. # set all image properties to an emoji
  57. # if the related visible property is False
  58. for idx in range(0, len(pictures)):
  59.     if not pictures[idx].visible:
  60.         pictures[idx].image = emojis.pop()
  61.         pictures[idx].visible = True
  62.     if not buttons[idx].visible:
  63.         buttons[idx].image = emojis.pop()
  64.         buttons[idx].visible = True
  65.  
  66.  
  67. # now all image properties are set and we can display the GUI
  68. app.display()
Advertisement
Add Comment
Please, Sign In to add comment