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, info, CheckBox
- def match_emoji(matched):
- if matched:
- result.value = "Correct!!!"
- emojis=emoji_list()
- new_try()
- score.value = int(score.value) + 1
- row.value = int(row.value) + 1
- if int(row.value)==3:
- if bonustime.value:
- timer.value=int(timer.value)+10
- info("info","Well done, Extra time!!!")
- else:
- score.value = int(score.value) + 1
- info("info","Well done, bonus point!!!")
- row.value="0"
- else:
- result.value = "Incorrect"
- score.value = int(score.value) - 1
- row.value="0"
- # set the path to the emoji folder on your computer
- emojis_dir = "C:\\Users\\xavi.cano\\Code\\emojis\\emojis"
- def emoji_list():
- # create a list of the locations of the emoji images
- 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)
- return emojis
- Xrange=3
- Yrange=3
- buttons = []
- pictures = []
- def new_try():
- ## for each picture in the list
- for picture in pictures:
- ## make the picture a random emoji
- picture.image = emojis.pop()
- for button in buttons:
- # make the image feature of the PushButton a random emoji
- button.image = emojis.pop()
- # set the command to be called and pass False, as these emoji wont be the matching ones
- button.update_command(match_emoji, [False])
- random_button = randint(0,8)
- random_picture = randint(0,8)
- buttons[random_button].image=pictures[random_picture].image
- buttons[random_button].update_command(match_emoji, [True])
- def button_sec():
- if buttonsec.bg == "black":
- buttonsec.bg = "white"
- buttonsec.text_color = "black"
- else:
- buttonsec.bg = "black"
- buttonsec.text_color = "white"
- def decrease():
- counter=int(timer.value)-1
- button_sec()
- if int(rounds.value)==5:
- info("info","Go home!!! Too many tries")
- if counter==0:
- timer.value="30"
- score.value = "0"
- rounds.value=str(int(rounds.value)+1)
- else:
- timer.value = str(counter)
- emojis=[]
- app = App("emoji match")
- # create a box to house the grids
- game_box = Box(app)
- # create a box to house the pictures
- pictures_box = Box(game_box, layout="grid", align="left")
- # create a box to house the buttons
- buttons_box = Box(game_box, layout="grid", align="right")
- command_box= Box(app)
- scoreboard = Box(app)
- label = Text(scoreboard, text="Score : ", align="left")
- score = Text(scoreboard, text="0", align="left")
- label2 = Text(scoreboard, text="In a row : ", align="left")
- row=Text(scoreboard, text="0", align="left")
- score.value = "0"
- result = Text(app)
- timer=Text(command_box, text="30", align="left")
- buttonsec=PushButton(command_box,text=" seconds", align="left")
- buttonsec.bg="black"
- buttonsec.text_color = "white"
- Label_rounds=Text(command_box, text="Rounds : ", align="left")
- rounds=Text(command_box, text="1", align="left")
- bonustime = CheckBox(command_box, text="Bonus Time")
- emojis=emoji_list()
- timer.repeat(1000, decrease)
- # create 9 PushButtons with a different grid coordinate and add to the list
- for x in range(0,Xrange):
- for y in range(0,Yrange):
- # put the pictures and buttons into the lists
- picture = Picture(pictures_box, grid=[x,y])
- pictures.append(picture)
- button = PushButton(buttons_box, grid=[x,y])
- buttons.append(button)
- new_try()
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement