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,Text, PushButton,warn,info
- no_count=False
- # set the path to the emoji folder on your computer
- emojis_dir = "emojis"
- # create a list of the locations of the emoji images. This is never altered once set up
- pemojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
- emojis=[]
- def make_emojis():
- global emojis
- #Copy emoji paths to a temporary variable which is destroyed during the running of the game
- #Copy data and not pointer
- emojis=pemojis[:]
- ###print(emojis)
- # shuffle the emojis
- shuffle(emojis)
- ###print(emojis)
- app = App("emoji match")
- result = Text(app)
- attempts=0
- score_total =0
- def counter():
- global attempts, score_total,no_count
- if no_count: return
- timer.value = int(timer.value) - 1
- if int(timer.value) == 0:
- timer.cancel(counter)
- # reset the timer
- result.value = "Game Over"
- txt.text_color='green'
- txt.value = "FINAL SCORE You have " +str(score_total)
- warn("Time Out", "you've run out of time")
- #reset score counters
- attempts = 0
- score_total=0
- txt.value = "Start"
- # reset timer
- timer.value = "20"
- # reset result
- result.value = ""
- # start new round
- new_round()
- #restart timer
- timer.repeat(1000, counter)
- def match_emoji(matched):
- global attempts, score_total,no_count
- if matched:
- attempts+=1
- if attempts>2:
- attempts=0
- score_total+=10
- no_count=True
- info("Congratulations", "You just got a 10 point bonus and 10 extra seconds")
- timer.value=str(10 +int(timer.value))
- no_count=False
- score_total+=2
- txt.value = "CORRECT You have " +str(score_total) + ' score'
- txt.text_color='black'
- else:
- score_total-=1
- txt.value = "INCORRECT You have " +str(score_total) + ' score'
- txt.text_color='red'
- new_round()
- txt=Text(app, text='Click on the matching Emoji in the right grid. Good Luck ',align='top')
- # create a box to house the grid
- pictures_box = Box(app, layout="grid",align='left', border=3)
- buttons_box = Box(app, layout="grid", align='right', border=3)
- # add in the extra features
- extra_features = Box(app)
- timer = Text(extra_features, text="Get Ready")
- # create an empty list to which pictures and one for buttons will be added
- pictures = []
- buttons=[]
- for x in range(0,3):
- for y in range(0,3):
- # put the pictures into the list
- picture = Picture(pictures_box, grid=[x,y])
- pictures.append(picture)
- button = PushButton(buttons_box, grid=[x,y])
- buttons.append(button)
- def new_round():
- global attempts, score_total, emojis
- #Check if we have enough emoji to carry on; if not make another set
- if len(emojis)<17:
- make_emojis()
- match_number=randint(0,8)# pick random emoji place for match picture
- pnum=0
- match_pic=None
- # for each picture in the list but save a random one for match
- for picture in pictures:
- picture.image = emojis.pop()
- if pnum ==match_number:
- match_pic=picture.image #save selected matching emoji
- pnum+=1
- match_dup=randint(0,8) #pick random place to put matching pictue
- pnum=0
- #Now same for buttons
- for button in buttons:
- if pnum==match_dup:
- button.image=match_pic
- button.update_command(match_emoji, [True])
- else:
- button.image = emojis.pop()
- button.update_command(match_emoji, [False])
- pnum+=1
- # set the command to be called and pass False, as these emoji wont be the matching ones
- new_round()
- # start the timer
- timer.value = 20
- timer.repeat(1000, counter)
- app.display()
Add Comment
Please, Sign In to add comment