maroph

emojis_game2.py

Dec 13th, 2019
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.75 KB | None | 0 0
  1. from guizero import App, Box, MenuBar, Picture, PushButton, Text
  2. import os
  3. from random import randint
  4. from random import shuffle
  5.  
  6.  
  7. no_success = 0
  8. no_failures = 0
  9.  
  10.  
  11. def setup_round():
  12.     global emojis_save
  13.     global pictures
  14.     global buttons
  15.  
  16.     emojis = emojis_save.copy()
  17.     shuffle(emojis)
  18.  
  19.     for idx in range(0, len(pictures)):
  20.         pictures[idx].visible = False
  21.         buttons[idx].visible = False
  22.         buttons[idx].update_command(match_emoji, [False])
  23.  
  24.     # select the matching emoji
  25.     matched_emoji = emojis.pop()
  26.     # select a random picture index for this emoji
  27.     idx_picture = randint(0, 8)
  28.     # select a random button index for this emoji
  29.     idx_button = randint(0, 8)
  30.  
  31.     # set the chosen picture's image to the matched_emoji
  32.     pictures[idx_picture].image = matched_emoji
  33.     pictures[idx_picture].visible = True
  34.     # set the chosen button's image to the matched_emoji
  35.     buttons[idx_button].image = matched_emoji
  36.     buttons[idx_button].visible = True
  37.     # set the command to be called and pass True, as this is the matching emoji
  38.     buttons[idx_button].update_command(match_emoji, [True])
  39.  
  40.     # set all image properties to an emoji
  41.     # if the related visible property is False
  42.     for idx in range(0, len(pictures)):
  43.         if not pictures[idx].visible:
  44.             pictures[idx].image = emojis.pop()
  45.             pictures[idx].visible = True
  46.         if not buttons[idx].visible:
  47.             buttons[idx].image = emojis.pop()
  48.             buttons[idx].visible = True
  49.  
  50.  
  51. def match_emoji(matched):
  52.     global no_success
  53.     global no_failures
  54.  
  55.     if matched:
  56.         no_success = no_success + 1
  57.         result.value = 'correct (' + str(no_success) + '/' + str(no_failures) + ')'
  58.     else:
  59.         no_failures = no_failures + 1
  60.         result.value = 'incorrect (' + str(no_success) + '/' + str(no_failures) + ')'
  61.  
  62.     setup_round()
  63.  
  64.  
  65. def exit_app():
  66.     global app
  67.     app.destroy()
  68.  
  69.  
  70. def about_app():
  71.     global app
  72.     app.info('About', "The Emoji Matching Game")
  73.  
  74.  
  75. # set the path to the emoji folder on your computer
  76. emojis_dir = "emojis"
  77.  
  78. # create a list of the locations of the emoji images
  79. # in each round we copy this data to the emojis array
  80. emojis_save = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  81.  
  82.  
  83. app = App("Emoji Match Game")
  84. app.when_closed = exit_app
  85.  
  86. menubar = MenuBar(app,
  87.                   # menu bar
  88.                   toplevel=["File", "Help"],
  89.                   # menu items
  90.                   options=[
  91.                       # File
  92.                       [
  93.                           # items in File
  94.                           ["Exit", exit_app]
  95.                       ],
  96.                       # Help
  97.                       [
  98.                           # items in Help
  99.                           ["About", about_app]
  100.                       ]
  101.                   ])
  102.  
  103. result = Text(app)
  104.  
  105. game_box = Box(app, border=True)
  106. pictures_box = Box(game_box, layout="grid", align="left")
  107. buttons_box = Box(game_box, layout="grid", border="3", align="left")
  108.  
  109. # create empty lists to which pictures and buttons will be added
  110. # we use the property visible to distinguish between set and not
  111. pictures = []
  112. buttons = []
  113. for x in range(0, 3):
  114.     for y in range(0, 3):
  115.         # put the pictures into the list
  116.         picture = Picture(pictures_box, grid=[x,y])
  117.         pictures.append(picture)
  118.         # put the buttons into the list
  119.         button = PushButton(buttons_box, grid=[x, y])
  120.         # set the command to be called and pass False, as these emoji wont be the matching ones
  121.         buttons.append(button)
  122.  
  123.  
  124. setup_round()
  125.  
  126. # now all image properties are set and we can display the GUI
  127. app.display()
Advertisement
Add Comment
Please, Sign In to add comment