Advertisement
Buzzbow

counter emojis

Aug 25th, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. import os
  2. from random import shuffle, randint
  3. from guizero import App, Box, Picture, PushButton, Text, warn
  4.  
  5. #set timer duration
  6. length = 20
  7. rounds = 0
  8.  
  9. # set the path to the emoji folder on your computer
  10. emojis_dir = "emojis"
  11. # create a list of the locations of the emoji images
  12. emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  13. # shuffle the emojis
  14. shuffle(emojis)
  15.  
  16. def match_emoji(matched):
  17. if matched:
  18. result.value = "correct"
  19. else:
  20. result.value = "incorrect"
  21.  
  22. setup_round()
  23.  
  24. def new_set():
  25. emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  26. # shuffle the emojis
  27. shuffle(emojis)
  28.  
  29.  
  30.  
  31. def setup_round():
  32. for picture in pictures:
  33. # make the picture a random emoji
  34. picture.image = emojis.pop()
  35.  
  36. for button in buttons:
  37. # make the button a random emoji
  38. button.image = emojis.pop()
  39. # set the command to be called and pass False, as these emoji wont be the matching ones
  40. button.update_command(match_emoji, [False])
  41.  
  42. # make 2 of the pictures the same, but in different grid
  43. matched_emoji = emojis.pop()
  44.  
  45. # select a number at random
  46. random_picture = randint(0,8)
  47. pictures[random_picture].image = matched_emoji
  48.  
  49. random_button = randint(0,8)
  50. # change the image feature of the PushButton with this index in the list of buttons to the new emoji
  51. buttons[random_button].image = matched_emoji
  52.  
  53. # set the command to be called and pass True, as this is the matching emoji
  54. buttons[random_button].update_command(match_emoji, [True])
  55. #increment rounds played
  56. global rounds
  57. rounds += 1
  58. rounds_played.value = ("you have played " + str(rounds) + " rounds")
  59.  
  60.  
  61.  
  62.  
  63. def counter():
  64. timer.value = int(timer.value) - 1
  65. if int(timer.value) == 0:
  66. timer.cancel(counter)
  67. # reset the timer
  68. result.value = "Game Over"
  69. warn("Time Out", "you've run out of time")
  70. # reset timer
  71. timer.value = str(length)
  72. # reset result
  73. result.value = ""
  74. # start new round
  75. setup_round()
  76. #restart timer
  77. timer.repeat(1000, counter)
  78. rounds = 0
  79.  
  80.  
  81.  
  82. # setup the app
  83. app = App("emoji match")
  84.  
  85. rounds_played = Text(app)
  86. result = Text(app)
  87.  
  88. # create a box to house the grids
  89. game_box = Box(app)
  90.  
  91. # create a box to house the pictures
  92. pictures_box = Box(game_box, layout="grid", align = "left")
  93. pad_box = Box(game_box,align = "left", width = 40, height = 40)
  94.  
  95. # create a box to house the buttons
  96. buttons_box = Box(game_box, layout="grid", align = "right")
  97. new_set_button = PushButton(app, text = "new set of emojis", align = "bottom", command = new_set)
  98.  
  99.  
  100. # create the an empty lists to add the buttons and pictures to
  101. buttons = []
  102. pictures = []
  103. # create 9 PushButtons with a different grid coordinate and add to the list
  104. for x in range(0,3):
  105. for y in range(0,3):
  106. # put the pictures and buttons into the lists
  107. picture = Picture(pictures_box, grid=[x,y])
  108. pictures.append(picture)
  109.  
  110. button = PushButton(buttons_box, grid=[x,y])
  111. buttons.append(button)
  112.  
  113. # add in the extra features
  114. extra_features = Box(app)
  115. timer = Text(extra_features, text="Get Ready")
  116.  
  117.  
  118. setup_round()
  119.  
  120. # start the timer
  121. timer.value = str(length)
  122. timer.repeat(1000,counter)
  123.  
  124.  
  125. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement