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, 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)
- # setup the app
- app = App("emoji match", height=300)
- global player
- player=1
- # create a control box to house commands
- control_box = Box(app, layout="auto")
- timer = Text(control_box, text="Time", width=4, align="left" )
- points_lab1 = Text(control_box, text="RED ", bg="white", color="red", width=4, align="left" )
- points1 = Text(control_box, text="0", width=4, align="left" )
- points_lab2 = Text(control_box, text="BLUE", bg="white", color="blue", width=4, align="left" )
- points2 = Text(control_box, text="0", width=4, align="left" )
- player_label = Text(control_box, text="player is red ", width=14, align="left" )
- # 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="left")
- # create a box to house the buttons
- buttons_box = Box(game_box, layout="grid", align="right")
- def match_emoji(matched):
- global player
- timer.cancel(counter)
- if matched:
- if (player == 1):
- points1.value = int(points1.value) + 1
- else:
- points2.value = int(points2.value) + 1
- else:
- if (player == 1):
- points1.value = int(points1.value) - 1
- else:
- points2.value = int(points2.value) - 1
- if (player == 1):
- player_label.value="player is blue"
- player = 2
- else:
- player_label.value="player is red "
- player = 1
- setup_round()
- timer.value = 30
- timer.repeat(1000, counter)
- 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])
- # 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)
- setup_round()
- def counter():
- global player
- timer.value = int(timer.value) - 1
- if (int(timer.value) == 0):
- # reset the timer
- timer.cancel(counter)
- # warning
- if (int(timer.value) == 0):
- warn("Time Out", "you've run out of time and lost your points")
- # reset points
- if (player == 1):
- points1.value = 0
- player = 2
- else:
- points2.value = 0
- player = 1
- # reset timer
- timer.value = "30"
- # start new round
- setup_round()
- #restart timer
- timer.repeat(1000, counter)
- # start the timer
- timer.value = 30
- timer.repeat(1000, counter)
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement