Advertisement
xavicano

Untitled

Aug 20th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.46 KB | None | 0 0
  1. import os
  2. from random import shuffle, randint
  3. from guizero import App, Box, Picture, PushButton, Text, info, CheckBox
  4.  
  5.  
  6.  
  7. def match_emoji(matched):
  8.     if matched:
  9.         result.value = "Correct!!!"
  10.         emojis=emoji_list()
  11.         new_try()
  12.         score.value = int(score.value) + 1
  13.         row.value = int(row.value) + 1
  14.         if int(row.value)==3:
  15.             if bonustime.value:
  16.                 timer.value=int(timer.value)+10
  17.                 info("info","Well done, Extra time!!!")
  18.             else:
  19.                 score.value = int(score.value) + 1
  20.                 info("info","Well done, bonus point!!!")
  21.             row.value="0"
  22.     else:
  23.         result.value = "Incorrect"
  24.         score.value = int(score.value) - 1
  25.         row.value="0"
  26.  
  27. # set the path to the emoji folder on your computer
  28. emojis_dir = "C:\\Users\\xavi.cano\\Code\\emojis\\emojis"
  29.  
  30. def emoji_list():
  31.     # create a list of the locations of the emoji images
  32.     emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  33.     # shuffle the emojis
  34.     shuffle(emojis)
  35.     return emojis
  36.  
  37.  
  38. Xrange=3
  39. Yrange=3
  40. buttons = []
  41. pictures = []
  42.  
  43.  
  44. def new_try():
  45.     ## for each picture in the list
  46.     for picture in pictures:
  47.         ## make the picture a random emoji
  48.         picture.image = emojis.pop()
  49.     for button in buttons:
  50.         # make the image feature of the PushButton a random emoji
  51.         button.image = emojis.pop()  
  52.         # set the command to be called and pass False, as these emoji wont be the matching ones
  53.         button.update_command(match_emoji, [False])
  54.     random_button = randint(0,8)
  55.     random_picture = randint(0,8)
  56.     buttons[random_button].image=pictures[random_picture].image
  57.     buttons[random_button].update_command(match_emoji, [True])
  58.  
  59.    
  60. def button_sec():
  61.     if buttonsec.bg == "black":
  62.         buttonsec.bg = "white"
  63.         buttonsec.text_color = "black"
  64.     else:
  65.         buttonsec.bg = "black"
  66.         buttonsec.text_color = "white"
  67.    
  68.    
  69. def decrease():
  70.     counter=int(timer.value)-1
  71.     button_sec()
  72.     if int(rounds.value)==5:
  73.         info("info","Go home!!! Too many tries")
  74.     if counter==0:
  75.         timer.value="30"
  76.         score.value = "0"
  77.         rounds.value=str(int(rounds.value)+1)
  78.     else:
  79.         timer.value = str(counter)
  80.        
  81.  
  82. emojis=[]
  83.  
  84. app = App("emoji match")
  85.  
  86.  
  87. # create a box to house the grids
  88. game_box = Box(app)
  89. # create a box to house the pictures
  90. pictures_box = Box(game_box, layout="grid", align="left")
  91. # create a box to house the buttons
  92. buttons_box = Box(game_box, layout="grid", align="right")
  93.  
  94. command_box= Box(app)
  95.  
  96. scoreboard = Box(app)
  97. label = Text(scoreboard, text="Score : ", align="left")
  98. score = Text(scoreboard, text="0", align="left")
  99. label2 = Text(scoreboard, text="In a row : ", align="left")
  100. row=Text(scoreboard, text="0", align="left")
  101. score.value = "0"
  102.  
  103.  
  104. result = Text(app)
  105.  
  106. timer=Text(command_box, text="30", align="left")
  107. buttonsec=PushButton(command_box,text=" seconds", align="left")
  108. buttonsec.bg="black"
  109. buttonsec.text_color = "white"
  110. Label_rounds=Text(command_box, text="Rounds : ", align="left")
  111. rounds=Text(command_box, text="1", align="left")
  112. bonustime = CheckBox(command_box, text="Bonus Time")
  113.  
  114. emojis=emoji_list()
  115. timer.repeat(1000, decrease)
  116.  
  117.  
  118.  
  119. # create 9 PushButtons with a different grid coordinate and add to the list
  120. for x in range(0,Xrange):
  121.     for y in range(0,Yrange):
  122.         # put the pictures and buttons into the lists
  123.         picture = Picture(pictures_box, grid=[x,y])
  124.         pictures.append(picture)
  125.  
  126.         button = PushButton(buttons_box, grid=[x,y])
  127.         buttons.append(button)
  128.  
  129. new_try()
  130. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement