brendan-stanford

emoji-match-timer

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