Advertisement
Buzzbow

emogi parallel and new list

Aug 25th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 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. def new_set():
  21. emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  22. # shuffle the emojis
  23. shuffle(emojis)
  24.  
  25.  
  26. def setup_round():
  27. for picture in pictures:
  28. # make the picture a random emoji
  29. picture.image = emojis.pop()
  30.  
  31. for button in buttons:
  32. # make the button a random emoji
  33. button.image = emojis.pop()
  34. # set the command to be called and pass False, as these emoji wont be the matching ones
  35. button.update_command(match_emoji, [False])
  36.  
  37. # make 2 of the pictures the same, but in different grid
  38. matched_emoji = emojis.pop()
  39.  
  40. # select a number at random
  41. random_picture = randint(0,8)
  42. pictures[random_picture].image = matched_emoji
  43.  
  44. random_button = randint(0,8)
  45. # change the image feature of the PushButton with this index in the list of buttons to the new emoji
  46. buttons[random_button].image = matched_emoji
  47.  
  48. # set the command to be called and pass True, as this is the matching emoji
  49. buttons[random_button].update_command(match_emoji, [True])
  50.  
  51. # setup the app
  52. app = App("emoji match")
  53.  
  54. result = Text(app)
  55.  
  56. # create a box to house the grids
  57. game_box = Box(app)
  58. # create a box to house the pictures
  59. pictures_box = Box(game_box, layout="grid", align = "left")
  60. pad_box = Box(game_box,align = "left", width = 40, height = 40)
  61. # create a box to house the buttons
  62. buttons_box = Box(game_box, layout="grid", align = "right")
  63. new_set_button = PushButton(app, text = "new set of emojis", align = "bottom", command = new_set)
  64.  
  65.  
  66. # create the an empty lists to add the buttons and pictures to
  67. buttons = []
  68. pictures = []
  69. # create 9 PushButtons with a different grid coordinate and add to the list
  70. for x in range(0,3):
  71. for y in range(0,3):
  72. # put the pictures and buttons into the lists
  73. picture = Picture(pictures_box, grid=[x,y])
  74. pictures.append(picture)
  75.  
  76. button = PushButton(buttons_box, grid=[x,y])
  77. buttons.append(button)
  78.  
  79.  
  80.  
  81.  
  82. setup_round()
  83. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement