johnpentyrch

GUI3p11z

May 11th, 2020
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.96 KB | None | 0 0
  1. import os
  2. from random import shuffle, randint
  3. from guizero import App, Box, Picture,Text, PushButton,warn,info
  4. no_count=False
  5. # set the path to the emoji folder on your computer
  6. emojis_dir = "emojis"
  7. # create a list of the locations of the emoji images. This is never altered once set up
  8. pemojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  9. emojis=[]
  10. def make_emojis():
  11.     global emojis
  12.     #Copy emoji paths to a temporary variable which is destroyed during the running of the game
  13.     #Copy data and not pointer
  14.     emojis=pemojis[:]
  15.     ###print(emojis)
  16.     # shuffle the emojis
  17.     shuffle(emojis)
  18.     ###print(emojis)
  19.  
  20. app = App("emoji match")
  21.  
  22. result = Text(app)
  23. attempts=0
  24. score_total =0
  25.  
  26. def counter():
  27.     global attempts, score_total,no_count
  28.     if no_count: return
  29.     timer.value = int(timer.value) - 1
  30.     if int(timer.value) == 0:
  31.         timer.cancel(counter)
  32.         # reset the timer
  33.         result.value = "Game Over"
  34.         txt.text_color='green'
  35.         txt.value = "FINAL SCORE You have " +str(score_total)
  36.         warn("Time Out", "you've run out of time")
  37.         #reset score counters
  38.         attempts = 0
  39.         score_total=0
  40.         txt.value = "Start"
  41.         # reset timer
  42.         timer.value = "20"
  43.         # reset result
  44.         result.value = ""
  45.         # start new round
  46.         new_round()
  47.         #restart timer
  48.         timer.repeat(1000, counter)
  49.  
  50.  
  51. def match_emoji(matched):
  52.     global attempts, score_total,no_count
  53.     if matched:
  54.         attempts+=1
  55.         if attempts>2:
  56.             attempts=0
  57.             score_total+=10
  58.             no_count=True
  59.             info("Congratulations", "You just got a 10 point bonus and 10 extra seconds")
  60.             timer.value=str(10 +int(timer.value))
  61.             no_count=False
  62.         score_total+=2
  63.         txt.value = "CORRECT You have " +str(score_total) + ' score'
  64.         txt.text_color='black'
  65.     else:
  66.         score_total-=1
  67.         txt.value = "INCORRECT You have " +str(score_total) + ' score'
  68.         txt.text_color='red'
  69.     new_round()
  70.    
  71. txt=Text(app, text='Click on the matching Emoji in the right grid. Good Luck  ',align='top')
  72.  
  73.  
  74. # create a box to house the grid
  75. pictures_box = Box(app, layout="grid",align='left', border=3)
  76. buttons_box = Box(app, layout="grid", align='right', border=3)
  77.  
  78. # add in the extra features
  79. extra_features = Box(app)
  80. timer = Text(extra_features, text="Get Ready")
  81.  
  82.  
  83. # create an empty list to which pictures and one for buttons will be added
  84. pictures = []
  85. buttons=[]
  86.  
  87. for x in range(0,3):
  88.     for y in range(0,3):
  89.         # put the pictures into the list
  90.         picture = Picture(pictures_box, grid=[x,y])
  91.         pictures.append(picture)      
  92.         button = PushButton(buttons_box, grid=[x,y])
  93.         buttons.append(button)
  94.  
  95. def new_round():
  96.     global attempts, score_total, emojis
  97.     #Check if we have enough emoji to carry on; if not make another set
  98.     if len(emojis)<17:
  99.         make_emojis()
  100.  
  101.     match_number=randint(0,8)# pick random emoji place for match picture
  102.     pnum=0
  103.     match_pic=None
  104.     # for each picture in the list but save a random one for match
  105.     for picture in pictures:
  106.         picture.image = emojis.pop()
  107.         if pnum ==match_number:
  108.             match_pic=picture.image #save selected matching emoji
  109.         pnum+=1
  110.        
  111.     match_dup=randint(0,8) #pick random place to put matching pictue
  112.     pnum=0
  113.     #Now same for buttons
  114.     for button in buttons:
  115.         if pnum==match_dup:
  116.             button.image=match_pic
  117.             button.update_command(match_emoji, [True])
  118.         else:
  119.             button.image = emojis.pop()
  120.             button.update_command(match_emoji, [False])
  121.         pnum+=1
  122.     # set the command to be called and pass False, as these emoji wont be the matching ones
  123.        
  124. new_round()
  125. # start the timer
  126. timer.value = 20
  127. timer.repeat(1000, counter)
  128.  
  129. app.display()
Add Comment
Please, Sign In to add comment