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 = 3
- score = 0
- TimeLeft = 30
- emojis_dir = "emojis"
- emojis = []
- buttons = []
- pictures = []
- # Event Handlers
- def imageClick(matched):
- global score
- if matched:
- lblResult.value = "Well done! 5 seconds gained!"
- score += 1
- txtTimeLeft.value = int(txtTimeLeft.value) + 5
- lblScore.value = f"Score: {score}"
- setupRound()
- else:
- lblResult.value = f"Incorrect! You have lost 2 seconds!"
- txtTimeLeft.value = int(txtTimeLeft.value) - 2
- def btnInfoClicked():
- 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.')
- # 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)
- # for each picture and button in the list assign an emoji to its image feature
- for button in pictures:
- button.image = emojis.pop()
- button.update_command(imageClick, [False])
- for button in buttons:
- button.image = emojis.pop()
- button.update_command(imageClick, [False])
- # Select new image and place in both grids
- newImage = emojis.pop()
- pos = randint(0,len(buttons)-1)
- buttons[pos].image = newImage
- buttons[pos].update_command(imageClick, [True])
- pos = randint(0,len(pictures)-1)
- pictures[pos].image = newImage
- pictures[pos].update_command(imageClick, [True])
- def decreaseTime():
- global score
- txtTimeLeft.value = int(txtTimeLeft.value) - 1
- if txtTimeLeft.value == '0':
- txtTimeLeft.cancel(decreaseTime)
- if yesno('OUT OF TIME',f'Game over. \nYour final score was {score}.\nPlay again?',app) == False:
- app.destroy()
- else:
- setupRound()
- txtTimeLeft.value = TimeLeft
- txtTimeLeft.repeat(1000,decreaseTime)
- score = 0
- lblScore.value = f"Score: {score}"
- # .......................................................................................
- # setup the app
- app = App("emoji match", width = GRID_SIZE*200, height = 100+GRID_SIZE*100, bg = 'white')
- # create a box for information
- infoBox = Box(app, width = 'fill', align = 'top', height = 30)
- infoBox.bg = 'black'
- lblInstructions = PushButton(infoBox, command = btnInfoClicked, text = 'Information', width = 20, height = 'fill')
- lblInstructions.text_color = 'white'
- # 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 image!',height = 3)
- # create a box to house the pictures
- picturesBox = Box(gameBox, layout="grid",align='left')
- # create a box to house the buttons
- buttonsBox = Box(gameBox, layout="grid",align='left')
- #create a box for feedback
- resultBox = Box(app, width = "fill", height = 50,align = 'top')
- resultBox.bg = 'light blue'
- lblResult = Text(resultBox,text = '', height = 'fill', color = 'red', size = 12, align = 'left')
- txtTimeLeft = TextBox(resultBox, text = '', align = 'right', height = 'fill')
- lblTime = Text(resultBox, text='Time:',align = 'right', height = 'fill')
- txtTimeLeft.text_size = 12
- # 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)
- button = PushButton(buttonsBox, grid=[x,y])
- buttons.append(button)
- # call round setup
- setupRound()
- txtTimeLeft.value = TimeLeft
- txtTimeLeft.repeat(1000,decreaseTime)
- # Display app
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement