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_p1(matched):
- if matched:
- result.value = "Correct!!!" + winning()
- new_try(1)
- score_p1.value = int(score_p1.value) + 1
- row_p1.value = int(row_p1.value) + 1
- if int(row_p1.value)==3:
- if bonustime.value:
- timer.value=int(timer.value)+10
- info("info","Well done, Extra time!!!")
- else:
- score_p1.value = int(score_p1.value) + 1
- info("info","Well done, bonus point!!!")
- row_p1.value="0"
- else:
- result.value = "Incorrect" + winning()
- score_p1.value = int(score_p1.value) - 1
- row_p1.value="0"
- rounds_p1.value=str(int(rounds_p1.value)+1)
- change_state()
- def match_emoji_p2(matched):
- if matched:
- result.value = "Correct!!!" + winning()
- emojis=emoji_list()
- new_try(2)
- score_p2.value = int(score_p2.value) + 1
- row_p2.value = int(row_p2.value) + 1
- if int(row_p2.value)==3:
- if bonustime.value:
- timer.value=int(timer.value)+10
- info("info","Well done, Extra time!!!")
- else:
- score_p2.value = int(score_p2.value) + 1
- info("info","Well done, bonus point!!!")
- row_p2.value="0"
- else:
- result.value = "Incorrect" + winning()
- score_p2.value = int(score_p2.value) - 1
- row_p2.value="0"
- rounds_p2.value=str(int(rounds_p2.value)+1)
- change_state()
- # set the path to the emoji folder on your computer
- emojis_dir = "C:\\Users\\xavi.cano\\Code\\emojis\\emojis"
- general_enable=False
- 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
- def new_try(player):
- emojis=emoji_list()
- random_button = randint(0,8)
- random_picture = randint(0,8)
- if player==1:
- ## for each picture in the list
- for picture_p1 in pictures_p1:
- ## make the picture a random emoji
- picture_p1.image = emojis.pop()
- for button_p1 in buttons_p1:
- # make the image feature of the PushButton a random emoji
- button_p1.image = emojis.pop()
- # set the command to be called and pass False, as these emoji wont be the matching ones
- button_p1.update_command(match_emoji_p1, [False])
- buttons_p1[random_button].image=pictures_p1[random_picture].image
- buttons_p1[random_button].update_command(match_emoji_p1, [True])
- else:
- ## for each picture in the list
- for picture_p2 in pictures_p2:
- ## make the picture a random emoji
- picture_p2.image = emojis.pop()
- for button_p2 in buttons_p2:
- # make the image feature of the PushButton a random emoji
- button_p2.image = emojis.pop()
- # set the command to be called and pass False, as these emoji wont be the matching ones
- button_p2.update_command(match_emoji_p2, [False])
- buttons_p2[random_button].image=pictures_p2[random_picture].image
- buttons_p2[random_button].update_command(match_emoji_p2, [True])
- def button_sec():
- global general_enable
- if general_enable==True:
- if buttonsec.bg == "black":
- buttonsec.bg = "white"
- buttonsec.text_color = "black"
- else:
- buttonsec.bg = "black"
- buttonsec.text_color = "white"
- def decrease():
- global general_enable
- if general_enable==True:
- counter=int(timer.value)-1
- button_sec()
- if counter==0:
- timer.value="30"
- change_state()
- rounds.value=str(int(rounds.value)+1)
- else:
- timer.value = str(counter)
- def button_start():
- global general_enable
- if general_enable==True:
- general_enable=False
- buttonsec.text=" Pause"
- else:
- general_enable=True
- pictures1_box.enabled=False
- change_state()
- def change_state():
- if pictures1_box.enabled==True:
- pictures2_box.enabled=True
- buttons2_box.enabled=True
- pictures1_box.enabled=False
- buttons1_box.enabled=False
- else:
- pictures2_box.enabled=False
- buttons2_box.enabled=False
- pictures1_box.enabled=True
- buttons1_box.enabled=True
- def winning():
- if int(score_p1.value)> int(score_p2.value):
- return " Player 1 Winning"
- elif int(score_p1.value)< int(score_p2.value):
- return " Player 2 Winning"
- else:
- return " Tie "
- def set_of_rules():
- build_a_snowman = app.yesno("A question...", "Do you agree the set of rules?")
- if build_a_snowman == True:
- app.info("Set of Rules", "Enjoy the game")
- else:
- app.error("Set of Rules", "Okay bye...")
- quit()
- return False
- emojis=[]
- app = App("emoji match")
- app.width=920
- app.height=380
- # create a box to house the grids
- game_box = Box(app, width="fill")
- # create a box to house the pictures
- pictures1_box = Box(game_box, layout="grid", align="left")
- # create a box to house the buttons
- buttons1_box = Box(game_box, layout="grid", align="left")
- # create a box to house the pictures
- pictures2_box = Box(game_box, layout="grid", align="left")
- # create a box to house the buttons
- buttons2_box = Box(game_box, layout="grid", align="left")
- command_box= Box(app)
- # Create a scoreBoard BOX
- scoreboard = Box(app, layout ="grid")
- label = Text(scoreboard, grid = [0,0], text="Player 1 ", align="left")
- label = Text(scoreboard, grid = [1,0],text="Score : ", align="left")
- score_p1 = Text(scoreboard, grid = [2,0],text="0", align="left")
- Label_rounds=Text(scoreboard, grid = [3,0],text=" Rounds : ", align="left")
- rounds_p1=Text(scoreboard, grid = [4,0],text="0", align="left")
- label2 = Text(scoreboard, grid = [5,0],text=" In a row : ", align="left")
- row_p1=Text(scoreboard, grid = [6,0],text="0", align="left")
- score_p1.value = "0"
- label = Text(scoreboard, grid = [0,1],text="Player 2 ", align="left")
- label = Text(scoreboard, grid = [1,1],text="Score : ", align="left")
- score_p2 = Text(scoreboard, grid = [2,1],text="0", align="left")
- Label_rounds=Text(scoreboard, grid = [3,1],text=" Rounds : ", align="left")
- rounds_p2=Text(scoreboard, grid = [4,1],text="0", align="left")
- label2 = Text(scoreboard, grid = [5,1],text=" In a row : ", align="left")
- row_p2=Text(scoreboard, grid = [6,1],text="0", align="left")
- score_p2.value = "0"
- result = Text(app)
- timer=Text(command_box, text="30", align="left")
- buttonsec=PushButton(command_box,command=button_start,text=" Start", align="left", height=1)
- buttonsec.bg="white"
- buttonsec.text_color = "black"
- buttonsec.text_size=12
- Label_rounds=Text(command_box, text="Rounds : ", align="left")
- rounds=Text(command_box, text="1", align="left")
- bonustime = CheckBox(command_box, text="Bonus Time", height="fill")
- bonustime.text_size=12
- buttonrules=PushButton(app,command=set_of_rules,text=" Set of Rules", align="right", height="fill")
- timer.repeat(1000, decrease)
- Xrange=3
- Yrange=3
- buttons_p1 = []
- pictures_p1 = []
- buttons_p2 = []
- pictures_p2 = []
- # 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):
- picture_p1 = Picture(pictures1_box, grid=[x,y])
- pictures_p1.append(picture_p1)
- button_p1 = PushButton(buttons1_box, grid=[x,y])
- buttons_p1.append(button_p1)
- picture_p2 = Picture(pictures2_box, grid=[x,y])
- pictures_p2.append(picture_p2)
- button_p2 = PushButton(buttons2_box, grid=[x,y])
- buttons_p2.append(button_p2)
- new_try(1)
- new_try(2)
- buttons1_box.enabled=False
- buttons2_box.enabled=False
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment