Advertisement
sclnl

Untitled

Jan 27th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.17 KB | None | 0 0
  1. import os
  2. from random import shuffle, randint
  3. from guizero import App, Box, Picture, PushButton, Text, warn
  4.  
  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
  8. emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  9. # shuffle the emojis
  10. shuffle(emojis)
  11.  
  12. # setup the app
  13. app = App("emoji match", height=300)
  14.  
  15. global player
  16. player=1
  17.  
  18. # create a control box to house commands
  19. control_box = Box(app, layout="auto")
  20.  
  21. timer = Text(control_box, text="Time", width=4, align="left" )
  22.  
  23. points_lab1 = Text(control_box, text="RED ", bg="white", color="red", width=4, align="left" )
  24. points1 = Text(control_box, text="0", width=4, align="left" )
  25.  
  26. points_lab2 = Text(control_box, text="BLUE", bg="white", color="blue", width=4, align="left" )
  27. points2 = Text(control_box, text="0", width=4, align="left" )
  28.  
  29. player_label = Text(control_box, text="player is red ", width=14, align="left" )
  30.  
  31. # create a box to house the grids
  32. game_box = Box(app)
  33. # create a box to house the pictures
  34. pictures_box = Box(game_box, layout="grid", align="left")
  35. # create a box to house the buttons
  36. buttons_box = Box(game_box, layout="grid", align="right")
  37.  
  38. def match_emoji(matched):
  39.     global player
  40.    
  41.     timer.cancel(counter)
  42.    
  43.     if matched:
  44.         if (player == 1):
  45.             points1.value = int(points1.value) + 1
  46.         else:
  47.             points2.value = int(points2.value) + 1
  48.     else:
  49.         if (player == 1):
  50.             points1.value = int(points1.value) - 1
  51.         else:
  52.             points2.value = int(points2.value) - 1
  53.    
  54.     if (player == 1):
  55.         player_label.value="player is blue"
  56.         player = 2
  57.     else:
  58.         player_label.value="player is red "
  59.         player = 1
  60.    
  61.     setup_round()
  62.     timer.value = 30
  63.     timer.repeat(1000, counter)
  64.  
  65. def setup_round():
  66.     # for each picture and button in the list assign an emoji to its image feature
  67.     for picture in pictures:
  68.         # make the picture a random emoji
  69.         picture.image = emojis.pop()
  70.  
  71.     for button in buttons:
  72.         # make the image feature of the PushButton a random emoji
  73.         button.image = emojis.pop()
  74.         # set the command to be called and pass False, as these emoji wont be the matching ones
  75.         button.update_command(match_emoji, [False])
  76.  
  77.     # choose a new emoji
  78.     matched_emoji = emojis.pop()
  79.  
  80.     # select a number at random
  81.     random_picture = randint(0,8)
  82.     # change the image feature of the Picture with this index in the list of pictures to the new emoji
  83.     pictures[random_picture].image = matched_emoji
  84.  
  85.     random_button = randint(0,8)
  86.     # change the image feature of the PushButton with this index in the list of buttons to the new emoji
  87.     buttons[random_button].image = matched_emoji
  88.     # set the command to be called and pass True, as this is the matching emoji
  89.     buttons[random_button].update_command(match_emoji, [True])
  90.  
  91. # create the an empty lists to add the buttons and pictures to
  92. buttons = []
  93. pictures = []
  94.  
  95. # create 9 PushButtons with a different grid coordinate and add to the list
  96. for x in range(0,3):
  97.     for y in range(0,3):
  98.         # put the pictures and buttons into the lists
  99.         picture = Picture(pictures_box, grid=[x,y])
  100.         pictures.append(picture)
  101.        
  102.         button = PushButton(buttons_box, grid=[x,y])
  103.         buttons.append(button)
  104.  
  105. setup_round()
  106.  
  107. def counter():
  108.     global player
  109.     timer.value = int(timer.value) - 1
  110.     if (int(timer.value) == 0):
  111.         # reset the timer
  112.         timer.cancel(counter)
  113.         # warning
  114.         if (int(timer.value) == 0):
  115.             warn("Time Out", "you've run out of time and lost your points")
  116.         # reset points
  117.         if (player == 1):
  118.             points1.value = 0
  119.             player = 2
  120.         else:
  121.             points2.value = 0
  122.             player = 1
  123.         # reset timer
  124.         timer.value = "30"
  125.         # start new round
  126.         setup_round()
  127.         #restart timer
  128.         timer.repeat(1000, counter)
  129.  
  130. # start the timer
  131. timer.value = 30
  132. timer.repeat(1000, counter)
  133.  
  134. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement