Advertisement
Buzzbow

score bonuses

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