Advertisement
brendan-stanford

Emoji-match-bonus

Oct 20th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. #import neccessary libraries
  2. import os
  3. from random import shuffle, choice, randint
  4. from guizero import App, Box, Picture, PushButton, Text, warn
  5. from time import sleep
  6.  
  7. #create the app and put it in a box and create seperate boxes for display and choice
  8. app = App()
  9. pic_box = Box(app, align = "left")
  10. button_box = Box(app, align = "right")
  11.  
  12. #create text to display result and a timer
  13. result = Text(app, align = "top")
  14. time = Text(app, text = 10, align = "top")
  15. score = Text(app, text = 0, align = "top")
  16. rounds = Text(app, text = 0, align = "bottom")
  17. streak = Text(app, text = 0, visible = False)
  18.  
  19. #display whether used choice matched
  20. def match_emoji(matched):
  21. round_time = 30
  22. if matched:
  23. #if matched, say so and increase socre and streak by 1
  24. result.value = "YES!"
  25. score.value = int(score.value) + 1
  26. streak.value = int(streak.value) + 1
  27. #if the user has streak of 3, award bonus point and 10s time
  28. if int(streak.value) % 3 == 0:
  29. score.value = int(streak.value) + 1
  30. round_time += 10
  31. #start next round
  32. setup_game(round_time)
  33. #if incorrect, say so and decrease score by 1; set streak to 0
  34. else:
  35. result.value = "NO!"
  36. score.value = int(score.value) - 1
  37. streak.value = 0
  38.  
  39. # create a box to house the grid
  40. pictures_box = Box(pic_box, layout="grid")
  41. buttons_box = Box(button_box, layout="grid")
  42.  
  43. # create an empty list to which pictures will be added
  44. pictures = []
  45. buttons = []
  46.  
  47. #use a loop from 1-3 (y) within a loop of 1-3 (x) to create a 3x3 grid
  48. for x in range(0,3):
  49. for y in range(0,3):
  50. # put the pictures into the list
  51. picture = Picture(pictures_box, grid=[x,y])
  52. pictures.append(picture)
  53.  
  54. #put picture buttons in grid
  55. button = PushButton(buttons_box, grid = [x,y])
  56. buttons.append(button)
  57.  
  58. #Function to setup new game
  59. def setup_game(length):
  60. #set time to 30 seconds
  61. time.value = length
  62. #increase round count by 1
  63. rounds.value = int(rounds.value) + 1
  64. #import emojis library and set the path to the emoji folder on your computer
  65. emojis_dir = "emojis"
  66. #create a list of the locations of the emoji images
  67. emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  68. #shuffle the emojis
  69. shuffle(emojis)
  70.  
  71. #clear result text
  72. result.value = " "
  73. #Isolate a single emoji from list to match in pictures and buttons lists
  74. matched_emoji = emojis.pop()
  75.  
  76. #choose a random position to insert the picture in the pictures list
  77. random_picture = randint(0,8)
  78.  
  79. #choose a random position to insert the picture in the buttons list
  80. random_button = randint(0,8)
  81.  
  82. # for each picture in the list
  83. for picture in pictures:
  84. #if the current picture is the randomly chosen match, insert the matched_emoji
  85. if picture == pictures[random_picture]:
  86. picture.image = matched_emoji
  87. else:
  88. # make the picture a random emoji
  89. picture.image = emojis.pop()
  90.  
  91. #for each button in list
  92. for button in buttons:
  93. #if current button is the randomly chosen match, insert the matched_emoji
  94. if button == buttons[random_button]:
  95. button.image = matched_emoji
  96. button.update_command(match_emoji, [True])
  97. else:
  98. #insert a random emoji
  99. button.image = emojis.pop()
  100. button.update_command(match_emoji, [False])
  101.  
  102. #end game function
  103. def quit_game():
  104. app.destroy()
  105. time.cancel(countdown)
  106. button.cancel(button_colour_switch)
  107.  
  108. #timer function
  109. def countdown():
  110. if int(time.value) > 0:
  111. time.value = int(time.value) - 1
  112. else:
  113. result.value = "END"
  114. warn(title = "END", text = "Final Score: " + score.value)
  115. quit_game()
  116.  
  117. #change button background every second; change text colour to be visible
  118. def button_colour_switch():
  119. if end_game.bg == "black":
  120. end_game.bg = "white"
  121. end_game.text_color = "black"
  122. else:
  123. end_game.bg = "black"
  124. end_game.text_color = "white"
  125.  
  126. #repeat timer and colour-switching functions
  127. time.repeat(1000, countdown)
  128. button.repeat(1000,button_colour_switch)
  129.  
  130. #Create a button to and and another to reset the game
  131. end_game = PushButton(app, command = quit_game, text = "Quit", align = "bottom")
  132. #reset_game = PushButton(app, command = setup_game, text = "Next", align = "bottom")
  133.  
  134. #setup first round of emojis and quit button to black
  135. setup_game(30)
  136. button_colour_switch()
  137.  
  138. #start app
  139. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement