import os from random import shuffle, randint from guizero import App, Box, Picture, PushButton, Text, 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) def match_emoji(matched): if matched: result.value = "correct" score.value = int(score.value) + 1 rscore.value = int(rscore.value) + 1 tiar.value = tiar.value + "X" if tiar.value == "XXX": timer.value = int(timer.value) +5 tiar.value = "" else: result.value = "incorrect" unrscore.value = int(unrscore.value) + 1 tiar.value = "" setup_round() 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]) # 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 # 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: timer.cancel(counter) # reset the timer result.value = "Game Over" warn("Time Out", "you've run out of time final score:- " + rscore.value) # reset timer timer.value = "40" # reset result result.value = "" # start new round setup_round() #restart timer timer.repeat(1000, counter) #update round number round.value = int(round.value)+1 #reset score to 0 score.value = 0 unscore.value = 0 # setup the app app = App("emoji match", height=630) result = Text(app) # 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") # create a box to house the buttons buttons_box = Box(game_box, layout="grid") # create the an empty lists to add the buttons and pictures to buttons = [] pictures = [] # create 9 PushButtons with a different grid coordinate and add to the list for x in range(0,3): for y in range(0,3): # 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) # add in the extra features extra_features = Box(app) timer = Text(extra_features, text="Get Ready") roundboard = Box(app) rolabel = Text(roundboard, text = "Round", align="left") round = Text(roundboard, text="round") scoreboard = Box(app) sclabel = Text(scoreboard, text="Score", align="left") score = Text(scoreboard, text="0", align="left") rsclabel = Text(scoreboard, text="Runing Score", align="left") rscore = Text(scoreboard, text="0", align="left") unscoreboard = Box(app) unsclabel = Text(unscoreboard, text="Un Score", align="left") unscore = Text(unscoreboard, text="0", align="left") unrsclabel = Text(unscoreboard, text="Runing Un Score", align="left") unrscore = Text(unscoreboard, text="0", align="left") threeinarow = Box(app) tiar = Text(threeinarow, text = "", align ="left") setup_round() # start the timer timer.value = 40 timer.repeat(1000,counter) round.value = 1 app.display()