Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from random import shuffle, randint
- from guizero import App, Box, Picture,Text, PushButton
- # set the path to the emoji folder on your computer
- emojis_dir = "emojis"
- # create a list of the locations of the emoji images
- emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
- # shuffle the emojis
- shuffle(emojis)
- app = App("emoji match")
- result = Text(app)
- attempts=0
- correct =0
- def match_emoji(matched):
- global attempts, correct
- attempts+=1
- if matched:
- correct+=1
- txt.value = "CORRECT You have " +str(correct) + ' correct out of ' +str(attempts) + 'attempts'
- else:
- txt.value = "INCORRECT You have " +str(correct) + ' correct out of ' +str(attempts) + 'attempts'
- txt.text_color='red'
- new_round()
- txt=Text(app, text='Click on the matching Emoji in the right grid. Good Luck ',align='top')
- # create a box to house the grid
- pictures_box = Box(app, layout="grid",align='left', border=3)
- buttons_box = Box(app, layout="grid", align='right', border=3)
- # create an empty list to which pictures and one for buttons will be added
- pictures = []
- buttons=[]
- for x in range(0,3):
- for y in range(0,3):
- # put the pictures into the list
- picture = Picture(pictures_box, grid=[x,y])
- pictures.append(picture)
- button = PushButton(buttons_box, grid=[x,y])
- buttons.append(button)
- def new_round():
- global attempts, correct
- if len(emojis)<8:
- ## print('end out of emoji')
- ## print(attempts)
- ## print(emojis)
- exit()
- match_number=randint(0,8)# pick random emoji place for match picture
- pnum=0
- match_pic=None
- # for each picture in the list but save a random one for match
- for picture in pictures:
- picture.image = emojis.pop()
- if pnum ==match_number:
- match_pic=picture.image #save selected matching emoji
- pnum+=1
- match_dup=randint(0,8) #pick random place to put matching pictue
- pnum=0
- #Now same for buttons
- for button in buttons:
- if pnum==match_dup:
- button.image=match_pic
- button.update_command(match_emoji, [True])
- else:
- button.image = emojis.pop()
- button.update_command(match_emoji, [False])
- pnum+=1
- # set the command to be called and pass False, as these emoji wont be the matching ones
- new_round()
- app.display()
Add Comment
Please, Sign In to add comment