- import os
- from random import shuffle, randint
- from guizero import App, Box, Picture, PushButton, Text, warn, info, Window, PushButton
- # 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 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 setup_round_2():
- # 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_2, [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_2, [True])
- def match_emoji(matched):
- if matched:
- result.value = "correct"
- score_p1.value = int(score_p1.value) + 1
- if int(score_p1.value) == 3:
- app.info("bonus", "BONUS")
- #timer.cancel(counter)
- timer.value = int(timer.value) + 10
- else:
- result.value = "incorrect"
- score_p1.value = int(score_p1.value) - 1
- setup_round()
- def match_emoji_2(matched):
- if matched:
- result.value = "correct"
- score_p2.value = int(score_p2.value) + 1
- if int(score_p2.value) == 3:
- app.info("bonus", "BONUS")
- #timer.cancel(counter_2)
- timer.value = int(timer.value) + 10
- else:
- result.value = "incorrect"
- score_p2.value = int(score_p2.value) - 1
- setup_round_2()
- 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" + " \n Player 2 score was " + score_p1.value)
- rounds_p2.value = int(rounds_p2.value) + 1
- # reset timer
- timer.value = "20"
- # reset result
- #result.value = ""
- # start new round
- setup_round_2()
- #restart timer
- timer.repeat(1000, counter_2)
- #reset score
- #score_p1.value = "0"
- if score_p2.value > score_p1.value:
- label_leader.value = "P1"
- else:
- label_leader.value = "P2"
- def counter_2():
- timer.value = int(timer.value) - 1
- if int(timer.value) == 0:
- timer.cancel(counter_2)
- # reset the timer
- result.value = "Game Over"
- warn("Time Out", "you've run out of time" + " \n Player 1 score was " + score_p2.value)
- rounds_p1.value = int(rounds_p1.value) + 1
- # reset timer
- timer.value = "20"
- # reset result
- #result.value = ""
- # start new round
- setup_round()
- #restart timer
- timer.repeat(1000, counter)
- #reset score
- #score_p1.value = "0"
- if score_p2.value > score_p1.value:
- label_leader.value = "P1"
- else:
- label_leader.value = "P2"
- # setup the app
- app = App("emoji match")
- # 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", align="right")
- # create a box to house the buttons
- buttons_box = Box(game_box, layout="grid", align="right")
- # 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)
- result = Text(app, size = 30)
- extra_features = Box(app)
- timer = Text(extra_features, text="Get Ready")
- scoreboard = Box(app, border =3)
- score_p2 = Text(scoreboard, text="0", align="right")
- label_p2 = Text(scoreboard, text="Score player 1", align="right")
- rounds_p2 = Text(scoreboard, align="right")
- label_round_p2 = Text(scoreboard, text="Rounds P1", align="right")
- label_round = Box(app, border =3)
- label_round_p1 = Text(label_round, text="Rounds P2", align="left")
- rounds_p1 = Text(label_round, align="left")
- label_p1 = Text(label_round, text="Score player 2", align="left")
- score_p1 = Text(label_round, text="0", align="left" )
- leader_board = Box(app, border = 3)
- label_leader = Text(leader_board, align="bottom")
- leader = Text(leader_board, text="Leader", align="bottom")
- window = Window(app, title="Second window")
- text = Text(window, text="These are the rules:\n HAVE FUN!!!")
- window.hide()
- def open_window():
- window.show()
- def close_window():
- window.hide()
- open_button = PushButton(app, text="Rules", command=open_window, align="right")
- close_button = PushButton(window, text="Close", command=close_window, align="bottom")
- setup_round()
- setup_round_2()
- # start the timer
- rounds_p1.value = 0
- rounds_p2.value = 1
- timer.value = 20
- timer.repeat(1000, counter)
- timer.repeat(1000, counter_2)
- app.display()