johnpentyrch

FinalAssignmentGUIcourse

May 17th, 2020
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.25 KB | None | 0 0
  1. import os
  2. from random import shuffle, randint
  3. from guizero import App, Box, Picture,Text, PushButton,warn,info, MenuBar, Window, question, CheckBox,Slider, Combo
  4. import sys
  5.  
  6. app = App("emoji match")
  7. the_rules='''
  8. The Rules
  9.  
  10. There are two sets of 9 emojis. One emoji is repeated
  11. on both left and right panels. Clicking on the correct duplicate emoji
  12. on the right hand panel gives you points.
  13. The highest score & scorer (in any turn) is recorded. Players have a timed period for their
  14. turn before the second player's turn. This alternates for a number
  15. of rounds. After the maximum number of rounds the game finishes.
  16. The high score remains and further games may be played with the same or different
  17. players. There may be penalties for incorrect answers and may be bonuses for getting
  18. three correct in a row.
  19. A number of parameters can be changed to alter rounds per game, times, penalties and bonuses
  20.  
  21. '''
  22.  
  23. about_prog='''
  24.  
  25. Produced for the Raspberry PI course
  26. Not for sale
  27. No warrenty given
  28.  
  29. No functionallity stated
  30.  
  31. etc
  32.  
  33.  
  34. '''
  35. player1_name=''
  36. player1_high=0
  37. player2_name=''
  38. player2_high=0
  39. max_rounds=2
  40. bonus_time=True
  41. turn_time=20
  42. bonus_points=10
  43. current_round=0
  44. current_player=0
  45. high_scorer=''
  46. high_score=None
  47. initialized_set=False
  48.  
  49.  
  50. def init_game():
  51.     global player1_name,player2_name,current_player,initialized_set
  52.     initialized_set=True
  53.     x=question('name', 'Player 1 name?', initial_value=None)
  54.     player1_name=x
  55.     x=question('name', 'Player 2 name?', initial_value=None)
  56.     player2_name=x
  57.     pictures_box.visible=True
  58.     buttons_box.visible=True
  59.     current_player_text.value=player1_name
  60.     current_player=0
  61.  
  62. def new_game():
  63.     global current_player, score
  64.     current_player=0
  65.     init_game()      
  66.     new_turn()
  67.     # start the timer
  68.     timer.value = turn_time
  69.     timer.repeat(1000, counter)
  70.    
  71.    
  72. def another_game():
  73.     global current_player,current_player_tex,initialized_set,turn_time
  74.     if not initialized_set :
  75.         new_game()
  76.         return
  77.     current_player_text.value=player1_name
  78.     current_player=0
  79.     new_turn()
  80.     # start the timer
  81.     timer.value = turn_time
  82.     timer.repeat(1000, counter)
  83.  
  84. def exit_prog():
  85.     app.destroy()
  86.    
  87. def show_rules():
  88.     window.show(wait = True)
  89.    
  90. def show_about():
  91.     awindow.show(wait = True)
  92.    
  93. def show_settings():
  94.     swindow.show(wait = True)
  95.  
  96. def set_rounds():
  97.     global max_rounds
  98.     max_rounds=rounds_slider.value
  99.    
  100. def set_time():
  101.     global turn_time
  102.     turn_time=time_slider.value
  103.  
  104. def set_b_time():
  105.     global bonus_time
  106.     if time_checkbox.value ==1 :
  107.         bonus_time=True
  108.     else :
  109.         bonus_time=False
  110.    
  111. menubar = MenuBar(app,
  112.                   # These are the menu options
  113.                   toplevel=["Game", "Setting","Help", "Exit"],
  114.                   # The options are recorded in a nested lists, one list for each menu option
  115.                   # Each option is a list containing a name and a function
  116.                   options=[
  117.                       [ ["New Game (different players", new_game], ["Another Game with same players", another_game] ],
  118.                       [ ["do_settings", show_settings] ],
  119.                       [ ["Show Rules", show_rules] , ["About", show_about] ],
  120.                       [ ["Exit", exit_prog] ]
  121.                   ])
  122.  
  123. highscore_text=Text(app, text='High Scorer?', color="Yellow", bg='Red', size=20,width='fill')
  124. current_player_text=Text(app, text='Current player')
  125. current_player_text.value=player1_name
  126. current_player=player1_name
  127. ###current_player_text2=Text(app, text='Current player 2', visible=False)
  128. window = Window(app, title = "The Rules", height=400, width=800)
  129. text=Text(window, text=the_rules)
  130. window.hide()
  131. ##Configure About Window and hide
  132. awindow = Window(app, title = "About", height=400, width=800)
  133. atext=Text(awindow, text=about_prog)
  134. awindow.hide()
  135. #Configure Settings Page and hide
  136. swindow = Window(app, title = "About", height=400, width=800, layout='grid')
  137. stext=Text(swindow, text='Adjust settings', grid=[1,0])
  138. time_checkbox = CheckBox(swindow, text="Time Bonus"  , command=set_b_time, grid=[0,1])
  139. if bonus_time :
  140.     time_checkbox.value=1
  141. else:
  142.     time_checkbox.value=0
  143. rounds_txt=Text(swindow, text='No of Rounds', grid=[0,2])
  144. rounds_slider=Slider(swindow, start=1, end=10,command=set_rounds, grid=[1,2])
  145. rounds_slider.value=max_rounds
  146. time_txt=Text(swindow, text='Time for each player (sec)', grid=[0,3])
  147. time_slider=Slider(swindow, start=10, end=120,command=set_time, grid=[1,3])
  148. time_slider.value=turn_time
  149.  
  150. swindow.hide()
  151.  
  152.  
  153. no_count=False
  154. # set the path to the emoji folder on your computer
  155. emojis_dir = "emojis"
  156. # create a list of the locations of the emoji images. This is never altered once set up
  157. pemojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  158. pemojis.remove('emojis\\Thumbs.db') ##remove any thumbnail file produced by Windoze
  159. emojis=[]
  160. def make_emojis():
  161.     global emojis
  162.     #Copy emoji paths to a temporary variable which is destroyed during the running of the game
  163.     #Copy data and not pointer
  164.     emojis=pemojis[:]
  165.     # shuffle the emojis
  166.     shuffle(emojis)
  167.  
  168.  
  169.  
  170. result = Text(app)
  171. attempts=0
  172. score_total =0
  173.  
  174. def counter():
  175.     global attempts, score_total,no_count,max_rounds,current_round,current_player
  176.     if no_count: return
  177.     timer.value = int(timer.value) - 1
  178.     if int(timer.value) == 0:
  179.         timer.cancel(counter)
  180.         # reset the timer
  181.         ###
  182.         #check if game over or just turn
  183.         result.value = "Turn Over"
  184.         txt.text_color='green'  
  185.         txt.value = "This turn's SCORE You have " +str(score_total)
  186.        
  187.        
  188.         #change player
  189.         current_round+=1
  190.         if current_round > max_rounds and current_player==1:
  191.             nocount=True
  192.             warn('Game Over','Game finished\nSelect from menu what to do next')
  193.             return
  194.         else:
  195.             warn("Time Out", "Next Player Please")
  196.         if current_player==0:
  197.             current_player=1
  198.             current_player_text.value=player2_name ###
  199.         else:
  200.             current_player=0
  201.             current_player_text.value=player1_name ###
  202.         #reset score counters
  203.         attempts = 0
  204.         score_total=0
  205.         txt.value = ""
  206.         # reset timer
  207.         timer.value = turn_time
  208.         # reset result
  209.         result.value = ""
  210.         # start new round
  211.         new_turn()
  212.         #restart timer
  213.         timer.repeat(1000, counter)
  214.  
  215.  
  216. def match_emoji(matched):
  217.     global attempts, score_total,no_count,high_score,high_scorer,current_player
  218.     if matched:
  219.         attempts+=1
  220.         if attempts>2:
  221.             attempts=0
  222.             score_total+=bonus_points
  223.             no_count=True
  224.             if bonus_time:
  225.                 timer.value=str(10 +int(timer.value))
  226.             no_count=False
  227.         score_total+=2
  228.         txt.value = "CORRECT You have " +str(score_total) + ' score'
  229.         txt.text_color='black'
  230.         if high_score==None or high_score < score_total:
  231.             high_score=score_total
  232.             high_scorer=current_player
  233.             if current_player==0 :
  234.                 high_scorer=player1_name
  235.             else :
  236.                 high_scorer=player2_name
  237.         highscore_text.value='High Scorer: '  + high_scorer + '  high score: ' + str(high_score)
  238.  
  239.     else:
  240.         attempts=0
  241.         score_total-=1
  242.         txt.value = "INCORRECT You have " +str(score_total) + ' score'
  243.         txt.text_color='red'
  244.     new_turn()
  245.    
  246. txt=Text(app, text='Click on the menu above. Good Luck  ',align='top')
  247.  
  248.  
  249. # create a box to house the grid
  250. pictures_box = Box(app, layout="grid",align='left', border=3, visible=False)
  251. buttons_box = Box(app, layout="grid", align='right', border=3,visible=False)
  252.  
  253. # add in the extra features
  254. extra_features = Box(app)
  255. timer = Text(extra_features, text="Select Game -> New Game to start")
  256.  
  257.  
  258. # create an empty list to which pictures and one for buttons will be added
  259. pictures = []
  260. buttons=[]
  261.  
  262. for x in range(0,3):
  263.     for y in range(0,3):
  264.         # put the pictures into the list
  265.         picture = Picture(pictures_box, grid=[x,y])
  266.         pictures.append(picture)      
  267.         button = PushButton(buttons_box, grid=[x,y])
  268.         buttons.append(button)
  269.  
  270. def new_turn():
  271.     global attempts, score_total, emojis
  272.     score_total=0
  273.     #Check if we have enough emoji to carry on; if not make another set
  274.     if len(emojis)<17:
  275.         make_emojis()
  276.  
  277.     match_number=randint(0,8)# pick random emoji place for match picture
  278.     pnum=0
  279.     match_pic=None
  280.     # for each picture in the list but save a random one for match
  281.     for picture in pictures:
  282.         picture.image = emojis.pop()
  283.         if pnum ==match_number:
  284.             match_pic=picture.image #save selected matching emoji
  285.         pnum+=1
  286.        
  287.     match_dup=randint(0,8) #pick random place to put matching pictue
  288.     pnum=0
  289.     #Now same for buttons
  290.     for button in buttons:
  291.         if pnum==match_dup:
  292.             button.image=match_pic
  293.             button.update_command(match_emoji, [True])
  294.         else:
  295.             button.image = emojis.pop()
  296.             button.update_command(match_emoji, [False])
  297.         pnum+=1
  298.  
  299. app.display()
Add Comment
Please, Sign In to add comment