Advertisement
xavicano

Untitled

Aug 20th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | None | 0 0
  1. import os
  2. from random import shuffle, randint
  3. from guizero import App, Box, Picture, PushButton, Text
  4.  
  5.  
  6. def match_emoji(matched):
  7.     if matched:
  8.         result.value = "Correct!!!"
  9.         emojis=emoji_list()
  10.         new_try()
  11.     else:
  12.         result.value = "Incorrect"
  13.  
  14. # set the path to the emoji folder on your computer
  15. emojis_dir = "C:\\Users\\xavi.cano\\Code\\emojis\\emojis"
  16.  
  17. def emoji_list():
  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.     return emojis
  23.  
  24.  
  25. Xrange=3
  26. Yrange=3
  27. buttons = []
  28. pictures = []
  29.  
  30. def new_try():
  31.     ## for each picture in the list
  32.     for picture in pictures:
  33.         ## make the picture a random emoji
  34.         picture.image = emojis.pop()
  35.     for button in buttons:
  36.         # make the image feature of the PushButton a random emoji
  37.         button.image = emojis.pop()  
  38.         # set the command to be called and pass False, as these emoji wont be the matching ones
  39.         button.update_command(match_emoji, [False])
  40.     random_button = randint(0,8)
  41.     random_picture = randint(0,8)
  42.     buttons[random_button].image=pictures[random_picture].image
  43.     buttons[random_button].update_command(match_emoji, [True])
  44.  
  45. emojis=[]
  46.  
  47. app = App("emoji match")
  48.  
  49.  
  50. # create a box to house the grids
  51. game_box = Box(app)
  52. # create a box to house the pictures
  53. pictures_box = Box(game_box, layout="grid", align="left")
  54. # create a box to house the buttons
  55. buttons_box = Box(game_box, layout="grid", align="right")
  56.  
  57. result = Text(app)
  58. emojis=emoji_list()
  59.  
  60.  
  61.  
  62.  
  63. # create 9 PushButtons with a different grid coordinate and add to the list
  64. for x in range(0,Xrange):
  65.     for y in range(0,Yrange):
  66.         # put the pictures and buttons into the lists
  67.         picture = Picture(pictures_box, grid=[x,y])
  68.         pictures.append(picture)
  69.  
  70.         button = PushButton(buttons_box, grid=[x,y])
  71.         buttons.append(button)
  72.  
  73. new_try()
  74.  
  75.  
  76.    
  77.  
  78. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement