Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # imported functions
- import os
- from random import *
- from guizero import *
- from random import *
- class game_infos():
- rounds_played = "0"
- def exit_gui():
- app.destroy()
- def setup_round():
- for picture in pictures:
- # make the picture a random emoji
- picture.image = emojis.pop()
- for button in buttons:
- # make the button a random emoji
- button.image = emojis.pop()
- # set the command to be called and pass False, as these emoji wont be the matching ones
- button.update_command(match_emoji, [False])
- # make 2 of the pictures the same, but in different grid
- matched_emoji = emojis.pop()
- random_picture = randint(0, 8)
- pictures[random_picture].image = matched_emoji
- random_button = randint(0, 8)
- buttons[random_button].image = matched_emoji
- # set the command to be called and pass True, as this is the matching emoji
- buttons[random_button].update_command(match_emoji, [True])
- def match_emoji(matched):
- if matched:
- result.value = "correct"
- else:
- result.value = "incorrect"
- setup_round()
- game_infos.rounds_played = int(game_infos.rounds_played) + 1
- print(game_infos.rounds_played)
- game_played.value = "Played " + str(game_infos.rounds_played)
- # 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)
- def counter():
- timer.value = int(timer.value) - 1
- if int(timer.value) == 0:
- timer.cancel(counter)
- # reset the timer
- result.value = "Game Over"
- warn("Time Out", "you've run out of time")
- # reset timer
- timer.value = "20"
- # reset result
- result.value = ""
- # start new round
- setup_round()
- # restart timer
- timer.repeat(1000, counter)
- # def blinkBtn():
- # if btnTrick.bg == "red":
- # btnTrick.bg = "black"
- # btnTrick.text_color="white"
- # else:
- # btnTrick.bg = "red"
- # btnTrick.text_color = "Black"
- ###################### Begin of the App code ######################
- app = App("Emoji Match", height=600)
- game_played = Text(app, text="Played "+str(game_infos.rounds_played))
- result = Text(app, height=3)
- ###### blinky btn ########
- # btnTrick = PushButton(app, text="Color")
- # btnTrick.bg="red"
- # btnTrick.repeat(1000, blinkBtn)
- # add in the extra features
- extra_features = Box(app)
- timer = Text(extra_features, text="Get Ready")
- # create a box to house the grids
- game_box = Box(app)
- # create a box to house the grid
- pictures_box = Box(game_box, layout="grid")
- # create a box to house the buttons
- buttons_box = Box(game_box, layout="grid")
- # create an empty lists to add the buttons and pictures to
- pictures = []
- buttons = []
- # create 9 PushButtons with a different grid coordinate and add to the list
- for x in range(0, 3):
- for y in range(0, 3):
- # put the pictures and buttons into the lists
- button = PushButton(buttons_box, grid=[x, y])
- buttons.append(button)
- for x in range(0, 3):
- for y in range(0, 3):
- # put the pictures and buttons into the lists
- picture = Picture(pictures_box, grid=[x, y])
- pictures.append(picture)
- # for each picture and button in the list assign an emoji to its image feature
- for picture in pictures:
- # make the picture a random emoji
- picture.image = emojis.pop()
- for button in buttons:
- # make the image feature of the PushButton a random emoji
- button.image = emojis.pop()
- # set the command to be called and pass False, as these emoji wont be the matching ones
- button.update_command(match_emoji, [False])
- # for each picture in the list
- for picture in pictures:
- # make the picture a random emoji
- picture.image = emojis.pop()
- # choose a new emoji
- matched_emoji = emojis.pop()
- # select a number at random
- random_picture = randint(0, 8)
- # change the image feature of the Picture with this index in the list of pictures to the new emoji
- pictures[random_picture].image = matched_emoji
- random_button = randint(0, 8)
- # change the image feature of the PushButton with this index in the list of buttons to the new emoji
- buttons[random_button].image = matched_emoji
- # set the command to be called and pass True, as this is the matching emoji
- buttons[random_button].update_command(match_emoji, [True])
- # start the timer
- timer.value = 20
- timer.repeat(1000, counter)
- ###### bottom commands buttons #########
- bottom_btn_box = Box(app, width="fill", align="bottom")
- exitBtn = PushButton(bottom_btn_box, text="Exit", align="right", command=exit_gui)
- okBtn = PushButton(bottom_btn_box, text="Ok", align="right")
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement