Advertisement
Guest User

Joseph Whitt Pygame Project

a guest
Oct 9th, 2014
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 17.38 KB | None | 0 0
  1. """
  2. Joseph Whitt
  3. Pygame Project
  4. mastermind.py
  5. """
  6.  
  7. import pygame
  8. import random
  9.  
  10. pygame.init()
  11. pygame.mixer.init()
  12. clock = pygame.time.Clock()
  13. screen = pygame.display.set_mode((640, 640))
  14. pygame.display.set_caption("Mastermind by Joseph Whitt")
  15.  
  16. background = pygame.Surface(screen.get_size())
  17. background = background.convert()
  18. background.fill((0xaa, 0x73, 0x50))
  19.  
  20.  
  21. splashimage = pygame.image.load("splash.png")
  22. splashimage = splashimage.convert()
  23.  
  24.  
  25. Splash = True  #Show splash screen until Enter is pressed
  26. while Splash:
  27.     clock.tick(30)
  28.     for event in pygame.event.get():
  29.         if event.type == pygame.KEYDOWN:
  30.             if event.key == pygame.K_RETURN:
  31.                 Splash = False
  32.             else:
  33.                 Splash = True
  34.     screen.blit(background, (0, 0))
  35.     screen.blit(splashimage, (0, 0))
  36.     pygame.display.flip()
  37.  
  38. nameFont = pygame.font.SysFont("arial", 12)
  39. numberFont = pygame.font.SysFont("arial", 20)
  40.  
  41. #Proceed to main game
  42.  
  43. class Button(pygame.sprite.Sprite): #Creates the guess choice buttons
  44.     def __init__(self, btnText):
  45.         pygame.sprite.Sprite.__init__(self)
  46.         self.value = 0
  47.         self.selected = False
  48.         self.bgColor = (0x99, 0x99, 0x99)
  49.  
  50.         self.image = pygame.Surface((50, 50))
  51.         self.image.fill(self.bgColor)
  52.  
  53.         self.colName = numberFont.render(btnText, True, (0x00, 0x00, 0x00))
  54.         self.image.blit(self.colName,(10, 0))
  55.        
  56.         self.rect = self.image.get_rect()
  57.         self.rect.centerx = 100
  58.         self.rect.centery = 50
  59.  
  60.     def update(self):
  61.         self.image.fill(self.bgColor)
  62.         self.image.blit(self.colName,(18, 15))
  63.         self.rect.centery = 550
  64.  
  65.         if self.value == 1:
  66.             self.bgColor = (0xff, 0, 0) #Red
  67.         elif self.value == 2:
  68.             self.bgColor = (0, 0xff, 0) #Green
  69.         elif self.value == 3:
  70.             self.bgColor = (0, 0x80, 0xff) #Blue
  71.         elif self.value == 4:
  72.             self.bgColor = (0xff, 0xff, 0) #Yellow
  73.         elif self.value == 5:
  74.             self.bgColor = (0xff, 0x7e, 0) #Orange
  75.         elif self.value == 6:
  76.             self.bgColor = (0x41, 0, 0xff) #Purple
  77.  
  78.  
  79.     def nextchoice(self): #Move to next color choice
  80.         self.value += 1
  81.         if self.value > 6:
  82.              self.value = 1
  83.  
  84.     def prevchoice(self): #Move to previous color choice
  85.         self.value -= 1
  86.         if self.value < 1:
  87.             self.value = 6
  88.  
  89.     def setSelected(self, selValue): #set selected value and background color
  90.         if selValue == True:
  91.             self.selected == True
  92.         else:
  93.             self.selected == False
  94.  
  95.  
  96. class Textbox_bg(pygame.sprite.Sprite): #Draws background for text area
  97.     def __init__(self):
  98.         pygame.sprite.Sprite.__init__(self)
  99.         self.bgColor = (0xff, 0xff, 0xff)
  100.         self.image = pygame.Surface((220, 250))
  101.         self.image.fill(self.bgColor)
  102.         self.rect = self.image.get_rect()
  103.         self.rect.centerx = 473
  104.         self.rect.centery = 350
  105.  
  106. class Logo(pygame.sprite.Sprite): #Draws "Mastermind" logo
  107.     def __init__(self):
  108.         pygame.sprite.Sprite.__init__(self)
  109.         logo = pygame.image.load("logo.png")
  110.         self.image = pygame.image.load("logo.png")
  111.         self.rect = self.image.get_rect()
  112.         self.rect.centerx = 480
  113.         self.rect.centery = 100
  114.  
  115. def Box(background): #Draws a box around the history buttons
  116.         pygame.draw.rect(background, (0, 0, 0), ((15, 5), (300, 500)), 5)
  117.  
  118. class HistoryLabel(pygame.sprite.Sprite): #Puts a label inside said box
  119.     def __init__(self):
  120.         pygame.sprite.Sprite.__init__(self)
  121.         self.font = pygame.font.SysFont("Arial", 25)
  122.         self.text = "Previous Attempts"
  123.         self.center = (163, 25)
  124.                
  125.     def update(self):
  126.         self.image = self.font.render(self.text, 1, (0, 0, 0))
  127.         self.rect = self.image.get_rect()
  128.         self.rect.center = self.center
  129.  
  130. class HistoryBtn(pygame.sprite.Sprite): #Draws history of previous guesses
  131.     def __init__(self, center_x, center_y):
  132.         pygame.sprite.Sprite.__init__(self)
  133.         self.selected = False
  134.         self.bgColor = (0x99, 0x99, 0x99)
  135.         self.value = 0
  136.        
  137.  
  138.         self.image = pygame.Surface((30, 30))
  139.         self.image.fill(self.bgColor)
  140.  
  141.         self.rect = self.image.get_rect()
  142.  
  143.         if center_x == 1:
  144.             self.rect.centerx = 50
  145.         elif center_x == 2:
  146.             self.rect.centerx = 125
  147.         elif center_x == 3:
  148.             self.rect.centerx = 200
  149.         elif center_x == 4:
  150.             self.rect.centerx = 275
  151.  
  152.         self.rect.centery = center_y
  153.  
  154.     def update(self):
  155.         self.image.fill(self.bgColor)
  156.        
  157.         if self.value == 1:
  158.             self.bgColor = (0xff, 0, 0) #Red
  159.         elif self.value == 2:
  160.             self.bgColor = (0, 0xff, 0) #Green
  161.         elif self.value == 3:
  162.             self.bgColor = (0, 0x80, 0xff) #Blue
  163.         elif self.value == 4:
  164.             self.bgColor = (0xff, 0xff, 0) #Yellow
  165.         elif self.value == 5:
  166.             self.bgColor = (0xff, 0x7e, 0) #Orange
  167.         elif self.value == 6:
  168.             self.bgColor = (0x41, 0, 0xff) #Purple
  169.            
  170.  
  171. def HistBox_Creation(): #Creates guess history
  172.     list1 = {1, 2, 3, 4} #Creates 4 buttons for each history button group
  173.     for n in list1:
  174.         globals()["HistBtn1_"+str(n)] = HistoryBtn((n), 480)
  175.  
  176.     for n in list1:
  177.         globals()["HistBtn2_"+str(n)] = HistoryBtn((n), 435)
  178.  
  179.     for n in list1:
  180.         globals()["HistBtn3_"+str(n)] = HistoryBtn((n), 390)
  181.  
  182.     for n in list1:
  183.         globals()["HistBtn4_"+str(n)] = HistoryBtn((n), 345)
  184.  
  185.     for n in list1:
  186.         globals()["HistBtn5_"+str(n)] = HistoryBtn((n), 300)
  187.  
  188.     for n in list1:
  189.         globals()["HistBtn6_"+str(n)] = HistoryBtn((n), 255)
  190.  
  191.     for n in list1:
  192.         globals()["HistBtn7_"+str(n)] = HistoryBtn((n), 210)
  193.  
  194.     for n in list1:
  195.         globals()["HistBtn8_"+str(n)] = HistoryBtn((n), 165)
  196.  
  197.     for n in list1:
  198.         globals()["HistBtn9_"+str(n)] = HistoryBtn((n), 120)
  199.  
  200.     for n in list1:
  201.         globals()["HistBtn10_"+str(n)] = HistoryBtn((n), 75)
  202.  
  203. def HistBtnChange(Guess_Num): #Tracks history of player guesses
  204.  
  205.     if Guess_Num == 1:
  206.         HistBtn1_1.value = Btn1.value
  207.         HistBtn1_2.value = Btn2.value
  208.         HistBtn1_3.value = Btn3.value
  209.         HistBtn1_4.value = Btn4.value
  210.  
  211.     elif Guess_Num == 2:
  212.         HistBtn2_1.value = Btn1.value
  213.         HistBtn2_2.value = Btn2.value
  214.         HistBtn2_3.value = Btn3.value
  215.         HistBtn2_4.value = Btn4.value
  216.  
  217.     elif Guess_Num == 3:
  218.         HistBtn3_1.value = Btn1.value
  219.         HistBtn3_2.value = Btn2.value
  220.         HistBtn3_3.value = Btn3.value
  221.         HistBtn3_4.value = Btn4.value
  222.  
  223.     elif Guess_Num == 4:
  224.         HistBtn4_1.value = Btn1.value
  225.         HistBtn4_2.value = Btn2.value
  226.         HistBtn4_3.value = Btn3.value
  227.         HistBtn4_4.value = Btn4.value
  228.  
  229.     elif Guess_Num == 5:
  230.         HistBtn5_1.value = Btn1.value
  231.         HistBtn5_2.value = Btn2.value
  232.         HistBtn5_3.value = Btn3.value
  233.         HistBtn5_4.value = Btn4.value
  234.  
  235.     elif Guess_Num == 6:
  236.         HistBtn6_1.value = Btn1.value
  237.         HistBtn6_2.value = Btn2.value
  238.         HistBtn6_3.value = Btn3.value
  239.         HistBtn6_4.value = Btn4.value
  240.  
  241.     elif Guess_Num == 7:
  242.         HistBtn7_1.value = Btn1.value
  243.         HistBtn7_2.value = Btn2.value
  244.         HistBtn7_3.value = Btn3.value
  245.         HistBtn7_4.value = Btn4.value
  246.  
  247.     elif Guess_Num == 8:
  248.         HistBtn8_1.value = Btn1.value
  249.         HistBtn8_2.value = Btn2.value
  250.         HistBtn8_3.value = Btn3.value
  251.         HistBtn8_4.value = Btn4.value
  252.  
  253.     elif Guess_Num == 9:
  254.         HistBtn9_1.value = Btn1.value
  255.         HistBtn9_2.value = Btn2.value
  256.         HistBtn9_3.value = Btn3.value
  257.         HistBtn9_4.value = Btn4.value
  258.  
  259.     elif Guess_Num == 10:
  260.         HistBtn10_1.value = Btn1.value
  261.         HistBtn10_2.value = Btn2.value
  262.         HistBtn10_3.value = Btn3.value
  263.         HistBtn10_4.value = Btn4.value
  264.  
  265. class OutputLabel(pygame.sprite.Sprite): #Label for giving player feedback
  266.     def __init__(self, textinput):
  267.         pygame.sprite.Sprite.__init__(self)
  268.         self.font = pygame.font.SysFont("Arial", 20)
  269.         self.text = textinput
  270.         self.center = (425, 220)
  271.              
  272.     def update(self):
  273.         self.image = self.font.render(self.text, 1, (0, 0, 0))
  274.         self.rect = self.image.get_rect()
  275.         self.rect.center = self.center
  276.  
  277. def CheckGuess(Solution1, Solution2, Solution3, Solution4): #Analyzes guess and compares to solution
  278.     CodeBroken = False
  279.     keyscorrect = 0 #initialize number of correct guesses
  280.  
  281.     output1.text = ("") #initialize text to blank
  282.     output2.text = ("")
  283.     output3.text = ("")
  284.     output4.text = ("")
  285.     output5.text = ("")
  286.  
  287.     #Check each guess and compare with solution
  288.     if Btn1.value == Solution1:
  289.         keyscorrect += 1
  290.     elif Btn1.value == Solution2 or Btn1.value == Solution3 or Btn1.value == Solution4:
  291.         output1.text = ("Key 1: Wrong position")
  292.        
  293.     if Btn2.value == Solution2:
  294.         keyscorrect += 1
  295.     elif Btn2.value == Solution1 or Btn2.value == Solution3 or Btn2.value == Solution4:
  296.         output2.text = ("Key 2: Wrong position")
  297.  
  298.     if Btn3.value == Solution3:
  299.         keyscorrect += 1
  300.     elif Btn3.value == Solution1 or Btn3.value == Solution2 or Btn3.value == Solution4:
  301.         output3.text = ("Key 3: Wrong position")
  302.  
  303.     if Btn4.value == Solution4:
  304.         keyscorrect += 1
  305.     elif Btn4.value == Solution1 or Btn4.value == Solution2 or Btn4.value == Solution3:
  306.         output4.text = ("Key 4: Wrong position")
  307.  
  308.  
  309.     if keyscorrect == 1:
  310.         output5.text = ("One key is correct.")
  311.     elif keyscorrect == 2:
  312.         output5.text = ("Two keys are correct.")
  313.     elif keyscorrect == 3:
  314.         output5.text = ("Three keys are correct.")
  315.     elif keyscorrect == 4: #Checks to see if all values match the solution
  316.         CodeBroken = True #If so, set boolean to true and end game
  317.     else:
  318.         CodeBroken = False
  319.  
  320.     return CodeBroken
  321.        
  322. def show_solution(Solution1, Solution2, Solution3, Solution4):
  323.     Btn1.value = Solution1
  324.     Btn2.value = Solution2
  325.     Btn3.value = Solution3
  326.     Btn4.value = Solution4
  327.  
  328. #Set up various UI elements
  329. Btn1 = Button("1")
  330. Btn1.rect.center = (50, 100)
  331. Btn1.setSelected(True)
  332. btnSelected = Btn1 #Set Button 1 to be selected by default when game begins
  333.  
  334. Btn2 = Button("2")
  335. Btn2.rect.center = (125, 100)
  336.  
  337. Btn3 = Button("3")
  338. Btn3.rect.center = (200, 100)
  339.  
  340. Btn4 = Button("4")
  341. Btn4.rect.center = (275, 100)
  342.  
  343. #Set up arrow
  344. arrow = pygame.image.load("arrow.png")
  345. arrow = arrow.convert()
  346.  
  347. #Set up text box, logo, and history label
  348. txtbox = Textbox_bg()
  349. logo = Logo()
  350. HistLbl = HistoryLabel()
  351.  
  352. #Create output text labels
  353. output1 = OutputLabel("")
  354. output1.center = (465,240)
  355. output2 = OutputLabel("")
  356. output2.center = (465,295)
  357. output3 = OutputLabel("")
  358. output3.center = (465,350)
  359. output4 = OutputLabel("")
  360. output4.center = (465,405)
  361. output5 = OutputLabel("")
  362. output5.center = (465,460)
  363.  
  364. #Create sounds
  365. beep = pygame.mixer.Sound("beep.ogg")
  366. youwin = pygame.mixer.Sound("win.ogg")
  367.  
  368. #Generate solution
  369. Solution1 = random.randint(1,6)
  370. Solution2 = random.randint(1,6)
  371. Solution3 = random.randint(1,6)
  372. Solution4 = random.randint(1,6)
  373.  
  374. #Create guess history and groups
  375. HistBox_Creation()
  376. HistRow1 = pygame.sprite.Group()
  377. HistRow1.add(HistBtn1_1, HistBtn1_2, HistBtn1_3, HistBtn1_4)
  378. HistRow2 = pygame.sprite.Group()
  379. HistRow2.add(HistBtn2_1, HistBtn2_2, HistBtn2_3, HistBtn2_4)
  380. HistRow3 = pygame.sprite.Group()
  381. HistRow3.add(HistBtn3_1, HistBtn3_2, HistBtn3_3, HistBtn3_4)
  382. HistRow4 = pygame.sprite.Group()
  383. HistRow4.add(HistBtn4_1, HistBtn4_2, HistBtn4_3, HistBtn4_4)
  384. HistRow5 = pygame.sprite.Group()
  385. HistRow5.add(HistBtn5_1, HistBtn5_2, HistBtn5_3, HistBtn5_4)
  386. HistRow6 = pygame.sprite.Group()
  387. HistRow6.add(HistBtn6_1, HistBtn6_2, HistBtn6_3, HistBtn6_4)
  388. HistRow7 = pygame.sprite.Group()
  389. HistRow7.add(HistBtn7_1, HistBtn7_2, HistBtn7_3, HistBtn7_4)
  390. HistRow8 = pygame.sprite.Group()
  391. HistRow8.add(HistBtn8_1, HistBtn8_2, HistBtn8_3, HistBtn8_4)
  392. HistRow9 = pygame.sprite.Group()
  393. HistRow9.add(HistBtn9_1, HistBtn9_2, HistBtn9_3, HistBtn9_4)
  394. HistRow10 = pygame.sprite.Group()
  395. HistRow10.add(HistBtn10_1, HistBtn10_2, HistBtn10_3, HistBtn10_4)
  396.  
  397.  
  398. #main
  399. keepGoing = True
  400. txtboxdraw = pygame.sprite.RenderUpdates(txtbox)
  401. allSprites = pygame.sprite.RenderUpdates(Btn1, Btn2, Btn3, Btn4, \
  402.                                          output1, output2, output3, output4, output5, \
  403.                                          logo, HistLbl, \
  404.                                          HistRow1, HistRow2, HistRow3, HistRow4, \
  405.                                          HistRow5, HistRow6, HistRow7, HistRow8, \
  406.                                          HistRow9, HistRow10)
  407.  
  408. Box(background)
  409. screen.blit(background, (0, 0))
  410. screen.blit(arrow, (25,580)) #Arrow's default position is button 1
  411. Guess_Num = 0 #Initialize the guess number
  412.  
  413. while keepGoing:
  414.     clock.tick(30)
  415.     KeyStop = False #Used to stop key-repeating
  416.    
  417.     for event in pygame.event.get():
  418.         if event.type == pygame.QUIT:
  419.             keepGoing = False
  420.            
  421.         elif event.type == pygame.KEYUP: #Checks for releasing keys
  422.                 if event.key == pygame.K_UP:
  423.                     KeyStop = False
  424.                 elif event.key == pygame.K_DOWN:
  425.                     KeyStop = False
  426.                 elif event.key == pygame.K_RETURN:
  427.                     KeyStop = False
  428.  
  429.                
  430.         elif event.type == pygame.KEYDOWN:
  431.  
  432.             if event.key == pygame.K_RIGHT:            
  433.                 if btnSelected == Btn1:
  434.                     btnSelected = Btn2
  435.                     screen.blit(background, (0, 0))
  436.                     screen.blit(arrow, (100,580))
  437.                 elif btnSelected == Btn2:
  438.                     btnSelected = Btn3
  439.                     screen.blit(background, (0, 0))
  440.                     screen.blit(arrow, (175,580))
  441.                 elif btnSelected == Btn3:
  442.                     btnSelected = Btn4
  443.                     screen.blit(background, (0, 0))
  444.                     screen.blit(arrow, (250,580))
  445.                 elif btnSelected == Btn4:
  446.                     btnSelected = Btn1
  447.                     screen.blit(background, (0, 0))
  448.                     screen.blit(arrow, (25,580))
  449.  
  450.                 Btn1.setSelected(False)
  451.                 Btn2.setSelected(False)
  452.                 Btn3.setSelected(False)
  453.                 Btn4.setSelected(False)
  454.                 btnSelected.setSelected(True)
  455.  
  456.             elif event.key == pygame.K_LEFT:
  457.                 if btnSelected == Btn1:
  458.                     btnSelected = Btn4
  459.                     screen.blit(background, (0, 0))
  460.                     screen.blit(arrow, (250,580))
  461.                 elif btnSelected == Btn2:
  462.                     btnSelected = Btn1
  463.                     screen.blit(background, (0, 0))
  464.                     screen.blit(arrow, (25,580))
  465.                 elif btnSelected == Btn3:
  466.                     btnSelected = Btn2
  467.                     screen.blit(background, (0, 0))
  468.                     screen.blit(arrow, (100,580))
  469.                 elif btnSelected == Btn4:
  470.                     btnSelected = Btn3
  471.                     screen.blit(background, (0, 0))
  472.                     screen.blit(arrow, (175,580))
  473.            
  474.                 Btn1.setSelected(False)
  475.                 Btn2.setSelected(False)
  476.                 Btn3.setSelected(False)
  477.                 Btn4.setSelected(False)
  478.                 btnSelected.setSelected(True)
  479.  
  480.             elif event.key == pygame.K_UP:
  481.                 if KeyStop == False: #If key is not already being pressed, then proceed
  482.                     KeyStop = True
  483.                     btnSelected.nextchoice()
  484.  
  485.             elif event.key == pygame.K_DOWN:
  486.                 if KeyStop == False:
  487.                     KeyStop = True
  488.                     btnSelected.prevchoice()
  489.  
  490.             elif event.key == pygame.K_RETURN:
  491.                 if KeyStop == False:
  492.                     KeyStop = True
  493.                     Guess_Num += 1
  494.                     HistBtnChange(Guess_Num)
  495.                     CodeBroken = False
  496.                     CodeBroken = CheckGuess(Solution1, Solution2, Solution3, Solution4)
  497.  
  498.                     if CodeBroken == True:
  499.                         youwin.play()
  500.                         output1.text = ("")
  501.                         output2.text = ("You win!")
  502.                         output3.text = ("")
  503.                         output4.text = ("You broke the code!")
  504.                         output4.center = (465,435)
  505.                         output5.text = ("")
  506.                     elif Guess_Num < 10:
  507.                         beep.play()
  508.                     else: #10 guesses used, game over
  509.                         output1.text = ("Out of guesses!")
  510.                         output2.text = ("Game Over")
  511.                         output3.text = ("")
  512.                         output4.text = ("Solution shown")
  513.                         output5.text = ("")
  514.                         show_solution(Solution1, Solution2, Solution3, Solution4)
  515.  
  516.                    
  517.  
  518.             elif event.key == pygame.K_ESCAPE: #End program on escape key
  519.                 keepGoing = False
  520.  
  521.  
  522.     allSprites.clear(screen, background)
  523.     allSprites.update()
  524.     txtboxdraw.draw(screen)
  525.     allSprites.draw(screen)
  526.     pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement