Advertisement
brendan-stanford

emoji-match-final

Oct 21st, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 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. players_turn = Text(app, text = "Go P1", align = "top")
  14. result = Text(app, align = "top")
  15. time = Text(app, text = 10, align = "top")
  16.  
  17. #player1 score, round and streak counter
  18. score1 = Text(app, text = 0, align = "top")
  19. rounds1 = Text(app, text = 0, align = "bottom")
  20. streak1 = Text(app, text = 0, visible = False)
  21.  
  22. #player2 score, round and streak counter
  23. score2 = Text(app, text = 0, align = "top")
  24. rounds2 = Text(app, text = 0, align = "bottom")
  25. streak2 = Text(app, text = 0, visible = False)
  26.  
  27. #player_turn counter
  28. player_turn = 0
  29.  
  30. #display whether used choice matched
  31. def match_emoji(matched, turn):
  32. round1_time = 30
  33. round2_time = 30
  34. if matched:
  35. #if matched, say so and increase socre and streak by 1
  36. result.value = "YES!"
  37.  
  38. #update scores if player 1 turn
  39. if turn == 0:
  40. score1.value = int(score1.value) + 1
  41. streak1.value = int(streak1.value) + 1
  42. rounds1.value = int(rounds1.value) + 1
  43.  
  44. #if the user has streak of 3, award bonus point and 10s time
  45. if int(streak1.value) % 3 == 0:
  46. score1.value = int(score1.value) + 1
  47. round1_time += 10
  48.  
  49. #if player 2 turn, update scores
  50. if turn == 1:
  51. score2.value = int(score2.value) + 1
  52. streak2.value = int(streak2.value) + 1
  53. rounds2.value = int(rounds2.value) + 1
  54.  
  55. # if the user has a streak of 3, award bonus point and time
  56. if int(streak2.value) % 3 == 0:
  57. score2.value = int(score2.value) + 1
  58. round2_time += 10
  59.  
  60. #start next round
  61. global player_turn
  62. if turn == 0:
  63. player_turn += 1
  64. players_turn.value = "Go P2"
  65. setup_game(round2_time, player_turn)
  66. elif turn == 1:
  67. player_turn += 1
  68. players_turn.value = "Go P1"
  69. setup_game(round1_time, player_turn)
  70.  
  71. #if incorrect, say so and decrease score by 1; set streak to 0
  72. else:
  73. result.value = "NO!"
  74. if turn == 0:
  75. score1.value = int(score1.value) - 1
  76. streak1.value = 0
  77. elif turn == 1:
  78. score2.value = int(score2.value) - 1
  79. streak2.value = 0
  80.  
  81. # create a box to house the grid
  82. pictures_box = Box(pic_box, layout="grid")
  83. buttons_box = Box(button_box, layout="grid")
  84.  
  85. # create an empty list to which pictures will be added
  86. pictures = []
  87. buttons = []
  88.  
  89. #use a loop from 1-3 (y) within a loop of 1-3 (x) to create a 3x3 grid to display emojis
  90. for x in range(0,3):
  91. for y in range(0,3):
  92. # put the pictures into the list
  93. picture = Picture(pictures_box, grid=[x,y])
  94. pictures.append(picture)
  95.  
  96. #put picture buttons in grid
  97. button = PushButton(buttons_box, grid = [x,y])
  98. buttons.append(button)
  99.  
  100. #Function to setup new game
  101. def setup_game(length, player_turn):
  102. #set time to 30 seconds
  103. time.value = length
  104. #import emojis library and set the path to the emoji folder on your computer
  105. emojis_dir = "emojis"
  106. #create a list of the locations of the emoji images
  107. emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  108. #shuffle the emojis
  109. shuffle(emojis)
  110.  
  111. #clear result text
  112. result.value = " "
  113. #Isolate a single emoji from list to match in pictures and buttons lists
  114. matched_emoji = emojis.pop()
  115.  
  116. #choose a random position to insert the picture in the pictures list
  117. random_picture = randint(0,8)
  118.  
  119. #choose a random position to insert the picture in the buttons list
  120. random_button = randint(0,8)
  121.  
  122. # for each picture in the list
  123. for picture in pictures:
  124. #if the current picture is the randomly chosen match, insert the matched_emoji
  125. if picture == pictures[random_picture]:
  126. picture.image = matched_emoji
  127. else:
  128. # make the picture a random emoji
  129. picture.image = emojis.pop()
  130.  
  131. #for each button in list
  132. for button in buttons:
  133. #if current button is the randomly chosen match, insert the matched_emoji
  134. if button == buttons[random_button]:
  135. button.image = matched_emoji
  136. button.update_command(match_emoji, [True, (player_turn%2)])
  137. else:
  138. #insert a random emoji
  139. button.image = emojis.pop()
  140. button.update_command(match_emoji, [False, (player_turn%2)])
  141.  
  142. #end game function
  143. def quit_game():
  144. app.destroy()
  145. time.cancel(countdown)
  146. button.cancel(button_colour_switch)
  147.  
  148. #timer function
  149. def countdown():
  150. if int(time.value) > 0:
  151. time.value = int(time.value) - 1
  152. else:
  153. result.value = "END"
  154. warn(title = "END", text = "Score 1: " + score1.value + ", Score 2: " + score2.value)
  155. quit_game()
  156.  
  157. #display rules
  158. def game_rules():
  159. warn(title = "Rules", text = "Choose an Emoji Match before time runs out! Switch player after each correct choice!")
  160.  
  161. #change button background every second; change text colour to be visible
  162. def button_colour_switch():
  163. if end_game.bg == "black":
  164. end_game.bg = "white"
  165. end_game.text_color = "black"
  166. else:
  167. end_game.bg = "black"
  168. end_game.text_color = "white"
  169.  
  170. #repeat timer and colour-switching functions
  171. time.repeat(1000, countdown)
  172. button.repeat(1000,button_colour_switch)
  173.  
  174. #create a button to and and another to reset the game
  175. end_game = PushButton(app, command = quit_game, text = "Quit", align = "bottom")
  176.  
  177. #create a button to display rules
  178. rules_btn = PushButton(app, command = game_rules, text = "Rules", align = "bottom")
  179.  
  180. #setup first round of emojis and quit button to black
  181. setup_game(30, player_turn)
  182. button_colour_switch()
  183.  
  184. #start app
  185. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement