Mori007

29SecEmoji

Feb 14th, 2021
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.08 KB | None | 0 0
  1. import os
  2. from random import shuffle, randint
  3. from guizero import App, Box, Picture, PushButton, Text
  4. from guizero import warn
  5.  
  6. ## VARIABLES ########################################
  7. # set the path to the emoji folder on your computer #
  8. emojis_dir = "d:\WS_guizero\Ftrguizero\emojis"
  9. # create a list of the locations of the emoji images
  10. emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  11. # shuffle the emojis
  12. shuffle(emojis)
  13.  
  14. ## FUNCTION ##
  15. def round_setup():
  16. # for each picture in the list
  17.     for picture in pictures:
  18.     # make the picture a random emoji
  19.         picture.image = emojis.pop()
  20.  
  21.     for button in buttons:
  22.         button.image = emojis.pop()
  23.  
  24. # choose a new emoji
  25.     matched_emoji = emojis.pop()
  26.  
  27. # select a number at random
  28.     random_picture = randint(0,8)
  29. # change the image feature of the Picture with this index in the list of pictures to the new emoji
  30.     pictures[random_picture].image = matched_emoji
  31.     random_button = randint(0,8)
  32. # change the image feature of the PushButton with this index in the list of buttons to the new emoji
  33.     buttons[random_button].image = matched_emoji
  34.     buttons[random_button].update_command(match_emoji, [True])
  35.  
  36. # Check the matched emoji
  37. def match_emoji(matched):
  38.     if matched:
  39.        result.value == "correct"
  40.        score.value = int(score.value) + 1
  41.     else:
  42.         result.value == "incorrect"
  43.         score.value = int(score.value) - 1
  44.  
  45.     round_setup()
  46.  
  47. # Making time limitation with timer
  48. def counter():
  49.     timer.value = int(timer.value) - 1
  50.     # is it game over?
  51.     if int(timer.value) == 0:
  52.         timer.cancel(counter)
  53.         # reset the timer
  54.         result.value = "Game over!"
  55.         warn("Time Out", "you've run out of the time and Score =  "+ score.value)
  56.         # reset timer
  57.         timer.value = "20"
  58.         # reset result
  59.         result.value = ""
  60.         # start new round
  61.         round_setup()
  62.         #restart timer
  63.         timer.repeat(1000, counter)
  64.  
  65. app = App("Emoji Match")
  66.  
  67. # add in the extra features
  68. extra_features = Box(app)
  69. timer = Text(extra_features, text="Get Ready")
  70.  
  71. # Set up Box for score and timer
  72. game_box = Box(app)
  73. top_box = Box(game_box, align="top", width="fill")
  74. Text(top_box, align="left", text="Score ")
  75. score = Text(top_box, text="1", align="left")
  76. # Timer setup to 20 second "
  77. timer = Text(top_box, text="20", align="right")
  78. Text(top_box, text="Time", align="right")
  79. # create a box to house the picture
  80. pictures_box = Box(game_box,layout="grid")
  81. # create a box to house the button
  82. buttons_box = Box(game_box, layout="grid")
  83.  
  84. # create an empty list to which pictures and button will be added
  85. buttons = []
  86. pictures = []
  87. for x in range(0,3):
  88.     for y in range(0,3):
  89.         # put the pictures into the list
  90.         picture = Picture(pictures_box, grid=[x,y])
  91.         pictures.append(picture)
  92.  
  93.         button = PushButton(buttons_box, grid=[x,y])
  94.         buttons.append(button)
  95.  
  96.  
  97.  
  98. result = Text(app)
  99.  
  100. round_setup()
  101.  
  102. app.repeat(1000, counter)
  103.  
  104. app.display()
Advertisement
Add Comment
Please, Sign In to add comment