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 *
- from guizero import Text
- class game_infos():
- rounds_played = "0"
- game_score = 0
- correct_on_a_row = 0
- game_timer = 20
- 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"
- game_infos.game_timer = "20"
- game_infos.game_score = game_infos.game_score + 1
- game_infos.correct_on_a_row = game_infos.correct_on_a_row + 1
- score_text.value = game_infos.game_score
- if 0:# condition for bonus point => game_infos.correct_on_a_row >= 3:
- game_infos.game_score = game_infos.game_score + 1
- score_text.value = game_infos.game_score
- game_infos.correct_on_a_row = 0
- info("bonus","bonus on")
- if game_infos.correct_on_a_row >= 3:# condition for bonus time
- game_infos.game_timer = game_infos.game_timer + 5
- game_infos.correct_on_a_row = 0
- info("bonus", "bonus on")
- else:
- result.value = "incorrect"
- game_infos.game_timer = 20
- game_infos.game_score = game_infos.game_score - 1
- game_infos.correct_on_a_row = 0
- score_text.value = game_infos.game_score
- 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 = game_infos.game_timer
- timerTemp = int(game_infos.game_timer) - 1
- game_infos.game_timer = timerTemp
- if game_infos.game_timer == 0:
- timer.cancel(counter)
- # reset the timer
- result.value = "Game Over"
- warn("Time Out", "you've run out of time")
- # reset timer
- game_infos.timer = 20
- # reset result
- result.value = ""
- # start new round
- setup_round()
- # restart timer
- timer.repeat(1000, counter)
- print("im here")
- ###################### Begin of the App code ######################
- app = App("Emoji Match", height=700)
- game_played = Text(app, text="Rounds Played = " + str(game_infos.rounds_played))
- result: Text = Text(app)
- score_board_box = Box(app)
- score_lbl = Text(score_board_box, text="score", align="left")
- score_text = Text(score_board_box, text=game_infos.game_score, align="left")
- # add in the extra features
- extra_features = Box(app)
- timer_lable = Text(extra_features, text="Timer: ",align="left")
- timer = Text(extra_features, text="Get Ready",align="left")
- # 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.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