Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # My Emoji Match V3
- import os
- from random import shuffle, randint
- from guizero import App, Box, Picture, Text, PushButton, warn
- # 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("My Emoji Match V3", width=700, height=500)
- nos = 0
- rounds = Text(app)
- result = Text(app)
- def match_emoji(matched):
- if matched:
- result.value = "correct"
- else:
- result.value = "incorrect"
- setup_round()
- def setup_round():
- # for each picture in the list (872 pics)
- for picture in pictures:
- # make the picture a random emoji
- picture.image = emojis.pop()
- for button in buttons:
- # make the button 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])
- # make 2 of the pictures the same, but in different grid
- matched_emoji = emojis.pop()
- random_picture = randint(0,8)
- pictures[random_picture].image = matched_emoji
- random_button = randint(0,8)
- buttons[random_button].image = matched_emoji
- # say = Picture(game_box, image=matched_emoji)
- # set the command to be called and pass True, as this is the matching emoji
- buttons[random_button].update_command(match_emoji, [True])
- # def counter():
- # timer.value = int(timer.value) - 1
- # if int(timer.value) == 0:
- # # reset the timer
- # timer.value = 30
- def counter():
- timer.value = int(timer.value) - 1
- if int(timer.value) == 0:
- global nos
- nos += 1
- rounds.value = "Round "+str(nos)
- 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
- setup_round()
- #restart timer
- timer.repeat(1000, counter)
- # create a box to house the grids
- game_box = Box(app)
- # create box to house pictures
- pictures_box = Box(game_box, layout="grid", border=4, align="left")
- nl = Text(game_box, text=" ", align="left")
- # create a box to house the buttons
- buttons_box = Box(game_box, layout="grid", border=2, align="left")
- # create empty lists to which pictures will be added
- buttons = []
- pictures = []
- # populate pictures
- for x in range(0,3):
- for y in range(0,3):
- # put the pictures into the lists
- 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)
- timer = Text(extra_features, text="Get Ready")
- setup_round()
- # start the timer
- timer.value = 30
- timer.repeat(1000, counter)
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement