Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from random import shuffle, choice, randint
- from guizero import App, Box, Picture, 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)
- #create the app and put it in a box
- app = App("emoji match")
- game_box = Box(app)
- # create a box to house the grid
- pictures_box = Box(game_box, layout="grid")
- buttons_box = Box(game_box, layout="grid")
- # create an empty list to which pictures will be added
- pictures = []
- buttons = []
- #use a loop from 1-3 (y) within a loop of 1-3 (x) to create a 3x3 grid
- 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)
- #put picture buttons in grid
- button = PushButton(buttons_box, grid = [x,y])
- buttons.append(button)
- #Isolate a single emoji from list to match in pictures and buttons lists
- matched_emoji = emojis.pop()
- #choose a random position to insert the picture in the pictures list
- random_picture = randint(0,8)
- #choose a random position to insert the picture in the buttons list
- random_button = randint(0,8)
- # for each picture in the list
- for picture in pictures:
- #if the current picture is the randomly chosen match, insert the matched_emoji
- if picture == pictures[random_picture]:
- picture.image = matched_emoji
- else:
- # make the picture a random emoji
- picture.image = emojis.pop()
- #for each button in list
- for button in buttons:
- #if current button is the randomly chosen match, insert the matched_emoji
- if button == buttons[random_button]:
- button.image = matched_emoji
- else:
- #insert a random emoji
- button.image = emojis.pop()
- #start app
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement