Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from random import shuffle,randrange
- from guizero import App, Box, Picture, Text, info, PushButton, yesno, Window
- # 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
- blank = "25fb.png"
- start = "25b6.png"
- alert = "26a0.png"
- correct = "2705.png"
- usedemojis = []
- gameOn = False
- app = App("Emoji Match Game",width=720, height=420)
- #initialise other windows for game
- helpwindow = Window(app, title="Help Screen", width=600, height=400)
- hswindow = Window(app, title="High Scores", width=600, height=400)
- addHSwindow = Window(app, title="Add a new high score", width=600, height=400)
- #functions for adding high scores
- def closeAddHS():
- addHSwindow.hide()
- def newHighScore(topscore):
- ahs_score.value = topscore
- openAddHS()
- def openAddHS():
- #makes the add high score window main focus until new score added
- addHSwindow.show(wait=True)
- def addKey(event_data):
- #adds 3 letters for each high score recorded
- key_pressed = event_data.widget
- chars=[]
- for count in range(0,3):
- chars.append(ahs_name.value[count])
- chars[ahs_name.pointer]=key_pressed.text
- ahs_name.value=chars[0]+chars[1]+chars[2]
- ahs_name.pointer=(ahs_name.pointer+1)%3
- def addScore():
- #appends high score to textfile
- with open("hiscores.txt", "a") as f:
- f.write(ahs_name.value+str(ahs_score.value)+"\n")
- f.close()
- addHSwindow.hide()
- #functions for high score screen
- def closeHS():
- hswindow.hide()
- def openHS():
- read_scores()
- hswindow.show()
- def read_scores():
- #reads and separates high scores and names
- names = []
- scores = []
- with open("hiscores.txt", "r") as f:
- allscores = f.readlines()
- for count in range(len(allscores)):
- allscores[count].replace("\n","")
- names.append(allscores[count][0:3])
- scores.append(int(allscores[count][3:]))
- #does a bubble sort - bit of overkill really as only newest high scores are recorded
- sorted_data = []
- for i in range(len(scores)):
- sorted_data.append(scores[i])
- done_sorting = False
- k = 0
- swaps = 0
- while not done_sorting:
- done_sorting = True
- k = k+1
- for j in range(len(sorted_data)-k):
- if (sorted_data[j]>sorted_data[j+1]):
- temp = sorted_data[j]
- sorted_data[j] = sorted_data[j+1]
- sorted_data[j+1] = temp
- done_sorting = False
- swaps = swaps + 1
- leader_score=[]
- leader_label=[]
- for count in range (len(sorted_data)-3,len(sorted_data)):
- leader_score.append(sorted_data[count])
- for count2 in range (len(scores)):
- if scores[count2] == sorted_data[count]:
- leader_label.append(names[count2])
- #displays three highest scores
- hs_label1.value = leader_label[2]
- hs_label2.value = leader_label[1]
- hs_label3.value = leader_label[0]
- hs_score1.value = leader_score[2]
- hs_score2.value = leader_score[1]
- hs_score3.value = leader_score[0]
- #functions for help screen
- def closeHelp():
- helpwindow.hide()
- def openHelp():
- helpwindow.show()
- #re-initialise scores and emoji for a new game
- def startAgain():
- roundno.value="0"
- P1_score.value = "0"
- P2_score.value = "0"
- for i in range (len(usedemojis)):
- emojis.append(usedemojis.pop())
- startbutton.show()
- #counts down time
- def doTime():
- if (int(timeleft.value)>0 and feedback.value != "Match Found!" and timeleft.timeout==False):
- timeleft.value=str(int(timeleft.value)-1)
- else:
- timeleft.value=str(int(timeleft.value)-0)
- #what happens when out of time
- if (int(timeleft.value)==0 and timeleft.timeout == False):
- timeleft.timeout=True
- extratime = yesno("Time Over","No time left - would you like to continue?")
- if extratime:
- timeleft.hide()
- else:
- startbutton.show()
- #sets up the emoji for game
- def initialicon():
- leftset=[]
- rightset=[]
- shuffle(emojis)
- for count in range(0,9):
- leftset.append(emojis[count])
- rightset.append(emojis[count+8])
- shuffle(leftset)
- shuffle(rightset)
- myemojis = []
- for count in range(0,9):
- myemojis.append(leftset[count])
- myemojis.append(blank)
- myemojis.append(blank)
- for count in range(0,9):
- myemojis.append(rightset[count])
- return(myemojis)
- #when start button is pressed
- def doStart():
- if int(roundno.value)<10:
- myemojis = initialicon()
- for count in range(0,20):
- pictures[count].image = myemojis[count]
- feedback.value=""
- roundno.value=str(int(roundno.value)+1)
- timeleft.value=str(int(30))
- P1_score.bonus = 10
- P2_score.bonus = 10
- startbutton.hide()
- timeleft.show()
- timeleft.timeout=False
- #testing for a match
- def testmatch():
- if (pictures[9].image == pictures[10].image):
- feedback.value= "Match Found!"
- timeleft.hide()
- #removes matched emoji from current list
- usedemojis.append(pictures[9].value)
- emojis.remove(pictures[9].value)
- #adding to score and setting up for next round
- if (int(roundno.value)%2==1):
- P1_score.value=str(int(P1_score.value)+int(P1_score.bonus)+int(timeleft.value))
- startbutton.bg=(127,255,255)
- startbutton.text="Player 2 Start"
- else:
- P2_score.value=str(int(P2_score.value)+int(P2_score.bonus)+int(timeleft.value))
- startbutton.bg=(255,127,255)
- startbutton.text="Player 1 Start"
- #testing for end of game
- if (int(roundno.value) == 10):
- if (int(P1_score.value)>int(P2_score.value)):
- winner = "winner is Player 1"
- topscore = int(P1_score.value)
- elif (int(P1_score.value)<int(P2_score.value)):
- winner = "winner is Player 2"
- topscore = int(P2_score.value)
- else:
- winner = "result is a draw"
- topscore = int(P1_score.value)
- info("The End","Well done, the "+ winner)
- read_scores()
- if (int(topscore) > int(hs_score1.value)):
- newHighScore(topscore)
- anothergame = yesno("Another Game?","Would you like to play another game?")
- if anothergame:
- startAgain()
- else:
- #on to the next round
- startbutton.show()
- else:
- #what happens if they don't match?
- if((pictures[9].image != blank) and (pictures[10].image != blank)):
- feedback.value="Not a Match!"
- if (int(roundno.value)%2==1):
- P1_score.bonus=int(P1_score.bonus)-1
- else:
- P2_score.bonus=int(P2_score.bonus)-1
- # clicking on the left and right hand grids
- def leftselected(event_data):
- pic_clicked = event_data.widget
- pictures[9].image = pic_clicked.image
- testmatch()
- def rightselected(event_data):
- pic_clicked = event_data.widget
- pictures[10].image = pic_clicked.image
- testmatch()
- # call to set up the first two grids of emojis
- myemojis = initialicon()
- # create the help window
- helpwindow.hide()
- help_box=Box(helpwindow, width= 600, height=400, layout="grid")
- help_title=Text(help_box, text="Emoji Game Help", size=24, grid = [0,0,3,1])
- help_body1=Text(help_box, text="This is a two-player game.\nEach player takes turns to find matching emoji.", grid = [0,1,3,1])
- help_body2=Text(help_box, text="You see two grids of nine randomly arranged emoji.\nOnly one emoji will be the same in both grids.", grid = [0,2,3,1])
- help_body3=Text(help_box, text="You have 30 seconds to locate and click on the two emoji which are the same.", grid = [0,3,3,1])
- help_body4=Text(help_box, text="If you don\'t make any mistakes, you get a bonus of 10 points.", grid = [0,4,3,1])
- help_body5=Text(help_box, text="You take it in turns to play five rounds each.\nYour score for each round will be added to a cumulative total.", grid = [0,5,3,1])
- help_body6=Text(help_box, text="The player with the highest cumulative total wins.", grid = [0,6,3,1])
- help_close=PushButton(help_box, text="Close Help", align="right" ,grid=[2,7,1,1])
- #help window functions
- help_close.when_clicked=closeHelp
- #create High Score display
- hswindow.hide()
- hs_box=Box(hswindow, width= 600, height=400, layout="grid")
- hs_title=Text(hs_box, text="Emoji Game High Scores", size=24, grid = [0,0,3,1])
- hs_label1=Text(hs_box, text="---", size=18, grid=[0,1,1,1])
- hs_label2=Text(hs_box, text="---", size=18, grid=[0,2,1,1])
- hs_label3=Text(hs_box, text="---", size=18, grid=[0,3,1,1])
- hs_score1=Text(hs_box, text="0", size=18, grid=[1,1,1,1])
- hs_score2=Text(hs_box, text="0", size=18, grid=[1,2,1,1])
- hs_score3=Text(hs_box, text="0", size=18, grid=[1,3,1,1])
- hs_close=PushButton(hs_box, text="Close\nHigh Score\nDisplay", align="right" ,grid=[2,4,1,1])
- #high score display window functions
- hs_close.when_clicked=closeHS
- #create form for adding new high score
- addHSwindow.hide()
- ahs_box=Box(addHSwindow, width= 600, height=400, layout="grid")
- ahs_title=Text(ahs_box, text="Emoji Game : New High Score", size=24, grid = [0,0,15,1])
- ahs_label=Text(ahs_box, text="Name: ", size=18, grid=[0,1,3,1])
- ahs_name = Text(ahs_box,text="***",size=18,grid=[3,1,3,1])
- ahs_score_label= Text(ahs_box,text="Score: ",size=18,grid=[6,1,3,1])
- ahs_score= Text(ahs_box,text="0",size=18,grid=[9,1,3,1])
- ahs_info1=Text(ahs_box,text="We have a new High Score!\nClick on three keys for your name.",size=8,grid=[0,2,15,1])
- ahs_info2=Text(ahs_box,text="Click on the [ENTER] button when finished.",size=8,grid=[0,3,15,1])
- ahs_key=[]
- for x in range(0,13):
- for y in range (4,6):
- counter=x+(y-4)*13
- character=chr(65+counter)
- key=PushButton(ahs_box,text=character,grid=[x,y])
- ahs_key.append(key)
- key=PushButton(ahs_box,text='.',grid=[13,4])
- ahs_key.append(key)
- key=PushButton(ahs_box,text='-',grid=[14,4])
- ahs_key.append(key)
- enterkey=PushButton(ahs_box,text="Enter",grid=[13,5,2,1])
- ahs_name.pointer=0
- for count in range(0,28):
- ahs_key[count].when_clicked=addKey
- enterkey.when_clicked=addScore
- # create a box to house the grid
- pictures_box = Box(app, layout="grid")
- header=Text(pictures_box, text="Emoji Match Game", size=28, grid=[0,0,8,1])
- # create an empty list to which pictures will be added
- pictures = []
- for x in range(0,3):
- for y in range(1,4):
- # put the pictures into the list
- picture = Picture(pictures_box, grid=[x,y])
- pictures.append(picture)
- #these are the two images which display which emoji have been clicked on
- picture = Picture(pictures_box, grid=[3,4])
- pictures.append(picture)
- picture = Picture(pictures_box, grid=[4,4])
- pictures.append(picture)
- for x in range(5,8):
- for y in range(1,4):
- # put the pictures into the list
- picture = Picture(pictures_box, grid=[x,y])
- pictures.append(picture)
- # for each picture in the list
- counter = 0
- for picture in pictures:
- # sets all 18 images in the grids and the two status images to blank
- pictures[counter].image = blank
- counter=counter+1
- #display score
- P1_label=Text(pictures_box, text="Player 1\nScore:", size=10, align="right",grid=[0,4])
- P1_score=Text(pictures_box, text="0", size=20, align="left", grid=[1,4])
- P2_label=Text(pictures_box, text="Player 2\nScore:", size=10, align="right",grid=[6,4])
- P2_score=Text(pictures_box, text="0", size=20, align="left", grid=[7,4])
- #display tiemr and round
- time_label=Text(pictures_box, text="Time:", size=8,align="right",grid=[3,1])
- timeleft=Text(pictures_box, text="30", size=8,align="left",grid=[4,1])
- round_label=Text(pictures_box, text="Round:", align="right",size=8,grid=[3,3])
- roundno=Text(pictures_box, text="0", size=8,align="left",grid=[4,3])
- #help screen button
- helpbutton=PushButton(pictures_box,text="Help", grid=[7,5])
- helpbutton.when_clicked=openHelp
- # high score display button
- hsbutton=PushButton(pictures_box,text="High\nScore", grid=[0,5])
- hsbutton.when_clicked=openHS
- # display feedback for matching
- feedback=Text(pictures_box, text="",size=14, align="bottom" , grid=[3,5,2,1])
- #start button initialised for Player 1
- startbutton = PushButton(pictures_box, text="Player 1 Start", grid=[3,2,2,1])
- startbutton.bg=(255,128,255)
- startbutton.when_clicked = doStart
- # set up emoji as buttons
- for count in range(0,9):
- pictures[count].when_clicked = leftselected
- for count in range(11,20):
- pictures[count].when_clicked = rightselected
- #timer functions
- timeleft.timeout=True
- timeleft.hide()
- timeleft.repeat(1000,doTime)
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement