Advertisement
davidhellam

Python: Emoji Gameplay 3

Aug 16th, 2019
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.68 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 initialicon():
  21.     leftset=[]
  22.     rightset=[]
  23.  
  24.     shuffle(emojis)
  25.  
  26.     for count in range(0,9):
  27.         leftset.append(emojis[count])
  28.         rightset.append(emojis[count+8])
  29.  
  30.     shuffle(leftset)
  31.     shuffle(rightset)
  32.  
  33.     myemojis = []
  34.  
  35.     for count in range(0,9):
  36.         myemojis.append(leftset[count])
  37.  
  38.     myemojis.append(blank)
  39.     myemojis.append(blank)
  40.  
  41.     for count in range(0,9):
  42.         myemojis.append(rightset[count])
  43.  
  44.     return(myemojis)
  45.  
  46. def doStart():
  47.     if int(roundno.value)<10:
  48.         myemojis = initialicon()
  49.         for count in range(0,20):
  50.             pictures[count].image = myemojis[count]
  51.         feedback.value=""
  52.         roundno.value=str(int(roundno.value)+1)
  53.     else:
  54.         info("The End","Well done, 10 rounds completed!")
  55.     startbutton.hide()
  56.    
  57.  
  58. def testmatch():
  59.     if (pictures[9].image == pictures[10].image):
  60.         #info("Match","Match Found!")
  61.         feedback.value= "Match Found!"
  62.         emojis.remove(pictures[9].value)
  63.         startbutton.show()
  64.     else:
  65.         if((pictures[9].image != blank) and (pictures[10].image != blank)):
  66.             feedback.value="Not a Match!"
  67.  
  68. def leftselected(event_data):
  69.     pic_clicked = event_data.widget
  70.     pictures[9].image = pic_clicked.image
  71.     testmatch()
  72.  
  73. def rightselected(event_data):
  74.     pic_clicked = event_data.widget
  75.     pictures[10].image = pic_clicked.image
  76.     testmatch()
  77.  
  78. myemojis = initialicon()
  79.  
  80. # create a box to house the grid
  81. pictures_box = Box(app, layout="grid")
  82.  
  83. header=Text(pictures_box, text="Emoji Match Game", size=28, grid=[0,0,8,1])
  84.  
  85. # create an empty list to which pictures will be added
  86. pictures = []
  87. for x in range(0,3):
  88.     for y in range(1,4):
  89.         # put the pictures into the list
  90.         picture = Picture(pictures_box, grid=[x,y])
  91.         pictures.append(picture)
  92.  
  93. picture = Picture(pictures_box, grid=[3,4])
  94. pictures.append(picture)
  95. picture = Picture(pictures_box, grid=[4,4])
  96. pictures.append(picture)
  97.  
  98. for x in range(5,8):
  99.     for y in range(1,4):
  100.         # put the pictures into the list
  101.         picture = Picture(pictures_box, grid=[x,y])
  102.         pictures.append(picture)
  103. # for each picture in the list
  104. counter = 0
  105.  
  106. for picture in pictures:
  107.     # make the picture a random emoji
  108.     pictures[counter].image = blank
  109.     counter=counter+1
  110.  
  111. P1_label=Text(pictures_box, text="Player 1\nScore:", size=10, align="right",grid=[0,4])
  112. P1_score=Text(pictures_box, text="xxxx", size=20, align="left", grid=[1,4])
  113. P2_label=Text(pictures_box, text="Player 2\nScore:", size=10, align="right",grid=[6,4])
  114. P2_score=Text(pictures_box, text="xxxx", size=20, align="left", grid=[7,4])
  115. round_label=Text(pictures_box, text="Round:", size=10,align="right",grid=[3,1])
  116. roundno=Text(pictures_box, text="0", size=10,align="left",grid=[4,1])
  117.  
  118. feedback=Text(pictures_box, text="",size=14, align="bottom" , grid=[3,3,2,1])
  119.  
  120. startbutton = PushButton(pictures_box, text="start", grid=[3,2,2,1])
  121. startbutton.when_clicked = doStart
  122.  
  123. for count in range(0,9):
  124.     pictures[count].when_clicked = leftselected
  125.  
  126. for count in range(11,20):
  127.     pictures[count].when_clicked = rightselected
  128.  
  129. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement