maroph icon

emojis_game1.py

maroph | PRO | 12/13/19 04:04:39 PM UTC | 0 ⭐ | 586 👁️ | Never ⏰ | []
Python |

2.07 KB

|

None

|

0 👍

/

0 👎

from guizero import App, Box, Picture, PushButton
import os
from random import randint
from random import shuffle
 
 
# 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)
 
app = App("Emoji Match Game")
 
game_box = Box(app, border=True)
 
pictures_box = Box(game_box, layout="grid", border=True)
buttons_box = Box(game_box, layout="grid", border=True)
 
# create a empty lists to which pictures and buttons will be added
# we use the property visible to distinguish between set and not
# set image properties
# visible False: image not yet set
pictures = []
buttons = []
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])
        picture.visible = False
        pictures.append(picture)
        # put the buttons into the list
        button = PushButton(buttons_box, grid=[x, y])
        button.visible = False
        buttons.append(button)
 
 
# select the matching emoji
matched_emoji = emojis.pop()
# select a random picture index for this emoji
idx_picture = randint(0,8)
# select a random button index for this emoji
idx_button = randint(0,8)
 
# set the chosen picture's image to the matched_emoji
pictures[idx_picture].image = matched_emoji
pictures[idx_picture].visible = True
# set the chosen button's image to the matched_emoji
buttons[idx_button].image = matched_emoji
buttons[idx_button].visible = True
 
 
# set all image properties to an emoji
# if the related visible property is False
for idx in range(0, len(pictures)):
    if not pictures[idx].visible:
        pictures[idx].image = emojis.pop()
        pictures[idx].visible = True
    if not buttons[idx].visible:
        buttons[idx].image = emojis.pop()
        buttons[idx].visible = True
 
 
# now all image properties are set and we can display the GUI
app.display()

Comments