Advertisement
timber101

Untitled

Sep 3rd, 2019
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.20 KB | None | 0 0
  1. import os
  2. from random import shuffle, randint
  3. from guizero import App, Box, Picture, PushButton, Text, warn
  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. def match_emoji(matched):
  13.     if matched:
  14.         result.value = "correct"
  15.     else:
  16.         result.value = "incorrect"
  17.        
  18.     setup_round()
  19.  
  20. def setup_round():
  21.     # for each picture and button in the list assign an emoji to its image feature
  22.     for picture in pictures:
  23.         # make the picture a random emoji
  24.         picture.image = emojis.pop()
  25.  
  26.     for button in buttons:
  27.         # make the image feature of the PushButton a random emoji
  28.         button.image = emojis.pop()
  29.         # set the command to be called and pass False, as these emoji wont be the matching ones
  30.         button.update_command(match_emoji, [False])
  31.  
  32.     # choose a new emoji
  33.     matched_emoji = emojis.pop()
  34.  
  35.     # select a number at random
  36.     random_picture = randint(0,8)
  37.     # change the image feature of the Picture with this index in the list of pictures to the new emoji
  38.     pictures[random_picture].image = matched_emoji
  39.  
  40.     random_button = randint(0,8)
  41.     # change the image feature of the PushButton with this index in the list of buttons to the new emoji
  42.     buttons[random_button].image = matched_emoji
  43.     # set the command to be called and pass True, as this is the matching emoji
  44.     buttons[random_button].update_command(match_emoji, [True])
  45.  
  46. def counter():
  47.     timer.value = int(timer.value) - 1
  48.     if int(timer.value) == 0:
  49.         timer.cancel(counter)
  50.         # reset the timer
  51.         result.value = "Game Over"
  52.         warn("Time Out", "you've run out of time")
  53.         # reset timer
  54.         timer.value = "20"
  55.         # reset result
  56.         result.value = ""
  57.         # start new round
  58.         setup_round()
  59.         #restart timer
  60.         timer.repeat(1000, counter)
  61.         #update round number
  62.         round.value = int(round.value)+1
  63.        
  64. # setup the app
  65. app = App("emoji match")
  66.  
  67. result = Text(app)
  68.  
  69. # create a box to house the grids
  70. game_box = Box(app)
  71. # create a box to house the pictures
  72. pictures_box = Box(game_box, layout="grid")
  73. # create a box to house the buttons
  74. buttons_box = Box(game_box, layout="grid")
  75.  
  76. # create the an empty lists to add the buttons and pictures to
  77. buttons = []
  78. pictures = []
  79. # create 9 PushButtons with a different grid coordinate and add to the list
  80. for x in range(0,3):
  81.     for y in range(0,3):
  82.         # put the pictures and buttons into the lists
  83.         picture = Picture(pictures_box, grid=[x,y])
  84.         pictures.append(picture)
  85.        
  86.         button = PushButton(buttons_box, grid=[x,y])
  87.         buttons.append(button)
  88.  
  89.  
  90. # add in the extra features
  91. extra_features = Box(app)
  92. timer = Text(extra_features, text="Get Ready")
  93. round = Text(extra_features, text="round")
  94. score = Text(extra_features, text="score")
  95.  
  96.  
  97. setup_round()
  98.  
  99. # start the timer
  100. timer.value = 20
  101. timer.repeat(1000,counter)
  102.  
  103. round.value = 1
  104.  
  105. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement