Advertisement
xavicano

Untitled

Aug 20th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 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.  
  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 int(rounds.value)==5:
  60.         warn("Go home!!!", "Too many tries")
  61.     if counter==0:
  62.         timer.value="20"
  63.         rounds.value=str(int(rounds.value)+1)
  64.     else:
  65.         timer.value = str(counter)
  66.        
  67.  
  68. emojis=[]
  69.  
  70. app = App("emoji match")
  71.  
  72.  
  73. # create a box to house the grids
  74. game_box = Box(app)
  75. # create a box to house the pictures
  76. pictures_box = Box(game_box, layout="grid", align="left")
  77. # create a box to house the buttons
  78. buttons_box = Box(game_box, layout="grid", align="right")
  79.  
  80.  
  81. result = Text(app)
  82.  
  83. timer=Text(app, text="20")
  84. buttonsec=PushButton(app,text=" seconds")
  85. buttonsec.bg="black"
  86. buttonsec.text_color = "white"
  87. rounds=Text(app, text="1")
  88.  
  89. emojis=emoji_list()
  90. timer.repeat(1000, decrease)
  91.  
  92.  
  93.  
  94. # create 9 PushButtons with a different grid coordinate and add to the list
  95. for x in range(0,Xrange):
  96.     for y in range(0,Yrange):
  97.         # put the pictures and buttons into the lists
  98.         picture = Picture(pictures_box, grid=[x,y])
  99.         pictures.append(picture)
  100.  
  101.         button = PushButton(buttons_box, grid=[x,y])
  102.         buttons.append(button)
  103.  
  104. new_try()
  105. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement