Advertisement
Mori007

emojimr

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