Advertisement
xavicano

Untitled

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