Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from random import *
- from guizero import *
- # 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)
- def match_emoji(matched):
- global r
- if matched:
- result.value = "correct"
- result.text_color="red"
- else:
- result.value = "incorrect"
- result.text_color="blue"
- rounds.value = int(rounds.value) + 1
- setup_round()
- def counter():
- timer.value = int(timer.value) - 1
- if int(timer.value) == 0:
- timer.cancel(counter)
- # reset the timer
- result.value = "Game Over"
- warn("Time Out", "You've run out of time")
- # reset timer
- timer.value = "20"
- # reset result
- result.value = ""
- # start new round
- rounds.value=0
- setup_round()
- #restart timer
- timer.repeat(1000, counter)
- def setup_round():
- # for each picture and button in the list assign an emoji to its image feature
- 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])
- matched_emoji = emojis.pop()
- # select a number at random
- random_picture = randint(0,8)
- # change the image feature of the Picture with this index in the list of pictures to the new emoji
- pictures[random_picture].image = matched_emoji
- random_button = randint(0,8)
- # change the image feature of the PushButton with this index in the list of buttons to the new emoji
- buttons[random_button].image = matched_emoji
- buttons[random_button].update_command(match_emoji, [True])
- # setup app
- app = App("emoji match")
- # create a box to house the grid
- 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")
- # create an empty list to which pictures and buttons will be added
- pictures = []
- buttons = []
- for x in range(0,3):
- for y in range(0,3):
- # put the pictures and buttons into the list
- picture = Picture(pictures_box, grid=[x,y])
- pictures.append(picture)
- button = PushButton(buttons_box, grid=[x,y])
- buttons.append(button)
- # add in the extra features
- extra_features = Box(app)
- result = Text(extra_features)
- timer_text = Text(extra_features, text="Time: ")
- timer = Text(extra_features, text="Get Ready")
- rounds = Text(extra_features, text="Round: ")
- rounds = Text(extra_features, text=" ")
- rounds.value = 0
- setup_round()
- # start the timer
- timer.value = 20
- timer.repeat(1000, counter)
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement