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
- # 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)
- #print(len(emojis))
- #print(emojis) - testing shuffling works
- app = App("emoji match", width = 500, height = 800)
- # create a box to house the grid
- pictures_box = Box(app, layout="grid")
- buttons_box = Box(app, layout="grid")
- # create an empty list to which pictures will be added
- pictures = []
- for x in range(0,4):
- for y in range(0,4):
- # put the pictures into the list
- picture = Picture(pictures_box, grid=[x,y])
- pictures.append(picture)
- # for each picture in the list
- for picture in pictures:
- # make the picture a random emoji
- picture.image = emojis[1]
- emojis.remove(emojis[1])##emojis.pop()##
- #print(len(emojis))
- #create and empty list into which the butons will be added
- buttons = []
- for x in range (0,4):
- for y in range (0,4):
- # add the pictures to the list
- button = PushButton(buttons_box, grid=[x,y])
- buttons.append(button)
- #print(len(emojis))
- #print("buttons length",len(buttons))
- for button in buttons:
- button.image= emojis[1]
- emojis.remove(emojis[1])
- '''
- # replace one in each set to be the matcher
- matcher_emojis = emojis[1]
- emojis.remove(emojis[1])
- #select a random position index from the pictures
- rand_pict = randint(0,15)
- #change picture
- pictures[rand_pict].image = matcher_emojis
- #select a random position index from the buttons
- rand_button = randint(0,15)
- #change button picture
- buttons[rand_button].image = matcher_emojis
- print("picture",rand_pict)
- print("button",rand_button)
- '''
- # my method, pick one random picture from the pictures and replace a random image in the buttons
- #select a random position index from the pictures
- rand_pict = randint(0,15)
- #select a random position index from the buttons
- rand_button = randint(0,15)
- #replace the button image with the one from the pictures
- buttons[rand_button].image = pictures[rand_pict].image
- print("picture master index", rand_pict)
- print("button swapped", rand_button)
- print(buttons[rand_button].image , pictures[rand_pict].image) # thanks to Stephen Carter
- buttons[rand_button].bg="green" # thanks to Stephen Carter
- pictures[rand_pict].bg="green" # thanks to Stephen Carter
- #print(len(emojis))
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement