xavicano

Untitled

Aug 26th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.24 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. def match_emoji_p1(matched):
  6.     if matched:
  7.         result.value = "Correct!!!" + winning()
  8.         new_try(1)
  9.         score_p1.value = int(score_p1.value) + 1
  10.         row_p1.value = int(row_p1.value) + 1
  11.         if int(row_p1.value)==3:
  12.             if bonustime.value:
  13.                 timer.value=int(timer.value)+10
  14.                 info("info","Well done, Extra time!!!")
  15.             else:
  16.                 score_p1.value = int(score_p1.value) + 1
  17.                 info("info","Well done, bonus point!!!")
  18.             row_p1.value="0"
  19.     else:
  20.         result.value = "Incorrect" + winning()
  21.         score_p1.value = int(score_p1.value) - 1
  22.         row_p1.value="0"
  23.         rounds_p1.value=str(int(rounds_p1.value)+1)
  24.         change_state() 
  25.        
  26. def match_emoji_p2(matched):
  27.     if matched:
  28.         result.value = "Correct!!!" + winning()
  29.         emojis=emoji_list()
  30.         new_try(2)
  31.         score_p2.value = int(score_p2.value) + 1
  32.         row_p2.value = int(row_p2.value) + 1
  33.         if int(row_p2.value)==3:
  34.             if bonustime.value:
  35.                 timer.value=int(timer.value)+10
  36.                 info("info","Well done, Extra time!!!")
  37.             else:
  38.                 score_p2.value = int(score_p2.value) + 1
  39.                 info("info","Well done, bonus point!!!")
  40.             row_p2.value="0"
  41.     else:
  42.         result.value = "Incorrect" + winning()
  43.         score_p2.value = int(score_p2.value) - 1
  44.         row_p2.value="0"
  45.         rounds_p2.value=str(int(rounds_p2.value)+1)
  46.         change_state() 
  47.  
  48. # set the path to the emoji folder on your computer
  49. emojis_dir = "C:\\Users\\xavi.cano\\Code\\emojis\\emojis"
  50. general_enable=False
  51.  
  52. def emoji_list():
  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.     return emojis
  58.  
  59. def new_try(player):
  60.     emojis=emoji_list()
  61.     random_button = randint(0,8)
  62.     random_picture = randint(0,8)
  63.     if player==1:
  64.         ## for each picture in the list
  65.         for picture_p1 in pictures_p1:
  66.             ## make the picture a random emoji
  67.             picture_p1.image = emojis.pop()
  68.         for button_p1 in buttons_p1:
  69.             # make the image feature of the PushButton a random emoji
  70.             button_p1.image = emojis.pop()  
  71.             # set the command to be called and pass False, as these emoji wont be the matching ones
  72.             button_p1.update_command(match_emoji_p1, [False])
  73.         buttons_p1[random_button].image=pictures_p1[random_picture].image
  74.         buttons_p1[random_button].update_command(match_emoji_p1, [True])
  75.     else:
  76.                 ## for each picture in the list
  77.         for picture_p2 in pictures_p2:
  78.             ## make the picture a random emoji
  79.             picture_p2.image = emojis.pop()
  80.         for button_p2 in buttons_p2:
  81.             # make the image feature of the PushButton a random emoji
  82.             button_p2.image = emojis.pop()  
  83.             # set the command to be called and pass False, as these emoji wont be the matching ones
  84.             button_p2.update_command(match_emoji_p2, [False])
  85.         buttons_p2[random_button].image=pictures_p2[random_picture].image
  86.         buttons_p2[random_button].update_command(match_emoji_p2, [True])
  87.            
  88. def button_sec():
  89.     global general_enable
  90.     if general_enable==True:
  91.         if buttonsec.bg == "black":
  92.             buttonsec.bg = "white"
  93.             buttonsec.text_color = "black"
  94.         else:
  95.             buttonsec.bg = "black"
  96.             buttonsec.text_color = "white"
  97.        
  98. def decrease():
  99.     global general_enable
  100.     if general_enable==True:
  101.         counter=int(timer.value)-1
  102.         button_sec()
  103.         if counter==0:
  104.             timer.value="30"
  105.             change_state()
  106.             rounds.value=str(int(rounds.value)+1)
  107.         else:
  108.             timer.value = str(counter)
  109.        
  110. def button_start():
  111.     global general_enable
  112.     if general_enable==True:
  113.         general_enable=False
  114.         buttonsec.text=" Pause"
  115.     else:
  116.         general_enable=True
  117.         pictures1_box.enabled=False
  118.         change_state()
  119.  
  120. def change_state():
  121.     if pictures1_box.enabled==True:
  122.         pictures2_box.enabled=True
  123.         buttons2_box.enabled=True
  124.         pictures1_box.enabled=False
  125.         buttons1_box.enabled=False
  126.     else:
  127.         pictures2_box.enabled=False
  128.         buttons2_box.enabled=False
  129.         pictures1_box.enabled=True
  130.         buttons1_box.enabled=True
  131.  
  132. def winning():
  133.     if int(score_p1.value)> int(score_p2.value):
  134.         return " Player 1 Winning"
  135.     elif int(score_p1.value)< int(score_p2.value):
  136.         return " Player 2 Winning"
  137.     else:
  138.         return " Tie "
  139.        
  140. def set_of_rules():
  141.     build_a_snowman = app.yesno("A question...", "Do you agree the set of rules?")
  142.     if build_a_snowman == True:
  143.         app.info("Set of Rules", "Enjoy the game")
  144.     else:
  145.         app.error("Set of Rules", "Okay bye...")
  146.         quit()
  147.     return False
  148.  
  149. emojis=[]
  150.  
  151. app = App("emoji match")
  152. app.width=920
  153. app.height=380
  154.  
  155. # create a box to house the grids
  156. game_box = Box(app, width="fill")
  157. # create a box to house the pictures
  158. pictures1_box = Box(game_box, layout="grid", align="left")
  159. # create a box to house the buttons
  160. buttons1_box = Box(game_box, layout="grid", align="left")
  161. # create a box to house the pictures
  162. pictures2_box = Box(game_box, layout="grid", align="left")
  163. # create a box to house the buttons
  164. buttons2_box = Box(game_box, layout="grid", align="left")
  165.  
  166. command_box= Box(app)
  167.  
  168. # Create a scoreBoard BOX
  169. scoreboard = Box(app, layout ="grid")
  170.  
  171. label = Text(scoreboard, grid = [0,0], text="Player 1    ", align="left")
  172. label = Text(scoreboard, grid = [1,0],text="Score : ", align="left")
  173. score_p1 = Text(scoreboard, grid = [2,0],text="0", align="left")
  174. Label_rounds=Text(scoreboard, grid = [3,0],text="  Rounds : ", align="left")
  175. rounds_p1=Text(scoreboard, grid = [4,0],text="0", align="left")
  176. label2 = Text(scoreboard, grid = [5,0],text="  In a row : ", align="left")
  177. row_p1=Text(scoreboard, grid = [6,0],text="0", align="left")
  178. score_p1.value = "0"
  179.  
  180. label = Text(scoreboard, grid = [0,1],text="Player 2    ", align="left")
  181. label = Text(scoreboard, grid = [1,1],text="Score : ", align="left")
  182. score_p2 = Text(scoreboard, grid = [2,1],text="0", align="left")
  183. Label_rounds=Text(scoreboard, grid = [3,1],text="  Rounds : ", align="left")
  184. rounds_p2=Text(scoreboard, grid = [4,1],text="0", align="left")
  185. label2 = Text(scoreboard, grid = [5,1],text="  In a row : ", align="left")
  186. row_p2=Text(scoreboard, grid = [6,1],text="0", align="left")
  187. score_p2.value = "0"
  188.  
  189. result = Text(app)
  190.  
  191. timer=Text(command_box, text="30", align="left")
  192. buttonsec=PushButton(command_box,command=button_start,text=" Start", align="left", height=1)
  193. buttonsec.bg="white"
  194. buttonsec.text_color = "black"
  195. buttonsec.text_size=12
  196. Label_rounds=Text(command_box, text="Rounds : ", align="left")
  197. rounds=Text(command_box, text="1", align="left")
  198. bonustime = CheckBox(command_box, text="Bonus Time", height="fill")
  199. bonustime.text_size=12
  200. buttonrules=PushButton(app,command=set_of_rules,text=" Set of Rules", align="right", height="fill")
  201.  
  202. timer.repeat(1000, decrease)
  203.  
  204. Xrange=3
  205. Yrange=3
  206. buttons_p1 = []
  207. pictures_p1 = []
  208. buttons_p2 = []
  209. pictures_p2 = []
  210.  
  211. # create 9 PushButtons with a different grid coordinate and add to the list
  212. for x in range(0,Xrange):
  213.     for y in range(0,Yrange):
  214.         picture_p1 = Picture(pictures1_box, grid=[x,y])
  215.         pictures_p1.append(picture_p1)
  216.         button_p1 = PushButton(buttons1_box, grid=[x,y])
  217.         buttons_p1.append(button_p1)
  218.        
  219.         picture_p2 = Picture(pictures2_box, grid=[x,y])
  220.         pictures_p2.append(picture_p2)
  221.         button_p2 = PushButton(buttons2_box, grid=[x,y])
  222.         buttons_p2.append(button_p2)
  223.    
  224. new_try(1)
  225. new_try(2)
  226.  
  227. buttons1_box.enabled=False
  228. buttons2_box.enabled=False
  229. app.display()
Advertisement
Add Comment
Please, Sign In to add comment