Advertisement
Mori007

emojigameready

Feb 14th, 2021
618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.44 KB | None | 0 0
  1. mport os
  2. from random import shuffle, randint
  3. from guizero import App, Box, Picture, PushButton, Text, warn
  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 counter():
  48.     timer.value = int(timer.value) - 1
  49.     # is it game over?
  50.     if int(timer.value) == 0:
  51.         timer.cancel(counter)
  52.         # reset the timer
  53.         result.value = "Game over!"
  54.         warn("Time Out", "you've run out of the time and Score =  "+ score.value)
  55.         # reset timer
  56.         timer.value = "20"
  57.         # reset result
  58.         result.value = ""
  59.         # start new round
  60.         round_setup()
  61.         # start count the round game
  62.         round_count()
  63.         #restart timer
  64.         timer.repeat(1000, counter)
  65.  
  66. # Making a game rounds counting and connected to the def counter function
  67. def round_count():
  68.       rounds = 0
  69.       roundcount.value = int(roundcount.value) + 1
  70.    
  71.    
  72. app = App("THE EmOjI M A T C H")
  73.  
  74. # add in the extra features
  75. extra_features = Box(app)
  76.  
  77. timer = Text(extra_features, text="Get Ready")
  78.  
  79. # Set up Box for score and timer and rounds the game
  80. game_box = Box(app)
  81.  
  82. top_box = Box(game_box, align="top", width="fill")
  83.  
  84. Text(top_box, align="left", text="Score")
  85. score = Text(top_box, text="1", align="left")
  86.  
  87. # Timer setup to 20 second "
  88. timer = Text(top_box, text="20", align="right")
  89. Text(top_box, text="Time", align="right")
  90.  
  91. Text(top_box, text="Rounds", align="left")
  92. roundcount = Text(top_box, text="0", align="left")
  93.  
  94. # create a box to house the picture
  95. pictures_box = Box(game_box,layout="grid")
  96. # create a box to house the button
  97. buttons_box = Box(game_box, layout="grid")
  98.  
  99. # create an empty list to which pictures and button will be added
  100. buttons = []
  101. pictures = []
  102. for x in range(0,3):
  103.     for y in range(0,3):
  104.         # put the pictures into the list
  105.         picture = Picture(pictures_box, grid=[x,y])
  106.         pictures.append(picture)
  107.  
  108.         button = PushButton(buttons_box, grid=[x,y])
  109.         buttons.append(button)
  110.  
  111. result = Text(app)
  112.  
  113. round_setup()
  114.  
  115. round_count()
  116.  
  117. app.repeat(1000, counter)
  118.  
  119. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement