Advertisement
kalpin

Peer Assessed Game

Aug 10th, 2020
1,889
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.91 KB | None | 0 0
  1. # Two player version of matching game
  2. # Kevin Alpin
  3.  
  4. import os
  5. from random import shuffle, randint
  6. from guizero import App, Box, PushButton, Text, TextBox, info, question, warn, Window
  7. import pickle
  8.  
  9. # global variables
  10. GRID_SIZE   = 4
  11. score1      = 0
  12. score2      = 0
  13. TimeLeft    = 60
  14. emojis_dir  = "emojis"
  15. emojis      = []
  16. buttons     = []
  17. pictures    = []
  18. player      = 1
  19. playing     = True
  20.  
  21.  
  22. # Event Handlers
  23.  
  24. def imageClick(matched):
  25.     global score1, score2, player
  26.     if matched:
  27.         if player == 1:
  28.             txtTimeLeft1.value = int(txtTimeLeft1.value) + 5
  29.             score1 += 1
  30.             player = 2
  31.             lblInstruct.value =  'Player 2'
  32.             lblInstruct.text_color = 'red'
  33.             lblScore1.value = f"Player 1 Score: {score1}"
  34.         else:
  35.             txtTimeLeft1.value = int(txtTimeLeft1.value) + 5
  36.             score2 += 1
  37.             player = 1
  38.             lblScore2.value = f"Player 2 Score: {score2}"
  39.             lblInstruct.value =  'Player 1'
  40.             lblInstruct.text_color = 'blue'
  41.         setupRound()
  42.     else:  
  43.         if player == 1:
  44.             txtTimeLeft1.value = int(txtTimeLeft1.value) - 1
  45.             player = 2
  46.             lblInstruct.value =  'Player 2'
  47.             lblInstruct.text_color = 'red'
  48.         else:
  49.             txtTimeLeft1.value = int(txtTimeLeft1.value) - 1
  50.             player = 1
  51.             lblInstruct.value =  'Player 1'
  52.             lblInstruct.text_color = 'blue'
  53.     if player == 1:
  54.         player1.bg = 'pink'
  55.         player2.bg = 'white'
  56.     else:
  57.         player1.bg = 'white'
  58.         player2.bg = 'pink'
  59.     txtTimeLeft1.startTime = int(txtTimeLeft1.value)
  60.        
  61. def btnInfoClicked():
  62.     info('HELP','''There is exactly one pair of matching images. \nClick on one of the two matching images. \nEvery incorrect click loses 1 seconds.\
  63.                   \nEvery match scores 1 point and gain 5 seconds of time.\
  64.                   \nBE QUICK - If you take more than 10 seconds you lose your turn!''')
  65.  
  66. def closeWindow():
  67.     resultsForm.hide()
  68.     app.destroy()
  69.        
  70. # Helper Functions
  71.  
  72. def setupRound():
  73.     global buttons, pictures, emojis, score1, score2, player
  74.    
  75.     # load emoji filenames
  76.     emojis = [os.path.join(emojis_dir, f) for f in os.listdir(emojis_dir) if os.path.isfile(os.path.join(emojis_dir, f))]
  77.     # shuffle the emojis
  78.     shuffle(emojis)
  79.  
  80.     # for each picture and button in the list assign an emoji to its image feature
  81.     for button in pictures:
  82.         button.image = emojis.pop()
  83.         button.update_command(imageClick, [False])
  84.        
  85.     for button in buttons:
  86.         button.image = emojis.pop()
  87.         button.update_command(imageClick, [False])
  88.  
  89.     # Select new image and place in both grids
  90.     newImage = emojis.pop()
  91.    
  92.     pos = randint(0,len(buttons)-1)
  93.     buttons[pos].image = newImage
  94.     buttons[pos].update_command(imageClick, [True])
  95.    
  96.     pos = randint(0,len(pictures)-1)
  97.     pictures[pos].image = newImage
  98.     pictures[pos].update_command(imageClick, [True])
  99.  
  100.     # set time left
  101.     if not playing:
  102.         txtTimeLeft1.value = TimeLeft
  103.  
  104.        
  105. def decreaseTime1():
  106.     global player
  107.     txtTimeLeft1.value = int(txtTimeLeft1.value) - 1
  108.     if int(txtTimeLeft1.value) <= 0:
  109.         app.cancel(decreaseTime1)
  110.         playing = False
  111.         info('Game Over','Game Over')
  112.         endGameRoutine()
  113.    
  114.     if txtTimeLeft1.startTime - int(txtTimeLeft1.value)>=10: # ran out of time
  115.         app.cancel(decreaseTime1)
  116.         if player == 1:
  117.             player2.bg = 'pink'
  118.             player1.bg = 'white'
  119.             lblInstruct.value =  'Player 2'
  120.             lblInstruct.text_color = 'red'
  121.             player = 2
  122.         else:
  123.             player2.bg = 'white'
  124.             player1.bg = 'pink'
  125.             lblInstruct.value =  'Player 1'
  126.             lblInstruct.text_color = 'blue'
  127.             player = 1
  128.         warn('Time Out', f'You ran out of time. It is now player {player}''s go')
  129.         txtTimeLeft1.startTime = int(txtTimeLeft1.value )
  130.         app.repeat(1000,decreaseTime1)
  131.  
  132. def endGameRoutine():
  133.     # get current high scores
  134.     f = open('highscores.txt','rb')
  135.     highscores = pickle.load(f)
  136.     f.close()
  137.     highscores.append([score1,player1.value])
  138.     highscores.append([score2,player2.value])
  139.     highscores.sort(reverse = True)
  140.    
  141.     # show results
  142.     row = 1
  143.     for item in highscores:
  144.         scorevalue = Text(resultsForm,text = highscores[row-1][0], width = 20,grid = [0,row])
  145.         namevalue =  Text(resultsForm,text = highscores[row-1][1], width = 20, grid = [1,row])
  146.         row += 1
  147.     spacer = Text(resultsForm, text = "", grid = [0,row,2,1])
  148.     closeBtn = PushButton(resultsForm,text='CLOSE', width = 25, grid = [1,row,2,1],command = closeWindow)
  149.  
  150.     # resave high scores
  151.     f = open('highscores.txt','wb')
  152.     pickle.dump(highscores,f)
  153.     f.close()
  154.  
  155.     # show form
  156.     resultsForm.show(wait=True)
  157.    
  158.    
  159.  
  160. # .......................................................................................  
  161.  
  162. # setup the app
  163. app = App("emoji match", width = GRID_SIZE*200, height = 150+GRID_SIZE*100, bg = 'white')
  164.  
  165. # create a box for information
  166. spaceBox = Box(app,width = 'fill', height = 20)
  167. infoBox = Box(app, width = 'fill', align = 'top', height = 30)
  168. infoBox.bg = 'blue'
  169. lblInstructions = PushButton(infoBox, command = btnInfoClicked, text = 'Information', width = 20, height = 'fill')
  170. lblInstructions.bg = 'red'
  171. lblInstructions.text_color = 'white'
  172. spaceBox = Box(infoBox,width = 'fill', height = 20)
  173.  
  174. # player name box
  175. playerBox = Box(app, width = 'fill',height = 40)
  176. playerBox.bg = 'light yellow'
  177. player1 = TextBox(playerBox,text = 'Player 1',width = 30, align = 'left')
  178. player1.text_size = 14
  179. player1.bg = 'pink'
  180. player2 = TextBox(playerBox,text = 'Player 2',width = 30, align = 'right')
  181. player2.text_size = 14
  182.  
  183. # create a box to house the grids
  184. gameBox = Box(app)
  185. space = Box(gameBox, width = "fill", height = 60, align='top')
  186. lblInstruct = Text(space, text = 'Player 1: Your turn',height = 3)
  187.  
  188.  
  189. # create a box to house the pictures
  190. picturesBox = Box(gameBox, layout="grid",align='left')
  191.  
  192. # create a box to house the buttons
  193. buttonsBox = Box(gameBox, layout="grid",align='left')
  194.  
  195. # space
  196. spaceBox = Box(app,width = 'fill', height = 20)
  197.  
  198. #create a box for feedback player 1
  199. resultBox1 = Box(app, width = "fill", height = 50,align = 'top')
  200. resultBox1.bg = 'light blue'
  201.  
  202. lblTime1 = Text(resultBox1, text='Time Left:',align = 'left', height = '2', width = 10)
  203. txtTimeLeft1 = TextBox(resultBox1, text = '', align = 'left', height = '2')
  204. txtTimeLeft1.text_size = 12
  205.  
  206. lblScore1 = Text(resultBox1, text = '', width = '30', height = 'fill', color = 'blue')
  207. lblScore1.value = f" Player 1 Score: {score1}"
  208. lblScore2 = Text(resultBox1, text = '', width = '30', height = 'fill', color = 'red')
  209. lblScore2.value = f" Player 2 Score: {score2}"
  210.  
  211. # create PushButtons with a different grid coordinate and add to the list
  212. for x in range(0,GRID_SIZE):
  213.     for y in range(0,GRID_SIZE):
  214.         # put the pictures and buttons into the lists
  215.         button = PushButton(picturesBox, grid=[x,y])
  216.         pictures.append(button)
  217.  
  218.         button = PushButton(buttonsBox, grid=[x,y])
  219.         buttons.append(button)
  220.  
  221. # set up results window
  222. resultsForm = Window(app,title = 'Highscores', bg = 'bisque',layout = 'grid')
  223. lblScore = Text(resultsForm,text = 'Score', align = 'left', width = 20, grid = [0,0])
  224. lblName  = Text(resultsForm,text = 'Player', align = 'left', width = 20, grid = [1,0])
  225. resultsForm.hide()
  226.  
  227. # call round setup
  228. setupRound()
  229. name1 = question('Enter Name', 'Player 1: Enter your name')
  230. name2 = question('Enter Name', 'Player 2: Enter your name')
  231. player1.value = name1
  232. player2.value = name2
  233. playing = True
  234. txtTimeLeft1.value = TimeLeft
  235. txtTimeLeft1.startTime = int(txtTimeLeft1.value)
  236. app.repeat(1000,decreaseTime1)
  237.                    
  238. # Display app
  239. app.display()
  240.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement