Advertisement
SimonFung

myfinalProject03

Aug 25th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. import os
  2. from random import shuffle
  3. from guizero import App, Box, Picture, PushButton
  4.  
  5. # import the randint function
  6. from random import randint
  7.  
  8. # set the path to the emoji folder on your computer
  9. emojis_dir = "/home/pi/Public/image"
  10. # create a list of the locations of the emoji images
  11. emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  12. # shuffle the emojis
  13. shuffle(emojis)
  14.  
  15. # setup the app
  16. app = App("emoji match")
  17.  
  18. # create a box to house the grids
  19. game_box = Box(app)
  20. # create a box to house the pictures
  21. pictures_box = Box(game_box, layout="grid")
  22. # create a box to house the buttons
  23. buttons_box = Box(game_box, layout="grid")
  24.  
  25. # create the an empty lists to add the buttons and pictures to
  26. buttons = []
  27. pictures = []
  28. # create 9 PushButtons with a different grid coordinate and add to the list
  29. for x in range(0,3):
  30. for y in range(0,3):
  31. # put the pictures and buttons into the lists
  32. picture = Picture(pictures_box, grid=[x,y])
  33. pictures.append(picture)
  34.  
  35. button = PushButton(buttons_box, grid=[x,y])
  36. buttons.append(button)
  37.  
  38. # for each picture and button in the list assign an emoji to its image feature
  39. for picture in pictures:
  40. # make the picture a random emoji
  41. picture.image = emojis.pop()
  42.  
  43. for button in buttons:
  44. # make the image feature of the PushButton a random emoji
  45. button.image = emojis.pop()# choose a new emoji
  46.  
  47.  
  48. # select two numbers at random for both picture and botton
  49. random_picture = randint(0,8)
  50. random_button = randint (0,8)
  51.  
  52. # replace the image feature of the Picture by the of pictures to the randomly seclected emoji
  53. pictures[random_picture].image = buttons[random_button].image
  54.  
  55.  
  56.  
  57.  
  58.  
  59. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement