Advertisement
JAWarr

Emoji game with 2 sec timer alert and round function

May 15th, 2022
877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.40 KB | None | 0 0
  1. #adding a 20 second timer with round function
  2. import os
  3. from random import shuffle, randint
  4. from guizero import App, Box, Picture, PushButton, Text, warn
  5.  
  6. def counter():
  7.     timer.value = int(timer.value) - 1
  8.     if int(timer.value) == 0:
  9.         timer.cancel(counter)
  10.         # reset the timer
  11.         result.value = "Game Over"
  12.         warn("Time Out", "you've run out of time")
  13.         # reset timer
  14.         timer.value = "20"
  15.         # reset result
  16.         result.value = ""
  17.         # start new round
  18.         setup_round()
  19.         #restart timer
  20.         timer.repeat(1000, counter)
  21.         round_number()
  22.  
  23. def round_number():
  24.     rounds.value = int(rounds.value) +1
  25.  
  26.  
  27. # set the path to the emoji folder on your computer
  28. emojis_dir = "emojis"
  29. # create a list of the locations of the emoji images
  30. emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  31. # shuffle the emojis
  32. shuffle(emojis)
  33.  
  34. def match_emoji(matched):
  35.     if matched:
  36.         result.value = "correct"
  37.     else:
  38.         result.value = "incorrect"
  39.        
  40.     setup_round()
  41.  
  42. def setup_round():
  43.     # for each picture and button in the list assign an emoji to its image feature
  44.     for picture in pictures:
  45.         # make the picture a random emoji
  46.         picture.image = emojis.pop()
  47.  
  48.     for button in buttons:
  49.         # make the image feature of the PushButton a random emoji
  50.         button.image = emojis.pop()
  51.         # set the command to be called and pass False, as these emoji wont be the matching ones
  52.         button.update_command(match_emoji, [False])
  53.  
  54.     # choose a new emoji
  55.     matched_emoji = emojis.pop()
  56.  
  57.     # select a number at random
  58.     random_picture = randint(0,8)
  59.     # change the image feature of the Picture with this index in the list of pictures to the new emoji
  60.     pictures[random_picture].image = matched_emoji
  61.  
  62.     random_button = randint(0,8)
  63.     # change the image feature of the PushButton with this index in the list of buttons to the new emoji
  64.     buttons[random_button].image = matched_emoji
  65.     # set the command to be called and pass True, as this is the matching emoji
  66.     buttons[random_button].update_command(match_emoji, [True])
  67.  
  68.  
  69. # setup the app
  70. app = App("emoji match")
  71.  
  72. result = Text(app)
  73.  
  74. # create a box to house the grids
  75. game_box = Box(app)
  76. # create a box to house the pictures
  77. pictures_box = Box(game_box, layout="grid", align="left")
  78. # create a box to house the buttons
  79. buttons_box = Box(game_box, layout="grid", align="right")
  80.  
  81. # create the an empty lists to add the buttons and pictures to
  82. buttons = []
  83. pictures = []
  84. # create 9 PushButtons with a different grid coordinate and add to the list
  85. for x in range(0,3):
  86.     for y in range(0,3):
  87.         # put the pictures and buttons into the lists
  88.         picture = Picture(pictures_box, grid=[x,y])
  89.         pictures.append(picture)
  90.        
  91.         button = PushButton(buttons_box, grid=[x,y])
  92.         buttons.append(button)
  93.  
  94.  
  95. # add in the extra features
  96. extra_features = Box(app)
  97. timer_label = Text(extra_features, text="Countdown: ", align="left")
  98. timer = Text(extra_features, text="Get Ready", align="left")
  99. rounds = Text(extra_features, text=1, align="right")
  100. rounds_label = Text(extra_features, text="Round: ", align="right")
  101.  
  102.  
  103.  
  104. setup_round()
  105.  
  106. # start the timer
  107. timer.value = 20
  108. timer.repeat(1000,counter)
  109. rounds.value = 1
  110.  
  111. app.display()
  112.  
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement