Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.89 KB | None | 0 0
  1. # imported functions
  2. import os
  3. from random import *
  4. from guizero import *
  5. from random import *
  6.  
  7.  
  8. class game_infos():
  9.     rounds_played = "0"
  10.  
  11.  
  12. def exit_gui():
  13.     app.destroy()
  14.  
  15.  
  16. def setup_round():
  17.     for picture in pictures:
  18.         # make the picture a random emoji
  19.         picture.image = emojis.pop()
  20.  
  21.     for button in buttons:
  22.         # make the button a random emoji
  23.         button.image = emojis.pop()
  24.         # set the command to be called and pass False, as these emoji wont be the matching ones
  25.         button.update_command(match_emoji, [False])
  26.  
  27.     # make 2 of the pictures the same, but in different grid
  28.     matched_emoji = emojis.pop()
  29.  
  30.     random_picture = randint(0, 8)
  31.     pictures[random_picture].image = matched_emoji
  32.  
  33.     random_button = randint(0, 8)
  34.     buttons[random_button].image = matched_emoji
  35.  
  36.     # set the command to be called and pass True, as this is the matching emoji
  37.     buttons[random_button].update_command(match_emoji, [True])
  38.  
  39.  
  40. def match_emoji(matched):
  41.     if matched:
  42.         result.value = "correct"
  43.     else:
  44.         result.value = "incorrect"
  45.     setup_round()
  46.     game_infos.rounds_played = int(game_infos.rounds_played) + 1
  47.     print(game_infos.rounds_played)
  48.     game_played.value = "Played " + str(game_infos.rounds_played)
  49.  
  50.  
  51. # set the path to the emoji folder on your computer
  52. emojis_dir = "emojis"
  53. # create a list of the locations of the emoji images
  54. emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  55. # shuffle the emojis
  56. shuffle(emojis)
  57.  
  58.  
  59. def counter():
  60.     timer.value = int(timer.value) - 1
  61.     if int(timer.value) == 0:
  62.         timer.cancel(counter)
  63.         # reset the timer
  64.         result.value = "Game Over"
  65.         warn("Time Out", "you've run out of time")
  66.         # reset timer
  67.         timer.value = "20"
  68.         # reset result
  69.         result.value = ""
  70.         # start new round
  71.         setup_round()
  72.         # restart timer
  73.         timer.repeat(1000, counter)
  74.  
  75.  
  76. # def blinkBtn():
  77. #    if btnTrick.bg == "red":
  78. #        btnTrick.bg = "black"
  79. #        btnTrick.text_color="white"
  80. #    else:
  81. #        btnTrick.bg = "red"
  82. #        btnTrick.text_color = "Black"
  83.  
  84.  
  85. ######################   Begin of the App code   ######################
  86. app = App("Emoji Match", height=600)
  87.  
  88. game_played = Text(app, text="Played "+str(game_infos.rounds_played))
  89.  
  90. result = Text(app, height=3)
  91.  
  92. ###### blinky btn ########
  93. # btnTrick = PushButton(app, text="Color")
  94. # btnTrick.bg="red"
  95. # btnTrick.repeat(1000, blinkBtn)
  96.  
  97. # add in the extra features
  98. extra_features = Box(app)
  99. timer = Text(extra_features, text="Get Ready")
  100.  
  101. # create a box to house the grids
  102. game_box = Box(app)
  103.  
  104. # create a box to house the grid
  105. pictures_box = Box(game_box, layout="grid")
  106.  
  107. # create a box to house the buttons
  108. buttons_box = Box(game_box, layout="grid")
  109.  
  110. # create an empty lists to add the buttons and pictures to
  111. pictures = []
  112. buttons = []
  113.  
  114. # create 9 PushButtons with a different grid coordinate and add to the list
  115. for x in range(0, 3):
  116.     for y in range(0, 3):
  117.         # put the pictures and buttons into the lists
  118.         button = PushButton(buttons_box, grid=[x, y])
  119.         buttons.append(button)
  120.  
  121. for x in range(0, 3):
  122.     for y in range(0, 3):
  123.         # put the pictures and buttons into the lists
  124.         picture = Picture(pictures_box, grid=[x, y])
  125.         pictures.append(picture)
  126.  
  127. # for each picture and button in the list assign an emoji to its image feature
  128. for picture in pictures:
  129.     # make the picture a random emoji
  130.     picture.image = emojis.pop()
  131.  
  132. for button in buttons:
  133.     # make the image feature of the PushButton a random emoji
  134.     button.image = emojis.pop()
  135.     # set the command to be called and pass False, as these emoji wont be the matching ones
  136.     button.update_command(match_emoji, [False])
  137.  
  138. # for each picture in the list
  139. for picture in pictures:
  140.     # make the picture a random emoji
  141.     picture.image = emojis.pop()
  142.  
  143. # choose a new emoji
  144. matched_emoji = emojis.pop()
  145.  
  146. # select a number at random
  147. random_picture = randint(0, 8)
  148. # change the image feature of the Picture with this index in the list of pictures to the new emoji
  149. pictures[random_picture].image = matched_emoji
  150.  
  151. random_button = randint(0, 8)
  152. # change the image feature of the PushButton with this index in the list of buttons to the new emoji
  153. buttons[random_button].image = matched_emoji
  154.  
  155. # set the command to be called and pass True, as this is the matching emoji
  156. buttons[random_button].update_command(match_emoji, [True])
  157.  
  158. # start the timer
  159. timer.value = 20
  160. timer.repeat(1000, counter)
  161.  
  162. ###### bottom commands buttons #########
  163. bottom_btn_box = Box(app, width="fill", align="bottom")
  164. exitBtn = PushButton(bottom_btn_box, text="Exit", align="right", command=exit_gui)
  165. okBtn = PushButton(bottom_btn_box, text="Ok", align="right")
  166.  
  167. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement