JAWarr

Emoji game with alerts and bonus

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