Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from random import shuffle, randint
- from guizero import App, Box, Picture, PushButton, Text, TextBox, info, yesno
- # global variables
- GRID_SIZE = 6 # must be even number
- score = 0
- TimeLeft = 30
- emojis_dir = "emojis"
- emojis = []
- pictures = []
- buttons = []
- TIME_BETWEEN = 1000
- # Event Handlers
- def btnClicked(button,filename):
- global buttons, score
- btn1.clicks += 1
- button.image = filename
- buttons.append(button)
- if btn1.clicks == 2:
- if buttons[0].image == buttons[1].image: # match found
- score += 1
- lblScore.value = f"Score: {score}"
- btn1.clicks = 0
- buttons = []
- else:
- btn1.timeLeft = TIME_BETWEEN
- btn1.repeat(100,myTimer)
- def myTimer():
- global buttons
- btn1.timeLeft -= 100
- if btn1.timeLeft <= 0:
- btn1.cancel(myTimer)
- for btn in buttons:
- btn.image = 'blank.png'
- buttons = []
- btn1.clicks = 0
- # Helper Functions
- def setupRound():
- global buttons, pictures, emojis, score
- # load emoji filenames
- emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
- # shuffle the emojis
- shuffle(emojis)
- # generate correct numbers of emojiis for game
- size = GRID_SIZE*GRID_SIZE
- images = ['']*size
- used = [False]*size
- for i in range(size // 2):
- image = emojis.pop()
- for j in range(2): # do this twice so each image is in the grid twice
- pos = randint(0,size-1)
- while used[pos] == True:
- pos = randint(0,size-1)
- images[pos] = image
- used[pos] = True
- # for each picture button assign blank image and set up event handler to dislay correct image
- for i in range(len(pictures)):
- pictures[i].image = 'blank.png'
- pictures[i].update_command(btnClicked, [pictures[i],images[i]])
- # .......................................................................................
- # setup the app
- app = App("emoji match", width = GRID_SIZE*100, height = GRID_SIZE*80, bg = 'white')
- btn1 = PushButton(app,text='',visible = False)
- btn1.clicks = 0
- btn1.timeLeft = TIME_BETWEEN
- # create a box to house the grids
- gameBox = Box(app)
- space = Box(gameBox, width = "fill", height = 60, align='top')
- lblInstruct = Text(space, text = 'Find the matching pairs!',height = 3)
- # create a box to house the pictures
- picturesBox = Box(gameBox, layout="grid",align='left')
- # create a box for score
- scoreBox = Box(app, width = 'fill', height = 50, align='bottom')
- lblScore = Text(scoreBox, text = '', width = 'fill', height = 'fill', color = 'blue')
- lblScore.bg = 'light yellow'
- lblScore.value = f"Score: {score}"
- # create PushButtons with a different grid coordinate and add to the list
- for x in range(0,GRID_SIZE):
- for y in range(0,GRID_SIZE):
- # put the pictures and buttons into the lists
- button = PushButton(picturesBox, grid=[x,y])
- pictures.append(button)
- # call round setup
- setupRound()
- # Display app
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement