Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Joseph Whitt
- Pygame Project
- mastermind.py
- """
- import pygame
- import random
- pygame.init()
- pygame.mixer.init()
- clock = pygame.time.Clock()
- screen = pygame.display.set_mode((640, 640))
- pygame.display.set_caption("Mastermind by Joseph Whitt")
- background = pygame.Surface(screen.get_size())
- background = background.convert()
- background.fill((0xaa, 0x73, 0x50))
- splashimage = pygame.image.load("splash.png")
- splashimage = splashimage.convert()
- Splash = True #Show splash screen until Enter is pressed
- while Splash:
- clock.tick(30)
- for event in pygame.event.get():
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_RETURN:
- Splash = False
- else:
- Splash = True
- screen.blit(background, (0, 0))
- screen.blit(splashimage, (0, 0))
- pygame.display.flip()
- nameFont = pygame.font.SysFont("arial", 12)
- numberFont = pygame.font.SysFont("arial", 20)
- #Proceed to main game
- class Button(pygame.sprite.Sprite): #Creates the guess choice buttons
- def __init__(self, btnText):
- pygame.sprite.Sprite.__init__(self)
- self.value = 0
- self.selected = False
- self.bgColor = (0x99, 0x99, 0x99)
- self.image = pygame.Surface((50, 50))
- self.image.fill(self.bgColor)
- self.colName = numberFont.render(btnText, True, (0x00, 0x00, 0x00))
- self.image.blit(self.colName,(10, 0))
- self.rect = self.image.get_rect()
- self.rect.centerx = 100
- self.rect.centery = 50
- def update(self):
- self.image.fill(self.bgColor)
- self.image.blit(self.colName,(18, 15))
- self.rect.centery = 550
- if self.value == 1:
- self.bgColor = (0xff, 0, 0) #Red
- elif self.value == 2:
- self.bgColor = (0, 0xff, 0) #Green
- elif self.value == 3:
- self.bgColor = (0, 0x80, 0xff) #Blue
- elif self.value == 4:
- self.bgColor = (0xff, 0xff, 0) #Yellow
- elif self.value == 5:
- self.bgColor = (0xff, 0x7e, 0) #Orange
- elif self.value == 6:
- self.bgColor = (0x41, 0, 0xff) #Purple
- def nextchoice(self): #Move to next color choice
- self.value += 1
- if self.value > 6:
- self.value = 1
- def prevchoice(self): #Move to previous color choice
- self.value -= 1
- if self.value < 1:
- self.value = 6
- def setSelected(self, selValue): #set selected value and background color
- if selValue == True:
- self.selected == True
- else:
- self.selected == False
- class Textbox_bg(pygame.sprite.Sprite): #Draws background for text area
- def __init__(self):
- pygame.sprite.Sprite.__init__(self)
- self.bgColor = (0xff, 0xff, 0xff)
- self.image = pygame.Surface((220, 250))
- self.image.fill(self.bgColor)
- self.rect = self.image.get_rect()
- self.rect.centerx = 473
- self.rect.centery = 350
- class Logo(pygame.sprite.Sprite): #Draws "Mastermind" logo
- def __init__(self):
- pygame.sprite.Sprite.__init__(self)
- logo = pygame.image.load("logo.png")
- self.image = pygame.image.load("logo.png")
- self.rect = self.image.get_rect()
- self.rect.centerx = 480
- self.rect.centery = 100
- def Box(background): #Draws a box around the history buttons
- pygame.draw.rect(background, (0, 0, 0), ((15, 5), (300, 500)), 5)
- class HistoryLabel(pygame.sprite.Sprite): #Puts a label inside said box
- def __init__(self):
- pygame.sprite.Sprite.__init__(self)
- self.font = pygame.font.SysFont("Arial", 25)
- self.text = "Previous Attempts"
- self.center = (163, 25)
- def update(self):
- self.image = self.font.render(self.text, 1, (0, 0, 0))
- self.rect = self.image.get_rect()
- self.rect.center = self.center
- class HistoryBtn(pygame.sprite.Sprite): #Draws history of previous guesses
- def __init__(self, center_x, center_y):
- pygame.sprite.Sprite.__init__(self)
- self.selected = False
- self.bgColor = (0x99, 0x99, 0x99)
- self.value = 0
- self.image = pygame.Surface((30, 30))
- self.image.fill(self.bgColor)
- self.rect = self.image.get_rect()
- if center_x == 1:
- self.rect.centerx = 50
- elif center_x == 2:
- self.rect.centerx = 125
- elif center_x == 3:
- self.rect.centerx = 200
- elif center_x == 4:
- self.rect.centerx = 275
- self.rect.centery = center_y
- def update(self):
- self.image.fill(self.bgColor)
- if self.value == 1:
- self.bgColor = (0xff, 0, 0) #Red
- elif self.value == 2:
- self.bgColor = (0, 0xff, 0) #Green
- elif self.value == 3:
- self.bgColor = (0, 0x80, 0xff) #Blue
- elif self.value == 4:
- self.bgColor = (0xff, 0xff, 0) #Yellow
- elif self.value == 5:
- self.bgColor = (0xff, 0x7e, 0) #Orange
- elif self.value == 6:
- self.bgColor = (0x41, 0, 0xff) #Purple
- def HistBox_Creation(): #Creates guess history
- list1 = {1, 2, 3, 4} #Creates 4 buttons for each history button group
- for n in list1:
- globals()["HistBtn1_"+str(n)] = HistoryBtn((n), 480)
- for n in list1:
- globals()["HistBtn2_"+str(n)] = HistoryBtn((n), 435)
- for n in list1:
- globals()["HistBtn3_"+str(n)] = HistoryBtn((n), 390)
- for n in list1:
- globals()["HistBtn4_"+str(n)] = HistoryBtn((n), 345)
- for n in list1:
- globals()["HistBtn5_"+str(n)] = HistoryBtn((n), 300)
- for n in list1:
- globals()["HistBtn6_"+str(n)] = HistoryBtn((n), 255)
- for n in list1:
- globals()["HistBtn7_"+str(n)] = HistoryBtn((n), 210)
- for n in list1:
- globals()["HistBtn8_"+str(n)] = HistoryBtn((n), 165)
- for n in list1:
- globals()["HistBtn9_"+str(n)] = HistoryBtn((n), 120)
- for n in list1:
- globals()["HistBtn10_"+str(n)] = HistoryBtn((n), 75)
- def HistBtnChange(Guess_Num): #Tracks history of player guesses
- if Guess_Num == 1:
- HistBtn1_1.value = Btn1.value
- HistBtn1_2.value = Btn2.value
- HistBtn1_3.value = Btn3.value
- HistBtn1_4.value = Btn4.value
- elif Guess_Num == 2:
- HistBtn2_1.value = Btn1.value
- HistBtn2_2.value = Btn2.value
- HistBtn2_3.value = Btn3.value
- HistBtn2_4.value = Btn4.value
- elif Guess_Num == 3:
- HistBtn3_1.value = Btn1.value
- HistBtn3_2.value = Btn2.value
- HistBtn3_3.value = Btn3.value
- HistBtn3_4.value = Btn4.value
- elif Guess_Num == 4:
- HistBtn4_1.value = Btn1.value
- HistBtn4_2.value = Btn2.value
- HistBtn4_3.value = Btn3.value
- HistBtn4_4.value = Btn4.value
- elif Guess_Num == 5:
- HistBtn5_1.value = Btn1.value
- HistBtn5_2.value = Btn2.value
- HistBtn5_3.value = Btn3.value
- HistBtn5_4.value = Btn4.value
- elif Guess_Num == 6:
- HistBtn6_1.value = Btn1.value
- HistBtn6_2.value = Btn2.value
- HistBtn6_3.value = Btn3.value
- HistBtn6_4.value = Btn4.value
- elif Guess_Num == 7:
- HistBtn7_1.value = Btn1.value
- HistBtn7_2.value = Btn2.value
- HistBtn7_3.value = Btn3.value
- HistBtn7_4.value = Btn4.value
- elif Guess_Num == 8:
- HistBtn8_1.value = Btn1.value
- HistBtn8_2.value = Btn2.value
- HistBtn8_3.value = Btn3.value
- HistBtn8_4.value = Btn4.value
- elif Guess_Num == 9:
- HistBtn9_1.value = Btn1.value
- HistBtn9_2.value = Btn2.value
- HistBtn9_3.value = Btn3.value
- HistBtn9_4.value = Btn4.value
- elif Guess_Num == 10:
- HistBtn10_1.value = Btn1.value
- HistBtn10_2.value = Btn2.value
- HistBtn10_3.value = Btn3.value
- HistBtn10_4.value = Btn4.value
- class OutputLabel(pygame.sprite.Sprite): #Label for giving player feedback
- def __init__(self, textinput):
- pygame.sprite.Sprite.__init__(self)
- self.font = pygame.font.SysFont("Arial", 20)
- self.text = textinput
- self.center = (425, 220)
- def update(self):
- self.image = self.font.render(self.text, 1, (0, 0, 0))
- self.rect = self.image.get_rect()
- self.rect.center = self.center
- def CheckGuess(Solution1, Solution2, Solution3, Solution4): #Analyzes guess and compares to solution
- CodeBroken = False
- keyscorrect = 0 #initialize number of correct guesses
- output1.text = ("") #initialize text to blank
- output2.text = ("")
- output3.text = ("")
- output4.text = ("")
- output5.text = ("")
- #Check each guess and compare with solution
- if Btn1.value == Solution1:
- keyscorrect += 1
- elif Btn1.value == Solution2 or Btn1.value == Solution3 or Btn1.value == Solution4:
- output1.text = ("Key 1: Wrong position")
- if Btn2.value == Solution2:
- keyscorrect += 1
- elif Btn2.value == Solution1 or Btn2.value == Solution3 or Btn2.value == Solution4:
- output2.text = ("Key 2: Wrong position")
- if Btn3.value == Solution3:
- keyscorrect += 1
- elif Btn3.value == Solution1 or Btn3.value == Solution2 or Btn3.value == Solution4:
- output3.text = ("Key 3: Wrong position")
- if Btn4.value == Solution4:
- keyscorrect += 1
- elif Btn4.value == Solution1 or Btn4.value == Solution2 or Btn4.value == Solution3:
- output4.text = ("Key 4: Wrong position")
- if keyscorrect == 1:
- output5.text = ("One key is correct.")
- elif keyscorrect == 2:
- output5.text = ("Two keys are correct.")
- elif keyscorrect == 3:
- output5.text = ("Three keys are correct.")
- elif keyscorrect == 4: #Checks to see if all values match the solution
- CodeBroken = True #If so, set boolean to true and end game
- else:
- CodeBroken = False
- return CodeBroken
- def show_solution(Solution1, Solution2, Solution3, Solution4):
- Btn1.value = Solution1
- Btn2.value = Solution2
- Btn3.value = Solution3
- Btn4.value = Solution4
- #Set up various UI elements
- Btn1 = Button("1")
- Btn1.rect.center = (50, 100)
- Btn1.setSelected(True)
- btnSelected = Btn1 #Set Button 1 to be selected by default when game begins
- Btn2 = Button("2")
- Btn2.rect.center = (125, 100)
- Btn3 = Button("3")
- Btn3.rect.center = (200, 100)
- Btn4 = Button("4")
- Btn4.rect.center = (275, 100)
- #Set up arrow
- arrow = pygame.image.load("arrow.png")
- arrow = arrow.convert()
- #Set up text box, logo, and history label
- txtbox = Textbox_bg()
- logo = Logo()
- HistLbl = HistoryLabel()
- #Create output text labels
- output1 = OutputLabel("")
- output1.center = (465,240)
- output2 = OutputLabel("")
- output2.center = (465,295)
- output3 = OutputLabel("")
- output3.center = (465,350)
- output4 = OutputLabel("")
- output4.center = (465,405)
- output5 = OutputLabel("")
- output5.center = (465,460)
- #Create sounds
- beep = pygame.mixer.Sound("beep.ogg")
- youwin = pygame.mixer.Sound("win.ogg")
- #Generate solution
- Solution1 = random.randint(1,6)
- Solution2 = random.randint(1,6)
- Solution3 = random.randint(1,6)
- Solution4 = random.randint(1,6)
- #Create guess history and groups
- HistBox_Creation()
- HistRow1 = pygame.sprite.Group()
- HistRow1.add(HistBtn1_1, HistBtn1_2, HistBtn1_3, HistBtn1_4)
- HistRow2 = pygame.sprite.Group()
- HistRow2.add(HistBtn2_1, HistBtn2_2, HistBtn2_3, HistBtn2_4)
- HistRow3 = pygame.sprite.Group()
- HistRow3.add(HistBtn3_1, HistBtn3_2, HistBtn3_3, HistBtn3_4)
- HistRow4 = pygame.sprite.Group()
- HistRow4.add(HistBtn4_1, HistBtn4_2, HistBtn4_3, HistBtn4_4)
- HistRow5 = pygame.sprite.Group()
- HistRow5.add(HistBtn5_1, HistBtn5_2, HistBtn5_3, HistBtn5_4)
- HistRow6 = pygame.sprite.Group()
- HistRow6.add(HistBtn6_1, HistBtn6_2, HistBtn6_3, HistBtn6_4)
- HistRow7 = pygame.sprite.Group()
- HistRow7.add(HistBtn7_1, HistBtn7_2, HistBtn7_3, HistBtn7_4)
- HistRow8 = pygame.sprite.Group()
- HistRow8.add(HistBtn8_1, HistBtn8_2, HistBtn8_3, HistBtn8_4)
- HistRow9 = pygame.sprite.Group()
- HistRow9.add(HistBtn9_1, HistBtn9_2, HistBtn9_3, HistBtn9_4)
- HistRow10 = pygame.sprite.Group()
- HistRow10.add(HistBtn10_1, HistBtn10_2, HistBtn10_3, HistBtn10_4)
- #main
- keepGoing = True
- txtboxdraw = pygame.sprite.RenderUpdates(txtbox)
- allSprites = pygame.sprite.RenderUpdates(Btn1, Btn2, Btn3, Btn4, \
- output1, output2, output3, output4, output5, \
- logo, HistLbl, \
- HistRow1, HistRow2, HistRow3, HistRow4, \
- HistRow5, HistRow6, HistRow7, HistRow8, \
- HistRow9, HistRow10)
- Box(background)
- screen.blit(background, (0, 0))
- screen.blit(arrow, (25,580)) #Arrow's default position is button 1
- Guess_Num = 0 #Initialize the guess number
- while keepGoing:
- clock.tick(30)
- KeyStop = False #Used to stop key-repeating
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- keepGoing = False
- elif event.type == pygame.KEYUP: #Checks for releasing keys
- if event.key == pygame.K_UP:
- KeyStop = False
- elif event.key == pygame.K_DOWN:
- KeyStop = False
- elif event.key == pygame.K_RETURN:
- KeyStop = False
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_RIGHT:
- if btnSelected == Btn1:
- btnSelected = Btn2
- screen.blit(background, (0, 0))
- screen.blit(arrow, (100,580))
- elif btnSelected == Btn2:
- btnSelected = Btn3
- screen.blit(background, (0, 0))
- screen.blit(arrow, (175,580))
- elif btnSelected == Btn3:
- btnSelected = Btn4
- screen.blit(background, (0, 0))
- screen.blit(arrow, (250,580))
- elif btnSelected == Btn4:
- btnSelected = Btn1
- screen.blit(background, (0, 0))
- screen.blit(arrow, (25,580))
- Btn1.setSelected(False)
- Btn2.setSelected(False)
- Btn3.setSelected(False)
- Btn4.setSelected(False)
- btnSelected.setSelected(True)
- elif event.key == pygame.K_LEFT:
- if btnSelected == Btn1:
- btnSelected = Btn4
- screen.blit(background, (0, 0))
- screen.blit(arrow, (250,580))
- elif btnSelected == Btn2:
- btnSelected = Btn1
- screen.blit(background, (0, 0))
- screen.blit(arrow, (25,580))
- elif btnSelected == Btn3:
- btnSelected = Btn2
- screen.blit(background, (0, 0))
- screen.blit(arrow, (100,580))
- elif btnSelected == Btn4:
- btnSelected = Btn3
- screen.blit(background, (0, 0))
- screen.blit(arrow, (175,580))
- Btn1.setSelected(False)
- Btn2.setSelected(False)
- Btn3.setSelected(False)
- Btn4.setSelected(False)
- btnSelected.setSelected(True)
- elif event.key == pygame.K_UP:
- if KeyStop == False: #If key is not already being pressed, then proceed
- KeyStop = True
- btnSelected.nextchoice()
- elif event.key == pygame.K_DOWN:
- if KeyStop == False:
- KeyStop = True
- btnSelected.prevchoice()
- elif event.key == pygame.K_RETURN:
- if KeyStop == False:
- KeyStop = True
- Guess_Num += 1
- HistBtnChange(Guess_Num)
- CodeBroken = False
- CodeBroken = CheckGuess(Solution1, Solution2, Solution3, Solution4)
- if CodeBroken == True:
- youwin.play()
- output1.text = ("")
- output2.text = ("You win!")
- output3.text = ("")
- output4.text = ("You broke the code!")
- output4.center = (465,435)
- output5.text = ("")
- elif Guess_Num < 10:
- beep.play()
- else: #10 guesses used, game over
- output1.text = ("Out of guesses!")
- output2.text = ("Game Over")
- output3.text = ("")
- output4.text = ("Solution shown")
- output5.text = ("")
- show_solution(Solution1, Solution2, Solution3, Solution4)
- elif event.key == pygame.K_ESCAPE: #End program on escape key
- keepGoing = False
- allSprites.clear(screen, background)
- allSprites.update()
- txtboxdraw.draw(screen)
- allSprites.draw(screen)
- pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement