Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import neccessary libraries
- import os
- from random import shuffle, choice, randint
- from guizero import App, Box, Picture, PushButton, Text, warn
- from time import sleep
- #create the app and put it in a box and create seperate boxes for display and choice
- app = App()
- pic_box = Box(app, align = "left")
- button_box = Box(app, align = "right")
- #create text to display result and a timer
- result = Text(app, align = "top")
- time = Text(app, text = 10, align = "top")
- score = Text(app, text = 0, align = "top")
- rounds = Text(app, text = 0, align = "bottom")
- #display whether used choice matched
- def match_emoji(matched):
- if matched:
- result.value = "YES!"
- score.value = int(score.value) + 1
- else:
- result.value = "NO!"
- # create a box to house the grid
- pictures_box = Box(pic_box, layout="grid")
- buttons_box = Box(button_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)
- #Function to setup new game
- def setup_game():
- #set time to 30 seconds
- time.value = 30
- #increase round count by 1
- rounds.value = int(rounds.value) + 1
- #import emojis library and 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)
- #clear result text
- result.value = " "
- #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
- button.update_command(match_emoji, [True])
- else:
- #insert a random emoji
- button.image = emojis.pop()
- button.update_command(match_emoji, [False])
- #end game function
- def quit_game():
- app.destroy()
- time.cancel(countdown)
- button.cancel(button_colour_switch)
- #timer function
- def countdown():
- if int(time.value) > 0:
- time.value = int(time.value) - 1
- else:
- result.value = "END"
- warn(title = "Game Over", text = "Time Out!")
- quit_game()
- #change button background every second; change text colour to be visible
- def button_colour_switch():
- if end_game.bg == "black":
- end_game.bg = "white"
- end_game.text_color = "black"
- else:
- end_game.bg = "black"
- end_game.text_color = "white"
- #repeat timer and colour-switching functions
- time.repeat(1000, countdown)
- button.repeat(1000,button_colour_switch)
- #Create a button to and and another to reset the game
- end_game = PushButton(app, command = quit_game, text = "Quit", align = "bottom")
- reset_game = PushButton(app, command = setup_game, text = "Next", align = "bottom")
- #setup first round of emojis and quit button to black
- setup_game()
- button_colour_switch()
- #start app
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement