scipiguy

GUI Zero emoji game timed

Nov 18th, 2019
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. import os
  2. from random import shuffle, randint
  3. from guizero import App, Box, Picture, Text, PushButton, 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.     setup_round()
  18.  
  19. def setup_round():
  20.     global num_rounds
  21.     emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  22.     shuffle(emojis)
  23.    
  24.     for picture in pictures:
  25.         picture.image = emojis.pop()
  26.  
  27.     for button in buttons:
  28.         button.image = emojis.pop()
  29.         button.update_command(match_emoji, [False])
  30.  
  31.     matched_emoji = emojis.pop()
  32.  
  33.     random_picture = randint(0,8)
  34.     pictures[random_picture].image = matched_emoji
  35.  
  36.     random_button = randint(0,8)
  37.     buttons[random_button].image = matched_emoji
  38.     buttons[random_button].update_command(match_emoji, [True])
  39.  
  40.     num_rounds = num_rounds + 1
  41.     rounds_played.value = "Round " +str(num_rounds)
  42.    
  43. def counter():
  44.     global num_rounds
  45.     timer.value = int(timer.value) - 1
  46.     if int(timer.value) == 0:
  47.         timer.cancel(counter)
  48.         result.value = "Game Over"
  49.         warn("Time Up", "You have run out of time!")
  50.         timer.value = 20
  51.         result.value = ""
  52.         setup_round()
  53.         timer.repeat(1000, counter)
  54.         num_rounds = 0
  55.  
  56. num_rounds = 0
  57.  
  58. app = App("Emoji Match")
  59. result = Text(app)
  60. rounds_played = Text(app, text="Round " + str(num_rounds))
  61.  
  62. game_box = Box(app, layout="grid")
  63.  
  64. # create a box to house the grid
  65. pictures_box = Box(game_box, layout="grid", grid=[0,0])
  66. buttons_box = Box(game_box, layout="grid", grid =[1,0])
  67. # create an empty list to which pictures will be added
  68. buttons = []
  69. pictures = []
  70. for x in range(0,3):
  71.     for y in range(0,3):
  72.         # put the pictures into the list
  73.         picture = Picture(pictures_box, grid=[x,y])
  74.         pictures.append(picture)
  75.  
  76.         button = PushButton(buttons_box, grid=[x,y])
  77.         buttons.append(button)
  78.  
  79. extra_features = Box(app)
  80. timer = Text(extra_features, text="Get Ready")
  81.  
  82. setup_round()
  83.  
  84. timer.value = 20
  85. timer.repeat(1000, counter)
  86.  
  87. app.display()
Advertisement
Add Comment
Please, Sign In to add comment