Advertisement
bensimmo

Untitled

Feb 9th, 2021
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.39 KB | None | 0 0
  1. import os
  2. from random import shuffle, randint
  3. from guizero import App, Box, PushButton, Picture, Text
  4. import time
  5.  
  6. def destroy_grids():
  7.     # Clear images
  8.     pic_images.clear()
  9.     pic_buttons.clear()
  10.     # Remove each widget in the boxes, this allows the ability to alter the size for next game.
  11.     for widget in top_emoji_box.children:
  12.         widget.destroy()
  13.     for widget in bottom_emoji_box.children:
  14.         widget.destroy()
  15.  
  16.  
  17. def play_matched():
  18.     for widget in bottom_emoji_box.children:
  19.         widget.disable()
  20.     button_play_game.enable()
  21.     text_timer.value = f"Time taken {round(time.time() - float(text_timer.value), 1)}s"
  22.     text_timer.show()
  23.  
  24. def setup_grids(width, height):
  25.      # Generate grids, mirrored in each box.
  26.     for x in range(width):
  27.         for y in range(height):
  28.             pic_image = Picture(top_emoji_box, grid=[x,y])
  29.             pic_button = PushButton(bottom_emoji_box, command=match_emoji, args=[False], grid=[x,y])
  30.             pic_images.append(pic_image)        
  31.             pic_buttons.append(pic_button)
  32.        
  33. def setup_round(width, height):
  34.      # set images and buttons to have a picture, remove from the list as we go.
  35.     for pic_image in pic_images:
  36.         pic_image.image = emojis.pop()
  37.        
  38.     for pic_button in pic_buttons:
  39.         pic_button.image = emojis.pop()
  40.  
  41.      # Set a button to match a picture and indicate this with True.
  42.     match_button = randint(0, width*height - 1)
  43.     pic_buttons[match_button].image = pic_images[randint(0, width*height - 1)].image
  44.     pic_buttons[match_button].update_command(match_emoji, [True])
  45.  
  46. def match_emoji(matched):
  47.     text_total_tries.value = int(text_total_tries.value) + 1
  48.     if matched:
  49.         text_result.value = "correct"
  50.         text_total_correct.value = int(text_total_correct.value) + 1
  51.         play_matched()
  52.     else:
  53.        
  54.         text_result.value = "incorrect"
  55.        
  56. def the_game():
  57.     button_play_game.disable()
  58.      # Game grid dimensions
  59.     width = randint(3,6)
  60.     height = randint(3,4)
  61.      # need to remove all widgets, before we recreate the,
  62.     destroy_grids()
  63.      # Generate Grids
  64.     setup_grids(width, height)
  65.     setup_round(width, height)
  66.     text_timer.value = time.time()
  67.     text_timer.hide()
  68.  
  69.  
  70.  # set the path to the emoji folder on your computer
  71.  # and create a shuffled list of the locations of the emoji images
  72. emojis_dir = "emojis"
  73. emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  74. shuffle(emojis)
  75.  
  76.  # picture object storage
  77. pic_images = []
  78. pic_buttons = []
  79.  
  80.  # The App window
  81. app=App("Emoji Match'em Game", width="490", height="750")
  82.  
  83.  # Create the boxes
  84. top_emoji_box = Box(app, layout="grid", align="top", width=480, height=330)
  85. control_box = Box(app, layout="grid")
  86. bottom_emoji_box = Box(app, layout="grid", align="bottom", width=480, height=320)
  87.  
  88. button_play_game = PushButton(control_box, text="Start Game", command=the_game, grid=[1,0])
  89. text_result = Text(control_box, grid=[2,0])
  90. text_timer = Text(control_box, visible = False, grid=[3,0])
  91. text_total_tries_text = Text(control_box, text = "Tries", grid=[2,1])
  92. text_total_tries = Text(control_box, text = "0", grid=[3,1])
  93. text_total_correct_text = Text(control_box, text = "Correct Tries", grid=[2,2])
  94. text_total_correct = Text(control_box, text = "0", grid=[3,2])
  95.  
  96.  
  97. app.display()
  98.  
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement