Advertisement
Guest User

Emoji matching game - Troy Martin

a guest
Aug 21st, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1. import os
  2. from random import shuffle
  3. from guizero import App, Box, Picture, PushButton, Text
  4. from random import randint
  5.  
  6.  
  7. # populates list of emojis and shuffles them
  8. def emoji_list():
  9.     global emojis
  10.     emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  11.     shuffle(emojis)
  12.  
  13. # sets result value if correct answer or not and creates a new round
  14. def match_emoji(matched):
  15.     if matched:
  16.         result.text_color = "green"
  17.         result.value = "correct"
  18.     else:
  19.         result.text_color = "red"
  20.         result.value = "incorrect"
  21.     setup_round()
  22.  
  23. # sets a round up
  24. def setup_round():
  25.     # makes a list of emojis
  26.     emoji_list()
  27.  
  28.     # sets pictures for each grid
  29.     for picture in pictures:
  30.         picture.image = emojis.pop()
  31.  
  32.     for button in buttons:
  33.         button.image = emojis.pop()
  34.         button.update_command(match_emoji, [False])
  35.  
  36.     # choses a new emoji to be matched and sets to a random cell in each grid
  37.     matched_emoji = emojis.pop()
  38.  
  39.     random_picture = randint(0,8)
  40.     pictures[random_picture].image = matched_emoji
  41.  
  42.     random_button = randint(0,8)
  43.     buttons[random_button].image = matched_emoji
  44.  
  45.     # sets the correct button to report true (the matched cell)
  46.     buttons[random_button].update_command(match_emoji, [True])
  47.  
  48.  
  49. # sets up widgets for the grid and result for whether a correct match or not
  50. app = App("emoji match")
  51. result = Text(app)
  52. game_box = Box(app)
  53. pictures_box = Box(game_box, layout="grid")
  54. buttons_box = Box(game_box, layout="grid")
  55.  
  56. # setup for emoji direction and creates empty list to populate
  57. emojis_dir = "emojis"
  58. emojis = []
  59.  
  60. # creates two lists and two grids
  61. pictures = []
  62. buttons = []
  63.  
  64. for x in range(0,3):
  65.     for y in range(0,3):
  66.         picture = Picture(pictures_box, grid=[x,y])
  67.         pictures.append(picture)
  68.  
  69.         button = PushButton(buttons_box, grid=[x,y])
  70.         buttons.append(button)
  71.  
  72. # sets up a round
  73. setup_round()
  74.  
  75. # starts app
  76. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement