Advertisement
Vesna_To

igra2

Aug 22nd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. import os
  2. from random import *
  3. from guizero import *
  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. rew = 0
  13. def match_emoji(matched):
  14. global rew
  15. if matched:
  16. result.value = "Correct"
  17. result.text_color="blue"
  18. score.value = int(score.value) + 1
  19. rew = rew + 1
  20. if rew == 3:
  21. warn("Reward", "Three in a row! You got 10 seconds of extra time!")
  22. timer.value = int(timer.value) + 10
  23. rew = 0
  24. else:
  25. result.value = "Incorrect!"
  26. result.text_color="red"
  27. score.value = int(score.value) - 1
  28. rew = 0
  29. rounds.value = int(rounds.value) + 1
  30. setup_round()
  31.  
  32. def counter():
  33. timer.value = int(timer.value) - 1
  34. if int(timer.value) == 0:
  35. timer.cancel(counter)
  36. # reset the timer
  37. result.value = "Game Over"
  38. warn("Time Out", "You've run out of time! Score: " + str(score.value) + " Round: " + str(rounds.value))
  39. # reset timer
  40. timer.value = "60"
  41. # reset result
  42. result.value = ""
  43. # start new round
  44. rounds.value = 0
  45. score.value = 0
  46. #restart timer
  47. timer.repeat(1000, counter)
  48. setup_round()
  49.  
  50. def setup_round():
  51.  
  52. # for each picture and button in the list assign an emoji to its image feature
  53. for picture in pictures:
  54. # make the picture a random emoji
  55. picture.image = emojis.pop()
  56.  
  57. for button in buttons:
  58. # make the image feature of the PushButton a random emoji
  59. button.image = emojis.pop()
  60. # set the command to be called and pass False, as these emoji wont be the matching ones
  61. button.update_command(match_emoji, [False])
  62.  
  63. matched_emoji = emojis.pop()
  64.  
  65. # select a number at random
  66. random_picture = randint(0,8)
  67. # change the image feature of the Picture with this index in the list of pictures to the new emoji
  68. pictures[random_picture].image = matched_emoji
  69.  
  70. random_button = randint(0,8)
  71. # change the image feature of the PushButton with this index in the list of buttons to the new emoji
  72. buttons[random_button].image = matched_emoji
  73. buttons[random_button].update_command(match_emoji, [True])
  74.  
  75. # setup app
  76. app = App("emoji match")
  77.  
  78. scoreboard = Box(app, layout="grid")
  79. label = Text(scoreboard, grid = [0, 0], text="Score")
  80. score = Text(scoreboard, text="0", grid = [0, 1])
  81. rounds = Text(scoreboard, text="Round: ", grid = [2, 0])
  82. rounds = Text(scoreboard, text=" ", grid = [2, 1] )
  83. rounds.value = 0
  84.  
  85.  
  86. # add in the extra features
  87. extra_features = Box(app)
  88. result = Text(extra_features)
  89. timer_text = Text(extra_features, text="Time: ")
  90. timer = Text(extra_features, text="Get Ready")
  91.  
  92. # create a box to house the grid
  93. game_box = Box(app)
  94. #create a box to house the pictures
  95. pictures_box = Box(game_box, layout="grid", align="left")
  96. #create a box to house the buttons
  97. buttons_box = Box(game_box, layout="grid", align="right")
  98. # create an empty list to which pictures and buttons will be added
  99. pictures = []
  100. buttons = []
  101. for x in range(0,3):
  102. for y in range(0,3):
  103. # put the pictures and buttons into the list
  104. picture = Picture(pictures_box, grid=[x,y])
  105. pictures.append(picture)
  106.  
  107. button = PushButton(buttons_box, grid=[x,y])
  108. buttons.append(button)
  109.  
  110.  
  111. # start the timer
  112. timer.value = 60
  113. timer.repeat(1000, counter)
  114. setup_round()
  115. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement