Advertisement
Vesna_To

igra1

Aug 22nd, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 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.  
  13. def match_emoji(matched):
  14. global r
  15. if matched:
  16. result.value = "correct"
  17. result.text_color="red"
  18. else:
  19. result.value = "incorrect"
  20. result.text_color="blue"
  21. rounds.value = int(rounds.value) + 1
  22. setup_round()
  23.  
  24. def counter():
  25. timer.value = int(timer.value) - 1
  26. if int(timer.value) == 0:
  27. timer.cancel(counter)
  28. # reset the timer
  29. result.value = "Game Over"
  30. warn("Time Out", "You've run out of time")
  31. # reset timer
  32. timer.value = "20"
  33. # reset result
  34. result.value = ""
  35. # start new round
  36. rounds.value=0
  37. setup_round()
  38. #restart timer
  39. timer.repeat(1000, counter)
  40.  
  41. def setup_round():
  42.  
  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. matched_emoji = emojis.pop()
  55.  
  56. # select a number at random
  57. random_picture = randint(0,8)
  58. # change the image feature of the Picture with this index in the list of pictures to the new emoji
  59. pictures[random_picture].image = matched_emoji
  60.  
  61. random_button = randint(0,8)
  62. # change the image feature of the PushButton with this index in the list of buttons to the new emoji
  63. buttons[random_button].image = matched_emoji
  64. buttons[random_button].update_command(match_emoji, [True])
  65.  
  66. # setup app
  67. app = App("emoji match")
  68.  
  69. # create a box to house the grid
  70. game_box = Box(app)
  71. #create a box to house the pictures
  72. pictures_box = Box(game_box, layout="grid", align="left")
  73. #create a box to house the buttons
  74. buttons_box = Box(game_box, layout="grid", align="right")
  75. # create an empty list to which pictures and buttons will be added
  76. pictures = []
  77. buttons = []
  78. for x in range(0,3):
  79. for y in range(0,3):
  80. # put the pictures and buttons into the list
  81. picture = Picture(pictures_box, grid=[x,y])
  82. pictures.append(picture)
  83.  
  84. button = PushButton(buttons_box, grid=[x,y])
  85. buttons.append(button)
  86.  
  87. # add in the extra features
  88. extra_features = Box(app)
  89. result = Text(extra_features)
  90. timer_text = Text(extra_features, text="Time: ")
  91. timer = Text(extra_features, text="Get Ready")
  92. rounds = Text(extra_features, text="Round: ")
  93. rounds = Text(extra_features, text=" ")
  94. rounds.value = 0
  95. setup_round()
  96.  
  97. # start the timer
  98. timer.value = 20
  99. timer.repeat(1000, counter)
  100.  
  101. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement