Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Two player version of matching game
- # Kevin Alpin
- import os
- from random import shuffle, randint
- from guizero import App, Box, PushButton, Text, TextBox, info, question, warn, Window
- import pickle
- # global variables
- GRID_SIZE = 4
- score1 = 0
- score2 = 0
- TimeLeft = 60
- emojis_dir = "emojis"
- emojis = []
- buttons = []
- pictures = []
- player = 1
- playing = True
- # Event Handlers
- def imageClick(matched):
- global score1, score2, player
- if matched:
- if player == 1:
- txtTimeLeft1.value = int(txtTimeLeft1.value) + 5
- score1 += 1
- player = 2
- lblInstruct.value = 'Player 2'
- lblInstruct.text_color = 'red'
- lblScore1.value = f"Player 1 Score: {score1}"
- else:
- txtTimeLeft1.value = int(txtTimeLeft1.value) + 5
- score2 += 1
- player = 1
- lblScore2.value = f"Player 2 Score: {score2}"
- lblInstruct.value = 'Player 1'
- lblInstruct.text_color = 'blue'
- setupRound()
- else:
- if player == 1:
- txtTimeLeft1.value = int(txtTimeLeft1.value) - 1
- player = 2
- lblInstruct.value = 'Player 2'
- lblInstruct.text_color = 'red'
- else:
- txtTimeLeft1.value = int(txtTimeLeft1.value) - 1
- player = 1
- lblInstruct.value = 'Player 1'
- lblInstruct.text_color = 'blue'
- if player == 1:
- player1.bg = 'pink'
- player2.bg = 'white'
- else:
- player1.bg = 'white'
- player2.bg = 'pink'
- txtTimeLeft1.startTime = int(txtTimeLeft1.value)
- def btnInfoClicked():
- info('HELP','''There is exactly one pair of matching images. \nClick on one of the two matching images. \nEvery incorrect click loses 1 seconds.\
- \nEvery match scores 1 point and gain 5 seconds of time.\
- \nBE QUICK - If you take more than 10 seconds you lose your turn!''')
- def closeWindow():
- resultsForm.hide()
- app.destroy()
- # Helper Functions
- def setupRound():
- global buttons, pictures, emojis, score1, score2, player
- # load emoji filenames
- 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)
- # for each picture and button in the list assign an emoji to its image feature
- for button in pictures:
- button.image = emojis.pop()
- button.update_command(imageClick, [False])
- for button in buttons:
- button.image = emojis.pop()
- button.update_command(imageClick, [False])
- # Select new image and place in both grids
- newImage = emojis.pop()
- pos = randint(0,len(buttons)-1)
- buttons[pos].image = newImage
- buttons[pos].update_command(imageClick, [True])
- pos = randint(0,len(pictures)-1)
- pictures[pos].image = newImage
- pictures[pos].update_command(imageClick, [True])
- # set time left
- if not playing:
- txtTimeLeft1.value = TimeLeft
- def decreaseTime1():
- global player
- txtTimeLeft1.value = int(txtTimeLeft1.value) - 1
- if int(txtTimeLeft1.value) <= 0:
- app.cancel(decreaseTime1)
- playing = False
- info('Game Over','Game Over')
- endGameRoutine()
- if txtTimeLeft1.startTime - int(txtTimeLeft1.value)>=10: # ran out of time
- app.cancel(decreaseTime1)
- if player == 1:
- player2.bg = 'pink'
- player1.bg = 'white'
- lblInstruct.value = 'Player 2'
- lblInstruct.text_color = 'red'
- player = 2
- else:
- player2.bg = 'white'
- player1.bg = 'pink'
- lblInstruct.value = 'Player 1'
- lblInstruct.text_color = 'blue'
- player = 1
- warn('Time Out', f'You ran out of time. It is now player {player}''s go')
- txtTimeLeft1.startTime = int(txtTimeLeft1.value )
- app.repeat(1000,decreaseTime1)
- def endGameRoutine():
- # get current high scores
- f = open('highscores.txt','rb')
- highscores = pickle.load(f)
- f.close()
- highscores.append([score1,player1.value])
- highscores.append([score2,player2.value])
- highscores.sort(reverse = True)
- # show results
- row = 1
- for item in highscores:
- scorevalue = Text(resultsForm,text = highscores[row-1][0], width = 20,grid = [0,row])
- namevalue = Text(resultsForm,text = highscores[row-1][1], width = 20, grid = [1,row])
- row += 1
- spacer = Text(resultsForm, text = "", grid = [0,row,2,1])
- closeBtn = PushButton(resultsForm,text='CLOSE', width = 25, grid = [1,row,2,1],command = closeWindow)
- # resave high scores
- f = open('highscores.txt','wb')
- pickle.dump(highscores,f)
- f.close()
- # show form
- resultsForm.show(wait=True)
- # .......................................................................................
- # setup the app
- app = App("emoji match", width = GRID_SIZE*200, height = 150+GRID_SIZE*100, bg = 'white')
- # create a box for information
- spaceBox = Box(app,width = 'fill', height = 20)
- infoBox = Box(app, width = 'fill', align = 'top', height = 30)
- infoBox.bg = 'blue'
- lblInstructions = PushButton(infoBox, command = btnInfoClicked, text = 'Information', width = 20, height = 'fill')
- lblInstructions.bg = 'red'
- lblInstructions.text_color = 'white'
- spaceBox = Box(infoBox,width = 'fill', height = 20)
- # player name box
- playerBox = Box(app, width = 'fill',height = 40)
- playerBox.bg = 'light yellow'
- player1 = TextBox(playerBox,text = 'Player 1',width = 30, align = 'left')
- player1.text_size = 14
- player1.bg = 'pink'
- player2 = TextBox(playerBox,text = 'Player 2',width = 30, align = 'right')
- player2.text_size = 14
- # create a box to house the grids
- gameBox = Box(app)
- space = Box(gameBox, width = "fill", height = 60, align='top')
- lblInstruct = Text(space, text = 'Player 1: Your turn',height = 3)
- # create a box to house the pictures
- picturesBox = Box(gameBox, layout="grid",align='left')
- # create a box to house the buttons
- buttonsBox = Box(gameBox, layout="grid",align='left')
- # space
- spaceBox = Box(app,width = 'fill', height = 20)
- #create a box for feedback player 1
- resultBox1 = Box(app, width = "fill", height = 50,align = 'top')
- resultBox1.bg = 'light blue'
- lblTime1 = Text(resultBox1, text='Time Left:',align = 'left', height = '2', width = 10)
- txtTimeLeft1 = TextBox(resultBox1, text = '', align = 'left', height = '2')
- txtTimeLeft1.text_size = 12
- lblScore1 = Text(resultBox1, text = '', width = '30', height = 'fill', color = 'blue')
- lblScore1.value = f" Player 1 Score: {score1}"
- lblScore2 = Text(resultBox1, text = '', width = '30', height = 'fill', color = 'red')
- lblScore2.value = f" Player 2 Score: {score2}"
- # create PushButtons with a different grid coordinate and add to the list
- for x in range(0,GRID_SIZE):
- for y in range(0,GRID_SIZE):
- # put the pictures and buttons into the lists
- button = PushButton(picturesBox, grid=[x,y])
- pictures.append(button)
- button = PushButton(buttonsBox, grid=[x,y])
- buttons.append(button)
- # set up results window
- resultsForm = Window(app,title = 'Highscores', bg = 'bisque',layout = 'grid')
- lblScore = Text(resultsForm,text = 'Score', align = 'left', width = 20, grid = [0,0])
- lblName = Text(resultsForm,text = 'Player', align = 'left', width = 20, grid = [1,0])
- resultsForm.hide()
- # call round setup
- setupRound()
- name1 = question('Enter Name', 'Player 1: Enter your name')
- name2 = question('Enter Name', 'Player 2: Enter your name')
- player1.value = name1
- player2.value = name2
- playing = True
- txtTimeLeft1.value = TimeLeft
- txtTimeLeft1.startTime = int(txtTimeLeft1.value)
- app.repeat(1000,decreaseTime1)
- # Display app
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement