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
- # 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"
- gameOn = False
- app = App("Emoji Match Game",width=720, height=480)
- def doTime():
- if (int(timeleft.value)>0 and feedback.value != "Match Found!"):
- timeleft.value=str(int(timeleft.value)-1)
- else:
- timeleft.value=str(int(timeleft.value)-0)
- 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)
- 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))
- startbutton.hide()
- def testmatch():
- if (pictures[9].image == pictures[10].image):
- #info("Match","Match Found!")
- feedback.value= "Match Found!"
- emojis.remove(pictures[9].value)
- if (int(roundno.value)%2==1):
- P1_score.value=str(int(P1_score.value)+10+int(timeleft.value))
- startbutton.bg=(127,255,255)
- startbutton.text="Player 2 Start"
- else:
- P2_score.value=str(int(P2_score.value)+10+int(timeleft.value))
- startbutton.bg=(255,127,255)
- startbutton.text="Player 1 Start"
- if (int(roundno.value) == 10):
- if (int(P1_score.value)>int(P2_score.value)):
- winner = "winner is Player 1"
- elif (int(P1_score.value)<int(P2_score.value)):
- winner = "winner is Player 2"
- else:
- winner = "result is a draw"
- info("The End","Well done, the "+ winner)
- else:
- startbutton.show()
- else:
- if((pictures[9].image != blank) and (pictures[10].image != blank)):
- feedback.value="Not a Match!"
- if (int(roundno.value)%2==1):
- P1_score.value=str(int(P1_score.value)-1)
- else:
- P2_score.value=str(int(P2_score.value)-1)
- 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()
- myemojis = initialicon()
- # 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)
- 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:
- # make the picture a random emoji
- pictures[counter].image = blank
- counter=counter+1
- 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])
- time_label=Text(pictures_box, text="Points:", 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])
- feedback=Text(pictures_box, text="",size=14, align="bottom" , grid=[3,5,2,1])
- startbutton = PushButton(pictures_box, text="Player 1 Start", grid=[3,2,2,1])
- startbutton.bg=(255,128,255)
- startbutton.when_clicked = doStart
- for count in range(0,9):
- pictures[count].when_clicked = leftselected
- for count in range(11,20):
- pictures[count].when_clicked = rightselected
- timeleft.repeat(1000,doTime)
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement