Advertisement
Guest User

Emoji matching game - Troy Martin

a guest
Aug 21st, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.71 KB | None | 0 0
  1. import os
  2. from random import shuffle
  3. from guizero import App, Box, Picture, PushButton, Text, warn
  4. from random import randint
  5.  
  6. # fields
  7. gameTimer = 20
  8. rounds = 0
  9. correct_guesses = 0
  10.  
  11. # timer countdown - ends game when time runs out
  12. def counter():
  13.     timer.value = int(timer.value) - 1
  14.     if int(timer.value) == 0:
  15.         # stops timer and shows game over
  16.         timer.cancel(counter)
  17.         result.text_color = "black"
  18.         result.value = "Game Over"
  19.         warn("Time Over", "You have ran out of time")
  20.         # resets timer/text/score/correct guesses, restarts game, and starts new timer
  21.         timer.value = gameTimer
  22.         result.value = ""
  23.         setup_round()
  24.         timer.repeat(1000, counter)
  25.         score.value = "0"
  26.         global correct_guesses
  27.         correct_guesses = 0
  28.  
  29. # populates list of emojis and shuffles them
  30. def emoji_list():
  31.     global emojis
  32.     emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  33.     shuffle(emojis)
  34.  
  35.  
  36. # sets result/score values if correct answer or not and creates a new round
  37. def match_emoji(matched):
  38.     global correct_guesses
  39.     if matched:
  40.         result.text_color = "green"
  41.         result.value = "correct"
  42.         score.value = int(score.value) + 1
  43.         # bonus point for 3 correct guesses in a row
  44.         correct_guesses += 1
  45.         if correct_guesses == 3:
  46.             score.value = int(score.value) + 1
  47.             score_bonus.value = "BONUS - 3 correct in a row!"
  48.     else:
  49.         result.text_color = "red"
  50.         result.value = "incorrect"
  51.         score.value = int(score.value) - 1
  52.         correct_guesses = 0
  53.     setup_round()
  54.  
  55.  
  56. # sets a round up
  57. def setup_round():
  58.     # makes a list of emojis
  59.     emoji_list()
  60.  
  61.     # sets pictures for each grid
  62.     for picture in pictures:
  63.         picture.image = emojis.pop()
  64.  
  65.     for button in buttons:
  66.         button.image = emojis.pop()
  67.         button.update_command(match_emoji, [False])
  68.  
  69.     # choses a new emoji to be matched and sets to a random cell in each grid
  70.     matched_emoji = emojis.pop()
  71.  
  72.     random_picture = randint(0,8)
  73.     pictures[random_picture].image = matched_emoji
  74.  
  75.     random_button = randint(0,8)
  76.     buttons[random_button].image = matched_emoji
  77.  
  78.     # sets the correct button to report true (the matched cell)
  79.     buttons[random_button].update_command(match_emoji, [True])
  80.  
  81.     # adds to round played
  82.     global rounds
  83.     rounds += 1
  84.     rounds_played.value = "Rounds played: " + str(rounds)
  85.  
  86.  
  87. # sets up widgets
  88. app = App("emoji match", width=320, height=570)
  89. rounds_played = Text(app, text="Rounds played: " + str(rounds))
  90. result = Text(app)
  91. game_box = Box(app)
  92. pictures_box = Box(game_box, layout="grid")
  93. buttons_box = Box(game_box, layout="grid")
  94.  
  95. # setup for emoji directory and creates empty list to populate
  96. emojis_dir = "emojis"
  97. emojis = []
  98.  
  99. # creates the two grids using a list
  100. pictures = []
  101. buttons = []
  102.  
  103. for x in range(0,3):
  104.     for y in range(0,3):
  105.         picture = Picture(pictures_box, grid=[x,y])
  106.         pictures.append(picture)
  107.  
  108.         button = PushButton(buttons_box, grid=[x,y])
  109.         buttons.append(button)
  110.  
  111. # sets timer text
  112. extra_features = Box(app)
  113. timerlabel = Text(extra_features, text="Time Left: ", align="left")
  114. timer = Text(extra_features, text="Get Ready", align="left")
  115.  
  116. # sets score and bonus texts
  117. scoreboard = Box(app)
  118. label = Text(scoreboard, text="Score: ", align="left")
  119. score = Text(scoreboard, text="0", align="left")
  120. bonus = Box(app)
  121. score_bonus = Text(bonus, text="", color="green")
  122.  
  123. # sets up a round
  124. setup_round()
  125.  
  126. # starts timer
  127. timer.value = gameTimer
  128. timer.repeat(1000, counter)
  129.  
  130. # starts app
  131. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement