Advertisement
Guest User

Untitled

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