Advertisement
timber101

Untitled

Sep 3rd, 2019
250
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.33 KB | None
  1. import os
  2. from random import shuffle, randint
  3. from guizero import App, Box, Picture, PushButton, Text, warn, info, Window, ButtonGroup
  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. shuffle(emojis)
  11.  
  12. def match_emoji(matched):
  13.     if matched:
  14.         result.value = "correct"
  15.         if player.value == "Player 1":
  16.             P1score.value = int(P1score.value)+1
  17.             P1rscore.value = int(P1rscore.value)+1
  18.             ##score.value = int(score.value) + 1
  19.             ##rscore.value = int(rscore.value) + 1
  20.             #tiar.value = tiar.value + "X"
  21.             #if tiar.value == "XXX":
  22.             #    timer.value = int(timer.value) +5
  23.             #    tiar.value = ""
  24.         else:
  25.             P2score.value = int(P2score.value)+1
  26.             P2rscore.value = int(P2rscore.value)+1
  27.  
  28.     else:
  29.         result.value = "incorrect"
  30.         #unrscore.value = int(unrscore.value) + 1
  31.         #tiar.value = ""
  32.        
  33.     setup_round()
  34.  
  35. def setup_round():
  36.     # for each picture and button in the list assign an emoji to its image feature
  37.     for picture in pictures:
  38.         # make the picture a random emoji
  39.         picture.image = emojis.pop()
  40.  
  41.     for button in buttons:
  42.         # make the image feature of the PushButton a random emoji
  43.         button.image = emojis.pop()
  44.         # set the command to be called and pass False, as these emoji wont be the matching ones
  45.         button.update_command(match_emoji, [False])
  46.  
  47.     # choose a new emoji
  48.     matched_emoji = emojis.pop()
  49.  
  50.     # select a number at random
  51.     random_picture = randint(0,8)
  52.     # change the image feature of the Picture with this index in the list of pictures to the new emoji
  53.     pictures[random_picture].image = matched_emoji
  54.  
  55.     random_button = randint(0,8)
  56.     # change the image feature of the PushButton with this index in the list of buttons to the new emoji
  57.     buttons[random_button].image = matched_emoji
  58.     # set the command to be called and pass True, as this is the matching emoji
  59.     buttons[random_button].update_command(match_emoji, [True])
  60.  
  61. def counter():
  62.     timer.value = int(timer.value) - 1
  63.     if int(timer.value) == 0:
  64.         timer.cancel(counter)
  65.         # reset the timer
  66.         result.value = "Game Over"
  67.         warn("Time Out", "you've run out of time final score:- " + rscore.value)
  68.         # reset timer
  69.         timer.value = "40"
  70.         # reset result
  71.         result.value = ""
  72.         # start new round
  73.         setup_round()
  74.         #restart timer
  75.         timer.repeat(1000, counter)
  76.         #update round number
  77.         round.value = int(round.value)+1
  78.         #reset scores to 0
  79.         P1score.value = 0
  80.         P1rscoreunscore.value = 0
  81.         P2score.value = 0
  82.         P2rscoreunscore.value = 0
  83.  
  84.  
  85. def open_inst_window():
  86.     instwindow.show()
  87.  
  88. def close_inst_window():
  89.     instwindow.hide()    
  90.  
  91. # setup the app
  92. app = App("emoji match", height=700)
  93.  
  94. result = Text(app)
  95. player = ButtonGroup(app,options=["Player 1", "Player 2"], selected = "Player 1", horizontal =True)
  96.  
  97.  
  98. # create a box to house the grids
  99. game_box = Box(app)
  100. # create a box to house the pictures
  101. pictures_box = Box(game_box, layout="grid")
  102. # create a box to house the buttons
  103. buttons_box = Box(game_box, layout="grid")
  104.  
  105. # create the an empty lists to add the buttons and pictures to
  106. buttons = []
  107. pictures = []
  108. # create 9 PushButtons with a different grid coordinate and add to the list
  109. for x in range(0,3):
  110.     for y in range(0,3):
  111.         # put the pictures and buttons into the lists
  112.         picture = Picture(pictures_box, grid=[x,y])
  113.         pictures.append(picture)
  114.        
  115.         button = PushButton(buttons_box, grid=[x,y])
  116.         buttons.append(button)
  117.  
  118.  
  119. # add in the extra features
  120. extra_features = Box(app)
  121. timer = Text(extra_features, text="Get Ready")
  122.  
  123. roundboard = Box(app)
  124. rolabel = Text(roundboard, text = "Round", align="left")
  125. round = Text(roundboard, text="round")
  126.  
  127. P1scoreboard = Box(app)
  128. P1sclab = Text(P1scoreboard, text="P1 Round Score", align="left")
  129. P1score = Text(P1scoreboard, text="0", align="left")
  130. P1rsclabel = Text(P1scoreboard, text="P1 Total Score", align="left")
  131. P1rscore = Text(P1scoreboard, text="0", align="left")
  132.  
  133. P2scoreboard = Box(app)
  134. P2sclabel = Text(P2scoreboard, text="P2 Round Score", align="left")
  135. P2score = Text(P2scoreboard, text="0", align="left")
  136. P2rsclabel = Text(P2scoreboard, text="P2 Total Score", align="left")
  137. P2rscore = Text(P2scoreboard, text="0", align="left")
  138.  
  139. threeinarow = Box(app)
  140. tiar = Text(threeinarow, text = "", align ="left")
  141.  
  142. setup_round()
  143.  
  144. # start the timer
  145. timer.value = 30
  146. timer.repeat(1000,counter)
  147.  
  148. round.value = 1
  149.  
  150.  
  151. # instructions window
  152.  
  153. instwindow = Window(app, title="Instructions")
  154. instwindow.hide()
  155.  
  156. insttext = Text(instwindow, text="Matching Emojis Game \n \n Spot the duplicate emoji by clicking the \n bottom button that matches a top picture \n  \n  Bonus 5 seconds added for 3 correct in a row \n \n Watch the timer")
  157.  
  158. open_inst_btn = PushButton(app, text="Instructions", command=open_inst_window)
  159. close_inst_btn = PushButton(instwindow, text="Close", command=close_inst_window)
  160.  
  161.  
  162. app.display()
Advertisement
RAW Paste Data Copied
Advertisement