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, MenuBar, Window, question, CheckBox,Slider, Combo
- import sys
- app = App("emoji match")
- the_rules='''
- The Rules
- There are two sets of 9 emojis. One emoji is repeated
- on both left and right panels. Clicking on the correct duplicate emoji
- on the right hand panel gives you points.
- The highest score & scorer (in any turn) is recorded. Players have a timed period for their
- turn before the second player's turn. This alternates for a number
- of rounds. After the maximum number of rounds the game finishes.
- The high score remains and further games may be played with the same or different
- players. There may be penalties for incorrect answers and may be bonuses for getting
- three correct in a row.
- A number of parameters can be changed to alter rounds per game, times, penalties and bonuses
- '''
- about_prog='''
- Produced for the Raspberry PI course
- Not for sale
- No warrenty given
- No functionallity stated
- etc
- '''
- player1_name=''
- player1_high=0
- player2_name=''
- player2_high=0
- max_rounds=2
- bonus_time=True
- turn_time=20
- bonus_points=10
- current_round=0
- current_player=0
- high_scorer=''
- high_score=None
- initialized_set=False
- def init_game():
- global player1_name,player2_name,current_player,initialized_set
- initialized_set=True
- x=question('name', 'Player 1 name?', initial_value=None)
- player1_name=x
- x=question('name', 'Player 2 name?', initial_value=None)
- player2_name=x
- pictures_box.visible=True
- buttons_box.visible=True
- current_player_text.value=player1_name
- current_player=0
- def new_game():
- global current_player, score
- current_player=0
- init_game()
- new_turn()
- # start the timer
- timer.value = turn_time
- timer.repeat(1000, counter)
- def another_game():
- global current_player,current_player_tex,initialized_set,turn_time
- if not initialized_set :
- new_game()
- return
- current_player_text.value=player1_name
- current_player=0
- new_turn()
- # start the timer
- timer.value = turn_time
- timer.repeat(1000, counter)
- def exit_prog():
- app.destroy()
- def show_rules():
- window.show(wait = True)
- def show_about():
- awindow.show(wait = True)
- def show_settings():
- swindow.show(wait = True)
- def set_rounds():
- global max_rounds
- max_rounds=rounds_slider.value
- def set_time():
- global turn_time
- turn_time=time_slider.value
- def set_b_time():
- global bonus_time
- if time_checkbox.value ==1 :
- bonus_time=True
- else :
- bonus_time=False
- menubar = MenuBar(app,
- # These are the menu options
- toplevel=["Game", "Setting","Help", "Exit"],
- # The options are recorded in a nested lists, one list for each menu option
- # Each option is a list containing a name and a function
- options=[
- [ ["New Game (different players", new_game], ["Another Game with same players", another_game] ],
- [ ["do_settings", show_settings] ],
- [ ["Show Rules", show_rules] , ["About", show_about] ],
- [ ["Exit", exit_prog] ]
- ])
- highscore_text=Text(app, text='High Scorer?', color="Yellow", bg='Red', size=20,width='fill')
- current_player_text=Text(app, text='Current player')
- current_player_text.value=player1_name
- current_player=player1_name
- ###current_player_text2=Text(app, text='Current player 2', visible=False)
- window = Window(app, title = "The Rules", height=400, width=800)
- text=Text(window, text=the_rules)
- window.hide()
- ##Configure About Window and hide
- awindow = Window(app, title = "About", height=400, width=800)
- atext=Text(awindow, text=about_prog)
- awindow.hide()
- #Configure Settings Page and hide
- swindow = Window(app, title = "About", height=400, width=800, layout='grid')
- stext=Text(swindow, text='Adjust settings', grid=[1,0])
- time_checkbox = CheckBox(swindow, text="Time Bonus" , command=set_b_time, grid=[0,1])
- if bonus_time :
- time_checkbox.value=1
- else:
- time_checkbox.value=0
- rounds_txt=Text(swindow, text='No of Rounds', grid=[0,2])
- rounds_slider=Slider(swindow, start=1, end=10,command=set_rounds, grid=[1,2])
- rounds_slider.value=max_rounds
- time_txt=Text(swindow, text='Time for each player (sec)', grid=[0,3])
- time_slider=Slider(swindow, start=10, end=120,command=set_time, grid=[1,3])
- time_slider.value=turn_time
- swindow.hide()
- 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))]
- pemojis.remove('emojis\\Thumbs.db') ##remove any thumbnail file produced by Windoze
- 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[:]
- # shuffle the emojis
- shuffle(emojis)
- result = Text(app)
- attempts=0
- score_total =0
- def counter():
- global attempts, score_total,no_count,max_rounds,current_round,current_player
- if no_count: return
- timer.value = int(timer.value) - 1
- if int(timer.value) == 0:
- timer.cancel(counter)
- # reset the timer
- ###
- #check if game over or just turn
- result.value = "Turn Over"
- txt.text_color='green'
- txt.value = "This turn's SCORE You have " +str(score_total)
- #change player
- current_round+=1
- if current_round > max_rounds and current_player==1:
- nocount=True
- warn('Game Over','Game finished\nSelect from menu what to do next')
- return
- else:
- warn("Time Out", "Next Player Please")
- if current_player==0:
- current_player=1
- current_player_text.value=player2_name ###
- else:
- current_player=0
- current_player_text.value=player1_name ###
- #reset score counters
- attempts = 0
- score_total=0
- txt.value = ""
- # reset timer
- timer.value = turn_time
- # reset result
- result.value = ""
- # start new round
- new_turn()
- #restart timer
- timer.repeat(1000, counter)
- def match_emoji(matched):
- global attempts, score_total,no_count,high_score,high_scorer,current_player
- if matched:
- attempts+=1
- if attempts>2:
- attempts=0
- score_total+=bonus_points
- no_count=True
- if bonus_time:
- 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'
- if high_score==None or high_score < score_total:
- high_score=score_total
- high_scorer=current_player
- if current_player==0 :
- high_scorer=player1_name
- else :
- high_scorer=player2_name
- highscore_text.value='High Scorer: ' + high_scorer + ' high score: ' + str(high_score)
- else:
- attempts=0
- score_total-=1
- txt.value = "INCORRECT You have " +str(score_total) + ' score'
- txt.text_color='red'
- new_turn()
- txt=Text(app, text='Click on the menu above. Good Luck ',align='top')
- # create a box to house the grid
- pictures_box = Box(app, layout="grid",align='left', border=3, visible=False)
- buttons_box = Box(app, layout="grid", align='right', border=3,visible=False)
- # add in the extra features
- extra_features = Box(app)
- timer = Text(extra_features, text="Select Game -> New Game to start")
- # 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_turn():
- global attempts, score_total, emojis
- score_total=0
- #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
- app.display()
Add Comment
Please, Sign In to add comment