Advertisement
timber101

Untitled

Sep 3rd, 2019
240
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.24 KB | None
  1. import os
  2. from random import shuffle, randint
  3. from guizero import App, Box, Picture, PushButton, Text, warn
  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. def match_emoji(matched):
  13.     if matched:
  14.         result.value = "correct"
  15.         score.value = int(score.value) + 1
  16.         rscore.value = int(rscore.value) + 1
  17.         tiar.value = tiar.value + "X"
  18.         if tiar.value == "XXX":
  19.             timer.value = int(timer.value) +5
  20.             tiar.value = ""
  21.  
  22.     else:
  23.         result.value = "incorrect"
  24.         unrscore.value = int(unrscore.value) + 1
  25.         tiar.value = ""
  26.        
  27.     setup_round()
  28.  
  29. def setup_round():
  30.     # for each picture and button in the list assign an emoji to its image feature
  31.     for picture in pictures:
  32.         # make the picture a random emoji
  33.         picture.image = emojis.pop()
  34.  
  35.     for button in buttons:
  36.         # make the image feature of the PushButton a random emoji
  37.         button.image = emojis.pop()
  38.         # set the command to be called and pass False, as these emoji wont be the matching ones
  39.         button.update_command(match_emoji, [False])
  40.  
  41.     # choose a new emoji
  42.     matched_emoji = emojis.pop()
  43.  
  44.     # select a number at random
  45.     random_picture = randint(0,8)
  46.     # change the image feature of the Picture with this index in the list of pictures to the new emoji
  47.     pictures[random_picture].image = matched_emoji
  48.  
  49.     random_button = randint(0,8)
  50.     # change the image feature of the PushButton with this index in the list of buttons to the new emoji
  51.     buttons[random_button].image = matched_emoji
  52.     # set the command to be called and pass True, as this is the matching emoji
  53.     buttons[random_button].update_command(match_emoji, [True])
  54.  
  55. def counter():
  56.     timer.value = int(timer.value) - 1
  57.     if int(timer.value) == 0:
  58.         timer.cancel(counter)
  59.         # reset the timer
  60.         result.value = "Game Over"
  61.         warn("Time Out", "you've run out of time final score:- " + rscore.value)
  62.         # reset timer
  63.         timer.value = "40"
  64.         # reset result
  65.         result.value = ""
  66.         # start new round
  67.         setup_round()
  68.         #restart timer
  69.         timer.repeat(1000, counter)
  70.         #update round number
  71.         round.value = int(round.value)+1
  72.         #reset score to 0
  73.         score.value = 0
  74.         unscore.value = 0
  75.        
  76. # setup the app
  77. app = App("emoji match", height=630)
  78.  
  79. result = Text(app)
  80.  
  81. # create a box to house the grids
  82. game_box = Box(app)
  83. # create a box to house the pictures
  84. pictures_box = Box(game_box, layout="grid")
  85. # create a box to house the buttons
  86. buttons_box = Box(game_box, layout="grid")
  87.  
  88. # create the an empty lists to add the buttons and pictures to
  89. buttons = []
  90. pictures = []
  91. # create 9 PushButtons with a different grid coordinate and add to the list
  92. for x in range(0,3):
  93.     for y in range(0,3):
  94.         # put the pictures and buttons into the lists
  95.         picture = Picture(pictures_box, grid=[x,y])
  96.         pictures.append(picture)
  97.        
  98.         button = PushButton(buttons_box, grid=[x,y])
  99.         buttons.append(button)
  100.  
  101.  
  102. # add in the extra features
  103. extra_features = Box(app)
  104. timer = Text(extra_features, text="Get Ready")
  105.  
  106. roundboard = Box(app)
  107. rolabel = Text(roundboard, text = "Round", align="left")
  108. round = Text(roundboard, text="round")
  109.  
  110. scoreboard = Box(app)
  111. sclabel = Text(scoreboard, text="Score", align="left")
  112. score = Text(scoreboard, text="0", align="left")
  113. rsclabel = Text(scoreboard, text="Runing Score", align="left")
  114. rscore = Text(scoreboard, text="0", align="left")
  115.  
  116. unscoreboard = Box(app)
  117. unsclabel = Text(unscoreboard, text="Un Score", align="left")
  118. unscore = Text(unscoreboard, text="0", align="left")
  119. unrsclabel = Text(unscoreboard, text="Runing Un Score", align="left")
  120. unrscore = Text(unscoreboard, text="0", align="left")
  121.  
  122. threeinarow = Box(app)
  123. tiar = Text(threeinarow, text = "", align ="left")
  124.  
  125. setup_round()
  126.  
  127. # start the timer
  128. timer.value = 40
  129. timer.repeat(1000,counter)
  130.  
  131. round.value = 1
  132.  
  133. app.display()
Advertisement
RAW Paste Data Copied
Advertisement