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
- ## 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 reduce_time():
- timer.value = int(timer.value) - 1
- # is it game over?
- if int(timer.value) < 0:
- result.value = "Game over! Score = " + score.value
- # hide the game
- pictures_box.hide()
- buttons_box.hide()
- timer.hide()
- score.hide()
- app = App("Emoji Match")
- # Set up Box for score and timer
- 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="4", align="left")
- timer = Text(top_box, text="30", align="right")
- Text(top_box, text="Time", align="right")
- # 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()
- app.repeat(1000, reduce_time)
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement