Advertisement
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, PushButton, Text
- def match_emoji(matched):
- if matched:
- result.value = "Correct!!!"
- emojis=emoji_list()
- new_try()
- else:
- result.value = "Incorrect"
- # set the path to the emoji folder on your computer
- emojis_dir = "C:\\Users\\xavi.cano\\Code\\emojis\\emojis"
- def emoji_list():
- # 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)
- return emojis
- Xrange=3
- Yrange=3
- buttons = []
- pictures = []
- def new_try():
- ## for each picture in the list
- for picture in pictures:
- ## make the picture a random emoji
- picture.image = emojis.pop()
- for button in buttons:
- # make the image feature of the PushButton a random emoji
- button.image = emojis.pop()
- # set the command to be called and pass False, as these emoji wont be the matching ones
- button.update_command(match_emoji, [False])
- random_button = randint(0,8)
- random_picture = randint(0,8)
- buttons[random_button].image=pictures[random_picture].image
- buttons[random_button].update_command(match_emoji, [True])
- value=120
- def button_sec():
- if buttonsec.bg == "black":
- buttonsec.bg = "white"
- buttonsec.text_color = "black"
- else:
- buttonsec.bg = "black"
- buttonsec.text_color = "white"
- def decrease():
- counter=int(timer.value)-1
- button_sec()
- if int(rounds.value)==5:
- warn("Go home!!!", "Too many tries")
- if counter==0:
- timer.value="20"
- rounds.value=str(int(rounds.value)+1)
- else:
- timer.value = str(counter)
- emojis=[]
- app = App("emoji match")
- # create a box to house the grids
- game_box = Box(app)
- # create a box to house the pictures
- pictures_box = Box(game_box, layout="grid", align="left")
- # create a box to house the buttons
- buttons_box = Box(game_box, layout="grid", align="right")
- result = Text(app)
- timer=Text(app, text="20")
- buttonsec=PushButton(app,text=" seconds")
- buttonsec.bg="black"
- buttonsec.text_color = "white"
- rounds=Text(app, text="1")
- emojis=emoji_list()
- timer.repeat(1000, decrease)
- # create 9 PushButtons with a different grid coordinate and add to the list
- for x in range(0,Xrange):
- for y in range(0,Yrange):
- # put the pictures and buttons into the lists
- picture = Picture(pictures_box, grid=[x,y])
- pictures.append(picture)
- button = PushButton(buttons_box, grid=[x,y])
- buttons.append(button)
- new_try()
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement