Advertisement
zhongnaomi

matching game v3 with timer and count click

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