Advertisement
kalpin

Match Game

Aug 7th, 2020 (edited)
1,161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.36 KB | None | 0 0
  1. import os
  2. from random import shuffle, randint
  3. from guizero import App, Box, Picture, PushButton, Text, TextBox, info, yesno
  4.  
  5. # global variables
  6. GRID_SIZE   = 3
  7. score       = 0
  8. TimeLeft    = 30
  9. emojis_dir  = "emojis"
  10. emojis      = []
  11. buttons     = []
  12. pictures    = []
  13.  
  14. # Event Handlers
  15.  
  16. def imageClick(matched):
  17.     global score
  18.     if matched:
  19.         lblResult.value = "Well done! 5 seconds gained!"
  20.         score += 1
  21.         txtTimeLeft.value = int(txtTimeLeft.value) + 5
  22.         lblScore.value = f"Score: {score}"
  23.         setupRound()
  24.     else:  
  25.         lblResult.value = f"Incorrect! You have lost 2 seconds!"
  26.         txtTimeLeft.value = int(txtTimeLeft.value) - 2
  27.        
  28. def btnInfoClicked():
  29.     info('HELP','There is exactly one pair of matching images. \nClick on one of the matching pairs. \nEvery incorrect click loses 2 seconds.\nEvery match scores 1 point plus gain 5 seconds of time.')
  30.        
  31. # Helper Functions
  32.  
  33. def setupRound():
  34.     global buttons, pictures, emojis, score
  35.    
  36.     # load emoji filenames
  37.     emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  38.     # shuffle the emojis
  39.     shuffle(emojis)
  40.  
  41.     # for each picture and button in the list assign an emoji to its image feature
  42.     for button in pictures:
  43.         button.image = emojis.pop()
  44.         button.update_command(imageClick, [False])
  45.        
  46.     for button in buttons:
  47.         button.image = emojis.pop()
  48.         button.update_command(imageClick, [False])
  49.  
  50.     # Select new image and place in both grids
  51.     newImage = emojis.pop()
  52.    
  53.     pos = randint(0,len(buttons)-1)
  54.     buttons[pos].image = newImage
  55.     buttons[pos].update_command(imageClick, [True])
  56.    
  57.     pos = randint(0,len(pictures)-1)
  58.     pictures[pos].image = newImage
  59.     pictures[pos].update_command(imageClick, [True])
  60.        
  61. def decreaseTime():
  62.     global score
  63.     txtTimeLeft.value = int(txtTimeLeft.value) - 1
  64.     if txtTimeLeft.value == '0':
  65.         txtTimeLeft.cancel(decreaseTime)
  66.         if yesno('OUT OF TIME',f'Game over. \nYour final score was {score}.\nPlay again?',app) == False:
  67.             app.destroy()
  68.         else:
  69.             setupRound()
  70.             txtTimeLeft.value = TimeLeft
  71.             txtTimeLeft.repeat(1000,decreaseTime)
  72.             score = 0
  73.             lblScore.value = f"Score: {score}"
  74.    
  75. # .......................................................................................  
  76.  
  77. # setup the app
  78. app = App("emoji match", width = GRID_SIZE*200, height = 100+GRID_SIZE*100, bg = 'white')
  79.  
  80. # create a box for information
  81. infoBox = Box(app, width = 'fill', align = 'top', height = 30)
  82. infoBox.bg = 'black'
  83. lblInstructions = PushButton(infoBox, command = btnInfoClicked, text = 'Information', width = 20, height = 'fill')
  84. lblInstructions.text_color = 'white'
  85.  
  86. # create a box to house the grids
  87. gameBox = Box(app)
  88. space = Box(gameBox, width = "fill", height = 60, align='top')
  89. lblInstruct = Text(space, text = 'Find the matching image!',height = 3)
  90.  
  91. # create a box to house the pictures
  92. picturesBox = Box(gameBox, layout="grid",align='left')
  93.  
  94. # create a box to house the buttons
  95. buttonsBox = Box(gameBox, layout="grid",align='left')
  96.  
  97. #create a box for feedback
  98. resultBox = Box(app, width = "fill", height = 50,align = 'top')
  99. resultBox.bg = 'light blue'
  100. lblResult = Text(resultBox,text = '', height = 'fill', color = 'red', size = 12, align = 'left')
  101. txtTimeLeft = TextBox(resultBox, text = '', align = 'right', height = 'fill')
  102. lblTime = Text(resultBox, text='Time:',align = 'right', height = 'fill')
  103. txtTimeLeft.text_size = 12
  104.  
  105. # create a box for score
  106. scoreBox = Box(app, width = 'fill', height = 50, align='bottom')
  107. lblScore = Text(scoreBox, text = '', width = 'fill', height = 'fill', color = 'blue')
  108. lblScore.bg = 'light yellow'
  109. lblScore.value = f"Score: {score}"
  110.  
  111. # create PushButtons with a different grid coordinate and add to the list
  112. for x in range(0,GRID_SIZE):
  113.     for y in range(0,GRID_SIZE):
  114.         # put the pictures and buttons into the lists
  115.         button = PushButton(picturesBox, grid=[x,y])
  116.         pictures.append(button)
  117.  
  118.         button = PushButton(buttonsBox, grid=[x,y])
  119.         buttons.append(button)
  120.  
  121. # call round setup
  122. setupRound()
  123. txtTimeLeft.value = TimeLeft
  124. txtTimeLeft.repeat(1000,decreaseTime)
  125.                    
  126. # Display app
  127. app.display()
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement