Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mport os
- from random import shuffle, randint
- from guizero import App, Box, Picture, PushButton, Text, warn
- ## VARIABLES ########################################
- # set the path to the emoji folder on your computer #
- emojis_dir = "d:\WS_guizero\Ftrguizero\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)
- ## FUNCTION ##
- def round_setup():
- # for each picture in the list
- for picture in pictures:
- # make the picture a random emoji
- picture.image = emojis.pop()
- for button in buttons:
- button.image = emojis.pop()
- # choose a new emoji
- 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])
- # Check the matched emoji
- def match_emoji(matched):
- if matched:
- result.value == "correct"
- score.value = int(score.value) + 1
- else:
- result.value == "incorrect"
- score.value = int(score.value) - 1
- round_setup()
- # Making time limitation with timer
- def counter():
- timer.value = int(timer.value) - 1
- # is it game over?
- if int(timer.value) == 0:
- timer.cancel(counter)
- # reset the timer
- result.value = "Game over!"
- warn("Time Out", "you've run out of the time and Score = "+ score.value)
- # reset timer
- timer.value = "20"
- # reset result
- result.value = ""
- # start new round
- round_setup()
- # start count the round game
- round_count()
- #restart timer
- timer.repeat(1000, counter)
- # Making a game rounds counting and connected to the def counter function
- def round_count():
- rounds = 0
- roundcount.value = int(roundcount.value) + 1
- app = App("THE EmOjI M A T C H")
- # add in the extra features
- extra_features = Box(app)
- timer = Text(extra_features, text="Get Ready")
- # Set up Box for score and timer and rounds the game
- game_box = Box(app)
- top_box = Box(game_box, align="top", width="fill")
- Text(top_box, align="left", text="Score")
- score = Text(top_box, text="1", align="left")
- # Timer setup to 20 second "
- timer = Text(top_box, text="20", align="right")
- Text(top_box, text="Time", align="right")
- Text(top_box, text="Rounds", align="left")
- roundcount = Text(top_box, text="0", align="left")
- # create a box to house the picture
- pictures_box = Box(game_box,layout="grid")
- # create a box to house the button
- buttons_box = Box(game_box, layout="grid")
- # create an empty list to which pictures and button will be added
- buttons = []
- pictures = []
- 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)
- result = Text(app)
- round_setup()
- round_count()
- app.repeat(1000, counter)
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement