Advertisement
JAWarr

Emoji game with renew list

May 6th, 2022
750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. # Emoji Match game challenges - renew the list
  2. import os, random
  3. from random import shuffle, randint
  4. from guizero import App, Box, Picture, PushButton, Text
  5.  
  6.  
  7. def match_emoji(matched):
  8.     if matched:
  9.         result.value = "correct"
  10.     else:
  11.         result.value = "incorrect"
  12.        
  13.     setup_round()
  14.  
  15. def setup_round():
  16.     # set the path to the emoji folder on your computer
  17.     emojis_dir = "emojis"
  18.     # create a list of the locations of the emoji images
  19.     emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  20.     # shuffle the emojis
  21.     shuffle(emojis)
  22.     # for each picture and button in the list assign an emoji to its image feature
  23.     for picture in pictures:
  24.         # make the picture a random emoji
  25.         picture.image = emojis.pop()
  26.  
  27.     for button in buttons:
  28.         # make the image feature of the PushButton a random emoji
  29.         button.image = emojis.pop()
  30.         # set the command to be called and pass False, as these emoji wont be the matching ones
  31.         button.update_command(match_emoji, [False])
  32.  
  33.     # choose a new emoji
  34.     matched_emoji = emojis.pop()
  35.  
  36.     # select a number at random
  37.     random_picture = randint(0,8)
  38.     # change the image feature of the Picture with this index in the list of pictures to the new emoji
  39.     pictures[random_picture].image = matched_emoji
  40.  
  41.     random_button = randint(0,8)
  42.     # change the image feature of the PushButton with this index in the list of buttons to the new emoji
  43.     buttons[random_button].image = matched_emoji
  44.     # set the command to be called and pass True, as this is the matching emoji
  45.     buttons[random_button].update_command(match_emoji, [True])
  46.  
  47. # setup the app
  48. app = App("emoji match")
  49.  
  50. result = Text(app)
  51.  
  52. # create a box to house the grids
  53. game_box = Box(app)
  54. # create a box to house the pictures
  55. pictures_box = Box(game_box, layout="grid", align="left")
  56. # create a box to house the buttons
  57. buttons_box = Box(game_box, layout="grid", align ="right")
  58.  
  59. # create the an empty lists to add the buttons and pictures to
  60. buttons = []
  61. pictures = []
  62. # create 9 PushButtons with a different grid coordinate and add to the list
  63. for x in range(0,3):
  64.     for y in range(0,3):
  65.         # put the pictures and buttons into the lists
  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. setup_round()
  73.  
  74. app.display()
  75.  
  76.  
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement