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, PushButton, Picture, Text
- import time
- def destroy_grids():
- # Clear images
- pic_images.clear()
- pic_buttons.clear()
- # Remove each widget in the boxes, this allows the ability to alter the size for next game.
- for widget in top_emoji_box.children:
- widget.destroy()
- for widget in bottom_emoji_box.children:
- widget.destroy()
- def play_matched():
- for widget in bottom_emoji_box.children:
- widget.disable()
- button_play_game.enable()
- text_timer.value = f"Time taken {round(time.time() - float(text_timer.value), 1)}s"
- text_timer.show()
- def setup_grids(width, height):
- # Generate grids, mirrored in each box.
- for x in range(width):
- for y in range(height):
- pic_image = Picture(top_emoji_box, grid=[x,y])
- pic_button = PushButton(bottom_emoji_box, command=match_emoji, args=[False], grid=[x,y])
- pic_images.append(pic_image)
- pic_buttons.append(pic_button)
- def setup_round(width, height):
- # set images and buttons to have a picture, remove from the list as we go.
- for pic_image in pic_images:
- pic_image.image = emojis.pop()
- for pic_button in pic_buttons:
- pic_button.image = emojis.pop()
- # Set a button to match a picture and indicate this with True.
- match_button = randint(0, width*height - 1)
- pic_buttons[match_button].image = pic_images[randint(0, width*height - 1)].image
- pic_buttons[match_button].update_command(match_emoji, [True])
- def match_emoji(matched):
- text_total_tries.value = int(text_total_tries.value) + 1
- if matched:
- text_result.value = "correct"
- text_total_correct.value = int(text_total_correct.value) + 1
- play_matched()
- else:
- text_result.value = "incorrect"
- def the_game():
- button_play_game.disable()
- # Game grid dimensions
- width = randint(3,6)
- height = randint(3,4)
- # need to remove all widgets, before we recreate the,
- destroy_grids()
- # Generate Grids
- setup_grids(width, height)
- setup_round(width, height)
- text_timer.value = time.time()
- text_timer.hide()
- # set the path to the emoji folder on your computer
- # and create a shuffled list of the locations of the emoji images
- emojis_dir = "emojis"
- 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(emojis)
- # picture object storage
- pic_images = []
- pic_buttons = []
- # The App window
- app=App("Emoji Match'em Game", width="490", height="750")
- # Create the boxes
- top_emoji_box = Box(app, layout="grid", align="top", width=480, height=330)
- control_box = Box(app, layout="grid")
- bottom_emoji_box = Box(app, layout="grid", align="bottom", width=480, height=320)
- button_play_game = PushButton(control_box, text="Start Game", command=the_game, grid=[1,0])
- text_result = Text(control_box, grid=[2,0])
- text_timer = Text(control_box, visible = False, grid=[3,0])
- text_total_tries_text = Text(control_box, text = "Tries", grid=[2,1])
- text_total_tries = Text(control_box, text = "0", grid=[3,1])
- text_total_correct_text = Text(control_box, text = "Correct Tries", grid=[2,2])
- text_total_correct = Text(control_box, text = "0", grid=[3,2])
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement