Advertisement
brendan-stanford

emoji-match-scores+rounds

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