JAWarr

Emoji match with countdown

May 15th, 2022
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | None | 0 0
  1. # Emoji Match game challenge - chosen from list with minutes and seconds countdown
  2. import os, random
  3. from random import shuffle, randint
  4. from guizero import App, Box, Picture, PushButton, Text
  5. from time import sleep
  6.  
  7. def timer():
  8.         seconds.value = int(seconds.value) - 1
  9.         if int (seconds.value) == 0:
  10.             seconds.value = 60
  11.             minutes.value = int(minutes.value) - 1
  12.             if int (minutes.value) < 0:
  13.                 seconds.cancel(timer)
  14.                 seconds.value = 0
  15.                 minutes.value = ("Game Over")
  16.            
  17.  
  18. def match_emoji(matched):
  19.     if matched:
  20.         result.value = "correct"
  21.     else:
  22.         result.value = "incorrect"
  23.  
  24. # set the path to the emoji folder
  25. emojis_dir = "emojis"
  26. # create a list of the locations of the emoji images
  27. emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  28. # shuffle the emojis
  29. shuffle(emojis)
  30.  
  31. app = App("emoji match")
  32. #create the timer text
  33. info_box= Box(app)
  34.  
  35. score = Text(info_box, text="Time: ", align="left")
  36. seconds = Text(info_box, text = "60", align="right")
  37. seconds.repeat(1000, timer)
  38. minutes = Text(info_box, text = "1", align="right")
  39.  
  40. # create a box to house the grid
  41. game_box= Box(app)
  42. pictures_box = Box(app, layout="grid")
  43. buttons_box = Box(game_box, layout="grid")
  44. result = Text(app)
  45.  
  46. matched = emojis.pop()
  47. pics = []
  48. for i in range(8):
  49.     pics.append(emojis.pop())
  50. pics.append(matched)
  51. shuffle(pics)
  52.  
  53. butts = []
  54. for i in range(8):
  55.     butts.append(emojis.pop())
  56. butts.append(matched)
  57.  
  58. # create an empty list to which pictures will be added
  59. pictures=[]
  60. for x in range(0,3):
  61.     for y in range(0,3):
  62.         # put the pictures into the list
  63.         picture = Picture(pictures_box, grid=[x,y])
  64.         pictures.append(picture)
  65.         picture.image = pics.pop()
  66.  
  67. # create an empty list to which buttons will be added
  68. buttons = []
  69. for x in range(0,3):
  70.     for y in range(0,3):
  71.         # put the pictures into the list
  72.         button = PushButton(buttons_box, grid=[x,y])
  73.         buttons.append(button)
  74.         button.image = butts.pop()
  75.         button.update_command(match_emoji, [False])
  76.  
  77.  
  78. app.display()
  79.  
  80.  
Add Comment
Please, Sign In to add comment