johnpentyrch

GUIemojiGame

May 11th, 2020
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 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. app = App("emoji match")
  13.  
  14. result = Text(app)
  15. attempts=0
  16. correct =0
  17.  
  18. def match_emoji(matched):
  19.     global attempts, correct
  20.     attempts+=1
  21.     if matched:
  22.         correct+=1
  23.         txt.value = "CORRECT You have " +str(correct) + ' correct out of ' +str(attempts) + 'attempts'
  24.     else:
  25.         txt.value = "INCORRECT You have " +str(correct) + ' correct out of ' +str(attempts) + 'attempts'
  26.         txt.text_color='red'
  27.     new_round()
  28.    
  29. txt=Text(app, text='Click on the matching Emoji in the right grid. Good Luck  ',align='top')
  30.  
  31.  
  32. # create a box to house the grid
  33. pictures_box = Box(app, layout="grid",align='left', border=3)
  34. buttons_box = Box(app, layout="grid", align='right', border=3)
  35.  
  36. # create an empty list to which pictures and one for buttons will be added
  37. pictures = []
  38. buttons=[]
  39.  
  40. for x in range(0,3):
  41.     for y in range(0,3):
  42.         # put the pictures into the list
  43.         picture = Picture(pictures_box, grid=[x,y])
  44.         pictures.append(picture)      
  45.         button = PushButton(buttons_box, grid=[x,y])
  46.         buttons.append(button)
  47.  
  48. def new_round():
  49.     global attempts, correct
  50.     if len(emojis)<8:
  51. ##        print('end out of emoji')
  52. ##        print(attempts)
  53. ##        print(emojis)
  54.         exit()
  55.  
  56.     match_number=randint(0,8)# pick random emoji place for match picture
  57.     pnum=0
  58.     match_pic=None
  59.     # for each picture in the list but save a random one for match
  60.     for picture in pictures:
  61.         picture.image = emojis.pop()
  62.         if pnum ==match_number:
  63.             match_pic=picture.image #save selected matching emoji
  64.         pnum+=1
  65.        
  66.     match_dup=randint(0,8) #pick random place to put matching pictue
  67.     pnum=0
  68.     #Now same for buttons
  69.     for button in buttons:
  70.         if pnum==match_dup:
  71.             button.image=match_pic
  72.             button.update_command(match_emoji, [True])
  73.         else:
  74.             button.image = emojis.pop()
  75.             button.update_command(match_emoji, [False])
  76.         pnum+=1
  77.     # set the command to be called and pass False, as these emoji wont be the matching ones
  78.        
  79. new_round()
  80. app.display()
Add Comment
Please, Sign In to add comment