Facebook
From Walloping Echidna, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 131
  1. import os
  2. from random import shuffle, randint
  3. from guizero import App, Box, Picture, PushButton, Text, warn, info, Window, PushButton
  4.  
  5. # set the path to the emoji folder on your computer
  6. emojis_dir = "emojis"
  7. # create a list of the locations of the emoji images
  8. emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  9. # shuffle the emojis
  10. shuffle(emojis)
  11.  
  12.  
  13.  
  14. def setup_round():
  15.     # for each picture and button in the list assign an emoji to its image feature
  16.     for picture in pictures:
  17.         # make the picture a random emoji
  18.         picture.image = emojis.pop()
  19.  
  20.     for button in buttons:
  21.         # make the image feature of the PushButton a random emoji
  22.         button.image = emojis.pop()
  23.         # set the command to be called and pass False, as these emoji wont be the matching ones
  24.         button.update_command(match_emoji, [False])
  25.  
  26.     # choose a new emoji
  27.     matched_emoji = emojis.pop()
  28.    
  29.  
  30.     # select a number at random
  31.     random_picture = randint(0,8)
  32.     # change the image feature of the Picture with this index in the list of pictures to the new emoji
  33.     pictures[random_picture].image = matched_emoji
  34.  
  35.     random_button = randint(0,8)
  36.     # change the image feature of the PushButton with this index in the list of buttons to the new emoji
  37.     buttons[random_button].image = matched_emoji
  38.     # set the command to be called and pass True, as this is the matching emoji
  39.     buttons[random_button].update_command(match_emoji, [True])
  40.  
  41. def setup_round_2():
  42.     # for each picture and button in the list assign an emoji to its image feature
  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 image feature of the PushButton 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_2, [False])
  52.  
  53.     # choose a new emoji
  54.     matched_emoji = emojis.pop()
  55.    
  56.  
  57.     # select a number at random
  58.     random_picture = randint(0,8)
  59.     # change the image feature of the Picture with this index in the list of pictures to the new emoji
  60.     pictures[random_picture].image = matched_emoji
  61.  
  62.     random_button = randint(0,8)
  63.     # change the image feature of the PushButton with this index in the list of buttons to the new emoji
  64.     buttons[random_button].image = matched_emoji
  65.     # set the command to be called and pass True, as this is the matching emoji
  66.     buttons[random_button].update_command(match_emoji_2, [True])
  67.  
  68.  
  69.  
  70. def match_emoji(matched):
  71.     if matched:
  72.         result.value = "correct"
  73.         score_p1.value = int(score_p1.value) + 1
  74.        
  75.         if int(score_p1.value) == 3:
  76.                 app.info("bonus", "BONUS")
  77.                 #timer.cancel(counter)
  78.                 timer.value = int(timer.value) + 10
  79.                
  80.     else:
  81.         result.value = "incorrect"
  82.         score_p1.value = int(score_p1.value) - 1
  83.        
  84.     setup_round()
  85.    
  86.  
  87. def match_emoji_2(matched):
  88.     if matched:
  89.         result.value = "correct"
  90.         score_p2.value = int(score_p2.value) + 1
  91.        
  92.         if int(score_p2.value) == 3:
  93.                 app.info("bonus", "BONUS")
  94.                 #timer.cancel(counter_2)
  95.                 timer.value = int(timer.value) + 10
  96.                
  97.     else:
  98.         result.value = "incorrect"
  99.         score_p2.value = int(score_p2.value) - 1
  100.        
  101.     setup_round_2()
  102.  
  103.        
  104. def counter():
  105.    
  106.    
  107.         timer.value = int(timer.value) - 1
  108.         if int(timer.value) == 0:
  109.                 timer.cancel(counter)
  110.                 # reset the timer
  111.                 result.value = "Game Over"
  112.                 warn("Time Out", "you've run out of time" + " \n Player 2 score was " + score_p1.value)
  113.                 rounds_p2.value = int(rounds_p2.value) + 1
  114.                 # reset timer
  115.                 timer.value = "20"
  116.                 # reset result
  117.                 #result.value = ""
  118.                 # start new round
  119.                 setup_round_2()
  120.                 #restart timer
  121.                 timer.repeat(1000, counter_2)
  122.                 #reset score
  123.                 #score_p1.value = "0"
  124.  
  125.                 if score_p2.value > score_p1.value:
  126.                     label_leader.value = "P1"
  127.                 else:
  128.                     label_leader.value = "P2"
  129.    
  130.  
  131. def counter_2():
  132.    
  133.         timer.value = int(timer.value) - 1
  134.         if int(timer.value) == 0:
  135.                 timer.cancel(counter_2)
  136.                 # reset the timer
  137.                 result.value = "Game Over"
  138.                 warn("Time Out", "you've run out of time" + " \n Player 1 score was " + score_p2.value)
  139.                 rounds_p1.value = int(rounds_p1.value) + 1
  140.                 # reset timer
  141.                 timer.value = "20"
  142.                 # reset result
  143.                 #result.value = ""
  144.                 # start new round
  145.                 setup_round()
  146.                 #restart timer
  147.                 timer.repeat(1000, counter)
  148.                 #reset score
  149.                 #score_p1.value = "0"
  150.                 if score_p2.value > score_p1.value:
  151.                     label_leader.value = "P1"
  152.                 else:
  153.                     label_leader.value = "P2"
  154.                
  155.  
  156.  
  157.  
  158.  
  159.        
  160. # setup the app
  161. app = App("emoji match")
  162.  
  163. # create a box to house the grids
  164. game_box = Box(app)
  165. # create a box to house the pictures
  166. pictures_box = Box(game_box, layout="grid", align="right")
  167. # create a box to house the buttons
  168. buttons_box = Box(game_box, layout="grid", align="right")
  169.  
  170. # create the an empty lists to add the buttons and pictures to
  171. buttons = []
  172. pictures = []
  173. # create 9 PushButtons with a different grid coordinate and add to the list
  174. for x in range(0,3):
  175.     for y in range(0,3):
  176.         # put the pictures and buttons into the lists
  177.         picture = Picture(pictures_box, grid=[x,y])
  178.         pictures.append(picture)
  179.        
  180.         button = PushButton(buttons_box, grid=[x,y])
  181.         buttons.append(button)
  182.  
  183. result = Text(app, size = 30)
  184. extra_features = Box(app)
  185. timer = Text(extra_features, text="Get Ready")
  186.  
  187.  
  188. scoreboard = Box(app, border =3)
  189. score_p2 = Text(scoreboard, text="0", align="right")
  190. label_p2 = Text(scoreboard, text="Score player 1", align="right")
  191. rounds_p2 = Text(scoreboard, align="right")
  192. label_round_p2 = Text(scoreboard, text="Rounds P1", align="right")
  193.  
  194. label_round = Box(app, border =3)
  195. label_round_p1 = Text(label_round, text="Rounds P2", align="left")
  196. rounds_p1 = Text(label_round, align="left")
  197. label_p1 = Text(label_round, text="Score player 2", align="left")
  198. score_p1 = Text(label_round, text="0", align="left" )
  199.  
  200. leader_board = Box(app, border = 3)
  201. label_leader = Text(leader_board, align="bottom")
  202. leader = Text(leader_board, text="Leader", align="bottom")
  203.  
  204.  
  205. window = Window(app, title="Second window")
  206. text = Text(window, text="These are the rules:\n HAVE FUN!!!")
  207. window.hide()
  208.  
  209.  
  210. def open_window():
  211.     window.show()
  212.  
  213. def close_window():
  214.     window.hide()
  215.  
  216.  
  217. open_button = PushButton(app, text="Rules", command=open_window, align="right")
  218. close_button = PushButton(window, text="Close", command=close_window, align="bottom")
  219.  
  220. setup_round()
  221. setup_round_2()
  222.  
  223.  
  224. # start the timer
  225. rounds_p1.value = 0
  226. rounds_p2.value = 1
  227. timer.value = 20
  228. timer.repeat(1000, counter)
  229. timer.repeat(1000, counter_2)
  230.  
  231.  
  232. app.display()
  233.