Advertisement
zhongnaomi

matching game 3

Jan 6th, 2020
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.72 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. def decrease():
  52.     timer.value = int(timer.value) - 1
  53.     if  int(timer.value)== 0:
  54.        
  55.         game_box.destroy()
  56.         timer.value="Game over!"
  57.        
  58.  
  59. result = Text(app)
  60. timer = Text(app, text=30)
  61. timer.repeat(1000, decrease)
  62. # create a box to house the grids
  63. game_box = Box(app)
  64.  
  65. # create a box to house the pictures
  66. pictures_box = Box(game_box, layout="grid")
  67. # create a box to house the buttons
  68. buttons_box = Box(game_box, layout="grid")
  69.  
  70. # create the an empty lists to add the buttons and pictures to
  71. buttons = []
  72. pictures = []
  73. # create 9 PushButtons with a different grid coordinate and add to the list
  74. for x in range(0,3):
  75.     for y in range(0,3):
  76.         # put the pictures and buttons into the lists
  77.         picture = Picture(pictures_box, grid=[x,y])
  78.         pictures.append(picture)
  79.        
  80.         button = PushButton(buttons_box, grid=[x,y])
  81.         buttons.append(button)
  82.  
  83. setup_round()
  84. scoreboard = Box(app)
  85. label = Text(scoreboard, text="Score", align="left")
  86. score = Text(scoreboard, text="0", align="left" )
  87. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement