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
- players_turn = Text(app, text = "Go P1", align = "top")
- result = Text(app, align = "top")
- time = Text(app, text = 10, align = "top")
- #player1 score, round and streak counter
- score1 = Text(app, text = 0, align = "top")
- rounds1 = Text(app, text = 0, align = "bottom")
- streak1 = Text(app, text = 0, visible = False)
- #player2 score, round and streak counter
- score2 = Text(app, text = 0, align = "top")
- rounds2 = Text(app, text = 0, align = "bottom")
- streak2 = Text(app, text = 0, visible = False)
- #player_turn counter
- player_turn = 0
- #display whether used choice matched
- def match_emoji(matched, turn):
- round1_time = 30
- round2_time = 30
- if matched:
- #if matched, say so and increase socre and streak by 1
- result.value = "YES!"
- #update scores if player 1 turn
- if turn == 0:
- score1.value = int(score1.value) + 1
- streak1.value = int(streak1.value) + 1
- rounds1.value = int(rounds1.value) + 1
- #if the user has streak of 3, award bonus point and 10s time
- if int(streak1.value) % 3 == 0:
- score1.value = int(score1.value) + 1
- round1_time += 10
- #if player 2 turn, update scores
- if turn == 1:
- score2.value = int(score2.value) + 1
- streak2.value = int(streak2.value) + 1
- rounds2.value = int(rounds2.value) + 1
- # if the user has a streak of 3, award bonus point and time
- if int(streak2.value) % 3 == 0:
- score2.value = int(score2.value) + 1
- round2_time += 10
- #start next round
- global player_turn
- if turn == 0:
- player_turn += 1
- players_turn.value = "Go P2"
- setup_game(round2_time, player_turn)
- elif turn == 1:
- player_turn += 1
- players_turn.value = "Go P1"
- setup_game(round1_time, player_turn)
- #if incorrect, say so and decrease score by 1; set streak to 0
- else:
- result.value = "NO!"
- if turn == 0:
- score1.value = int(score1.value) - 1
- streak1.value = 0
- elif turn == 1:
- score2.value = int(score2.value) - 1
- streak2.value = 0
- # 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 to display emojis
- 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(length, player_turn):
- #set time to 30 seconds
- time.value = length
- #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, (player_turn%2)])
- else:
- #insert a random emoji
- button.image = emojis.pop()
- button.update_command(match_emoji, [False, (player_turn%2)])
- #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 = "END", text = "Score 1: " + score1.value + ", Score 2: " + score2.value)
- quit_game()
- #display rules
- def game_rules():
- warn(title = "Rules", text = "Choose an Emoji Match before time runs out! Switch player after each correct choice!")
- #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")
- #create a button to display rules
- rules_btn = PushButton(app, command = game_rules, text = "Rules", align = "bottom")
- #setup first round of emojis and quit button to black
- setup_game(30, player_turn)
- button_colour_switch()
- #start app
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement