Advertisement
zhongnaomi

matching emoji game

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