scipiguy

FutureLearn GUIZero Emoji Game v2

Nov 17th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. import os
  2. from random import shuffle, randint
  3. from guizero import App, Box, Picture, Text, PushButton
  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.     emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  21.     shuffle(emojis)
  22.    
  23.     for picture in pictures:
  24.         picture.image = emojis.pop()
  25.  
  26.     for button in buttons:
  27.         button.image = emojis.pop()
  28.         button.update_command(match_emoji, [False])
  29.  
  30.     matched_emoji = emojis.pop()
  31.  
  32.     random_picture = randint(0,8)
  33.     pictures[random_picture].image = matched_emoji
  34.  
  35.     random_button = randint(0,8)
  36.     buttons[random_button].image = matched_emoji
  37.     buttons[random_button].update_command(match_emoji, [True])
  38.  
  39. app = App("Emoji Match")
  40. result = Text(app)
  41.  
  42. game_box = Box(app, layout="grid")
  43.  
  44. # create a box to house the grid
  45. pictures_box = Box(game_box, layout="grid", grid=[0,0])
  46. buttons_box = Box(game_box, layout="grid", grid =[1,0])
  47. # create an empty list to which pictures will be added
  48. buttons = []
  49. pictures = []
  50. for x in range(0,3):
  51.     for y in range(0,3):
  52.         # put the pictures into the list
  53.         picture = Picture(pictures_box, grid=[x,y])
  54.         pictures.append(picture)
  55.  
  56.         button = PushButton(buttons_box, grid=[x,y])
  57.         buttons.append(button)
  58.  
  59. setup_round()
  60.  
  61. app.display()
Add Comment
Please, Sign In to add comment