Advertisement
zhongnaomi

matching game 4

Jan 6th, 2020
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 KB | None | 0 0
  1. import os
  2. from random import shuffle, randint
  3. from guizero import App, Box, Picture, PushButton, Text
  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.  
  13. def match_emoji(matched):
  14.     if matched:
  15.         result.value = "correct"
  16.         score.value = int(score.value) + 1
  17.     else:
  18.         result.value = "incorrect"
  19.  
  20.     setup_round()
  21.    
  22.        
  23. def setup_round():
  24.     # for each picture and button in the list assign an emoji to its image feature
  25.     for picture in pictures:
  26.         # make the picture a random emoji
  27.         picture.image = emojis.pop()
  28.  
  29.     for button in buttons:
  30.         # make the image feature of the PushButton a random emoji
  31.         button.image = emojis.pop()
  32.         # set the command to be called and pass False, as these emoji wont be the matching ones
  33.         button.update_command(match_emoji, [False])
  34.  
  35.     # choose a new emoji
  36.     matched_emoji = emojis.pop()
  37.  
  38.     # select a number at random
  39.     random_picture = randint(0,8)
  40.     # change the image feature of the Picture with this index in the list of pictures to the new emoji
  41.     pictures[random_picture].image = matched_emoji
  42.  
  43.     random_button = randint(0,8)
  44.     # change the image feature of the PushButton with this index in the list of buttons to the new emoji
  45.     buttons[random_button].image = matched_emoji
  46.     # set the command to be called and pass True, as this is the matching emoji
  47.     buttons[random_button].update_command(match_emoji, [True])
  48.  
  49. # setup the app
  50. app = App("emoji match", height=600)
  51.  
  52.  
  53. def counter():
  54.     timer.value = int(timer.value) - 1
  55.     if  int(timer.value)== 0:
  56.        
  57.         game_box.destroy()
  58.         result.value = "Game Over"
  59.         timer.value = "Game Over"
  60.         # reset result
  61.         result.value = ""
  62.         # start new round
  63.         setup_round()
  64.         #restart timer
  65.         timer.repeat(1000, counter)
  66.        
  67.  
  68. result = Text(app)
  69.  
  70. # create a box to house the grids
  71. game_box = Box(app)
  72.  
  73. # create a box to house the pictures
  74. pictures_box = Box(game_box, layout="grid")
  75. # create a box to house the buttons
  76. buttons_box = Box(game_box, layout="grid")
  77.  
  78. # create the an empty lists to add the buttons and pictures to
  79. buttons = []
  80. pictures = []
  81. # create 9 PushButtons with a different grid coordinate and add to the list
  82. for x in range(0,3):
  83.     for y in range(0,3):
  84.         # put the pictures and buttons into the lists
  85.         picture = Picture(pictures_box, grid=[x,y])
  86.         pictures.append(picture)
  87.        
  88.         button = PushButton(buttons_box, grid=[x,y])
  89.         buttons.append(button)
  90. # add in the extra features
  91. extra_features = Box(app)
  92. timer = Text(extra_features, text="Get Ready")
  93. scoreboard = Box(app)
  94. label = Text(scoreboard, text="Score", align="left")
  95. score = Text(scoreboard, text="0", align="left" )
  96.  
  97. setup_round()
  98. # start the timer
  99. timer.value = 30
  100. timer.repeat(1000,counter)
  101. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement