Advertisement
davidhellam

Python: Emoji Timer

Aug 17th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.03 KB | None | 0 0
  1. import os
  2. from random import shuffle,randrange
  3. from guizero import App, Box, Picture, Text, info, PushButton
  4.  
  5. # set the path to the emoji folder on your computer
  6. emojis_dir = "emojis"
  7. # create a list of the locations of the emoji images
  8. emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  9. # shuffle the emojis
  10.  
  11. blank = "25fb.png"
  12. start = "25b6.png"
  13. alert = "26a0.png"
  14. correct = "2705.png"
  15.  
  16. gameOn = False
  17.  
  18. app = App("Emoji Match Game",width=720, height=480)
  19.  
  20. def doTime():
  21.     if (int(timeleft.value)>0 and feedback.value != "Match Found!"):
  22.         timeleft.value=str(int(timeleft.value)-1)
  23.     else:
  24.         timeleft.value=str(int(timeleft.value)-0)
  25.        
  26. def initialicon():
  27.     leftset=[]
  28.     rightset=[]
  29.  
  30.     shuffle(emojis)
  31.  
  32.     for count in range(0,9):
  33.         leftset.append(emojis[count])
  34.         rightset.append(emojis[count+8])
  35.  
  36.     shuffle(leftset)
  37.     shuffle(rightset)
  38.  
  39.     myemojis = []
  40.  
  41.     for count in range(0,9):
  42.         myemojis.append(leftset[count])
  43.  
  44.     myemojis.append(blank)
  45.     myemojis.append(blank)
  46.  
  47.     for count in range(0,9):
  48.         myemojis.append(rightset[count])
  49.  
  50.     return(myemojis)
  51.  
  52. def doStart():
  53.     if int(roundno.value)<10:
  54.         myemojis = initialicon()
  55.         for count in range(0,20):
  56.             pictures[count].image = myemojis[count]
  57.         feedback.value=""
  58.         roundno.value=str(int(roundno.value)+1)
  59.         timeleft.value=str(int(30))
  60.         startbutton.hide()
  61.        
  62.    
  63.    
  64.  
  65. def testmatch():
  66.     if (pictures[9].image == pictures[10].image):
  67.         #info("Match","Match Found!")
  68.         feedback.value= "Match Found!"
  69.         emojis.remove(pictures[9].value)
  70.         if (int(roundno.value)%2==1):
  71.             P1_score.value=str(int(P1_score.value)+10+int(timeleft.value))
  72.             startbutton.bg=(127,255,255)
  73.             startbutton.text="Player 2 Start"
  74.         else:
  75.             P2_score.value=str(int(P2_score.value)+10+int(timeleft.value))
  76.             startbutton.bg=(255,127,255)
  77.             startbutton.text="Player 1 Start"
  78.         if (int(roundno.value) == 10):
  79.             if (int(P1_score.value)>int(P2_score.value)):
  80.                 winner = "winner is Player 1"
  81.             elif (int(P1_score.value)<int(P2_score.value)):
  82.                 winner = "winner is Player 2"
  83.             else:
  84.                 winner = "result is a draw"
  85.             info("The End","Well done, the "+ winner)
  86.         else:
  87.             startbutton.show()
  88.     else:
  89.         if((pictures[9].image != blank) and (pictures[10].image != blank)):
  90.             feedback.value="Not a Match!"
  91.             if (int(roundno.value)%2==1):
  92.                 P1_score.value=str(int(P1_score.value)-1)
  93.             else:
  94.                 P2_score.value=str(int(P2_score.value)-1)
  95.    
  96.  
  97. def leftselected(event_data):
  98.     pic_clicked = event_data.widget
  99.     pictures[9].image = pic_clicked.image
  100.     testmatch()
  101.  
  102. def rightselected(event_data):
  103.     pic_clicked = event_data.widget
  104.     pictures[10].image = pic_clicked.image
  105.     testmatch()
  106.  
  107. myemojis = initialicon()
  108.  
  109. # create a box to house the grid
  110. pictures_box = Box(app, layout="grid")
  111.  
  112. header=Text(pictures_box, text="Emoji Match Game", size=28, grid=[0,0,8,1])
  113.  
  114. # create an empty list to which pictures will be added
  115. pictures = []
  116. for x in range(0,3):
  117.     for y in range(1,4):
  118.         # put the pictures into the list
  119.         picture = Picture(pictures_box, grid=[x,y])
  120.         pictures.append(picture)
  121.  
  122. picture = Picture(pictures_box, grid=[3,4])
  123. pictures.append(picture)
  124. picture = Picture(pictures_box, grid=[4,4])
  125. pictures.append(picture)
  126.  
  127. for x in range(5,8):
  128.     for y in range(1,4):
  129.         # put the pictures into the list
  130.         picture = Picture(pictures_box, grid=[x,y])
  131.         pictures.append(picture)
  132. # for each picture in the list
  133. counter = 0
  134.  
  135. for picture in pictures:
  136.     # make the picture a random emoji
  137.     pictures[counter].image = blank
  138.     counter=counter+1
  139.  
  140. P1_label=Text(pictures_box, text="Player 1\nScore:", size=10, align="right",grid=[0,4])
  141. P1_score=Text(pictures_box, text="0", size=20, align="left", grid=[1,4])
  142. P2_label=Text(pictures_box, text="Player 2\nScore:", size=10, align="right",grid=[6,4])
  143. P2_score=Text(pictures_box, text="0", size=20, align="left", grid=[7,4])
  144.  
  145. time_label=Text(pictures_box, text="Points:", size=8,align="right",grid=[3,1])
  146. timeleft=Text(pictures_box, text="30", size=8,align="left",grid=[4,1])
  147. round_label=Text(pictures_box, text="Round:", align="right",size=8,grid=[3,3])
  148. roundno=Text(pictures_box, text="0", size=8,align="left",grid=[4,3])
  149.  
  150. feedback=Text(pictures_box, text="",size=14, align="bottom" , grid=[3,5,2,1])
  151.  
  152. startbutton = PushButton(pictures_box, text="Player 1 Start", grid=[3,2,2,1])
  153. startbutton.bg=(255,128,255)
  154. startbutton.when_clicked = doStart
  155.  
  156. for count in range(0,9):
  157.     pictures[count].when_clicked = leftselected
  158.  
  159. for count in range(11,20):
  160.     pictures[count].when_clicked = rightselected
  161.  
  162. timeleft.repeat(1000,doTime)
  163.  
  164. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement