Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. # My Emoji Match V3
  2. import os
  3. from random import shuffle, randint
  4. from guizero import App, Box, Picture, Text, PushButton, warn
  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. app = App("My Emoji Match V3", width=700, height=500)
  13.  
  14. nos = 0
  15. rounds = Text(app)
  16. result = Text(app)
  17. def match_emoji(matched):
  18. if matched:
  19. result.value = "correct"
  20. else:
  21. result.value = "incorrect"
  22. setup_round()
  23.  
  24. def setup_round():
  25. # for each picture in the list (872 pics)
  26. for picture in pictures:
  27. # make the picture a random emoji
  28. picture.image = emojis.pop()
  29.  
  30. for button in buttons:
  31. # make the button a random emoji
  32. button.image = emojis.pop()
  33. # set the command to be called and pass False, as these emoji wont be the matching ones
  34. button.update_command(match_emoji, [False])
  35.  
  36. # make 2 of the pictures the same, but in different grid
  37. matched_emoji = emojis.pop()
  38.  
  39. random_picture = randint(0,8)
  40. pictures[random_picture].image = matched_emoji
  41.  
  42. random_button = randint(0,8)
  43. buttons[random_button].image = matched_emoji
  44.  
  45. # say = Picture(game_box, image=matched_emoji)
  46.  
  47. # set the command to be called and pass True, as this is the matching emoji
  48. buttons[random_button].update_command(match_emoji, [True])
  49.  
  50.  
  51. # def counter():
  52. # timer.value = int(timer.value) - 1
  53. # if int(timer.value) == 0:
  54. # # reset the timer
  55. # timer.value = 30
  56.  
  57. def counter():
  58. timer.value = int(timer.value) - 1
  59. if int(timer.value) == 0:
  60. global nos
  61. nos += 1
  62. rounds.value = "Round "+str(nos)
  63. timer.cancel(counter)
  64. # reset the timer
  65. result.value = "Game Over"
  66. warn("Time Out", "you've run out of time")
  67. # reset timer
  68. timer.value = "20"
  69. # reset result
  70. result.value = ""
  71. # start new round
  72. setup_round()
  73. #restart timer
  74. timer.repeat(1000, counter)
  75.  
  76.  
  77. # create a box to house the grids
  78. game_box = Box(app)
  79. # create box to house pictures
  80. pictures_box = Box(game_box, layout="grid", border=4, align="left")
  81. nl = Text(game_box, text=" ", align="left")
  82. # create a box to house the buttons
  83. buttons_box = Box(game_box, layout="grid", border=2, align="left")
  84.  
  85.  
  86. # create empty lists to which pictures will be added
  87. buttons = []
  88. pictures = []
  89.  
  90. # populate pictures
  91. for x in range(0,3):
  92. for y in range(0,3):
  93. # put the pictures into the lists
  94. picture = Picture(pictures_box, grid=[x,y])
  95. pictures.append(picture)
  96.  
  97. button = PushButton(buttons_box, grid=[x,y])
  98. buttons.append(button)
  99.  
  100. # add in the extra features
  101. extra_features = Box(app)
  102. timer = Text(extra_features, text="Get Ready")
  103.  
  104. setup_round()
  105. # start the timer
  106. timer.value = 30
  107. timer.repeat(1000, counter)
  108.  
  109. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement