maroph icon

emojis_game2.py

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

3.75 KB

|

None

|

0 👍

/

0 👎

from guizero import App, Box, MenuBar, Picture, PushButton, Text
import os
from random import randint
from random import shuffle
 
 
no_success = 0
no_failures = 0
 
 
def setup_round():
    global emojis_save
    global pictures
    global buttons
 
    emojis = emojis_save.copy()
    shuffle(emojis)
 
    for idx in range(0, len(pictures)):
        pictures[idx].visible = False
        buttons[idx].visible = False
        buttons[idx].update_command(match_emoji, [False])
 
    # 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 the command to be called and pass True, as this is the matching emoji
    buttons[idx_button].update_command(match_emoji, [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
 
 
def match_emoji(matched):
    global no_success
    global no_failures
 
    if matched:
        no_success = no_success + 1
        result.value = 'correct (' + str(no_success) + '/' + str(no_failures) + ')'
    else:
        no_failures = no_failures + 1
        result.value = 'incorrect (' + str(no_success) + '/' + str(no_failures) + ')'
 
    setup_round()
 
 
def exit_app():
    global app
    app.destroy()
 
 
def about_app():
    global app
    app.info('About', "The Emoji Matching Game")
 
 
# set the path to the emoji folder on your computer
emojis_dir = "emojis"
 
# create a list of the locations of the emoji images
# in each round we copy this data to the emojis array
emojis_save = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
 
 
app = App("Emoji Match Game")
app.when_closed = exit_app
 
menubar = MenuBar(app,
                  # menu bar
                  toplevel=["File", "Help"],
                  # menu items
                  options=[
                      # File
                      [
                          # items in File
                          ["Exit", exit_app]
                      ],
                      # Help
                      [
                          # items in Help
                          ["About", about_app]
                      ]
                  ])
 
result = Text(app)
 
game_box = Box(app, border=True)
pictures_box = Box(game_box, layout="grid", align="left")
buttons_box = Box(game_box, layout="grid", border="3", align="left")
 
# create empty lists to which pictures and buttons will be added
# we use the property visible to distinguish between set and not
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])
        pictures.append(picture)
        # put the buttons into the list
        button = PushButton(buttons_box, grid=[x, y])
        # set the command to be called and pass False, as these emoji wont be the matching ones
        buttons.append(button)
 
 
setup_round()
 
# now all image properties are set and we can display the GUI
app.display()

Comments