class WinScreen: def __init__(self, manager, screen, highscores): self.stateManager = manager self.window = screen self.highscores = highscores self.newHighScore = False self.uiContainer = UIContainer(self.window) self.uiContainer.horizontalStride = 0 self.uiContainer.verticalStride = 0 self.__selector = None self.titleFont = pygame.font.SysFont("monospace", 75, bold=True) self.textFont = pygame.font.SysFont("monospace", 25) self.textColour = (128, 128, 128) self.screenInfo = pygame.display.Info() pass def start(self): global gScore if self.highscores.compareHighScores(gScore): if self.__selector is None: self.__selector = self.uiContainer.add_name_selector(10, 10) self.__selector.setName(getRandomName()) self.__selector.rect = pygame.Rect(610, 537, 0, 0) else: self.__selector.enable() self.newHighScore = True else: self.newHighScore = False if self.__selector is not None: self.__selector.disable() def update(self): if xo_input.btn_check: if self.__selector is not None: global gScore self.highscores.addHighScore(gScore, self.__selector.getName()) self.stateManager.switchGameState("MainScreen") self.uiContainer.update() def draw(self): global gScore self.drawText("Game Over!", self.titleFont, 0, -50) self.drawText("Final Score: " + str(gScore), self.titleFont, 0, 50) if self.newHighScore: self.drawText("Please enter your name: ", self.textFont, -120, 150) self.uiContainer.draw() def final(self): if self.__selector is not None: self.__selector.setName("") def drawText(self, text, font, offsetX, offsetY): label = font.render(text, 1, self.textColour) text_width, text_height = font.size(text) text_x = (self.screenInfo.current_w/2) - (text_width/2) + offsetX text_y = (self.screenInfo.current_h/2) - (text_height/2) + offsetY self.window.blit(label, (text_x, text_y))
class WinScreen: def __init__(self, manager, screen, highscores): self.stateManager = manager self.window = screen self.highscores = highscores self.newHighScore = False self.uiContainer = UIContainer(self.window) self.uiContainer.horizontalStride = 0 self.uiContainer.verticalStride = 0 self.__selector = None self.titleFont = pygame.font.SysFont("monospace", 75, bold=True) self.textFont = pygame.font.SysFont("monospace", 25) self.textColour = (128, 128, 128) self.screenInfo = pygame.display.Info() pass def start(self): global gScore if self.highscores.compareHighScores(gScore): if self.__selector is None: self.__selector = self.uiContainer.add_name_selector(10, 10) self.__selector.setName(getRandomName()) self.__selector.rect = pygame.Rect(610, 537, 0, 0) else: self.__selector.enable() self.newHighScore = True else: self.newHighScore = False if self.__selector is not None: self.__selector.disable() def update(self): if xo_input.btn_check: if self.__selector is not None: global gScore self.highscores.addHighScore(gScore, self.__selector.getName()) self.stateManager.switchGameState("MainScreen") self.uiContainer.update() def draw(self): global gScore self.drawText("Game Over!", self.titleFont, 0, -50) self.drawText("Final Score: " + str(gScore), self.titleFont, 0, 50) if self.newHighScore: self.drawText("Please enter your name: ", self.textFont, -120, 150) self.uiContainer.draw() def final(self): if self.__selector is not None: self.__selector.setName("") def drawText(self, text, font, offsetX, offsetY): label = font.render(text, 1, self.textColour) text_width, text_height = font.size(text) text_x = (self.screenInfo.current_w / 2) - (text_width / 2) + offsetX text_y = (self.screenInfo.current_h / 2) - (text_height / 2) + offsetY self.window.blit(label, (text_x, text_y))
def __init__(self, manager, screen): self.stateManager = manager self.window = screen self.uiContainer = UIContainer(self.window) self.uiContainer.verticalStride = 0 self.screenInfo = pygame.display.Info() self.titleFont = pygame.font.SysFont("monospace", 75, bold=True) self.scoreFont = pygame.font.SysFont("monospace", 45) self.textColour = (128, 128, 128) self.initiateReset = False # Create the message box centerX = self.screenInfo.current_w / 2 centerY = self.screenInfo.current_h / 2 # Button sizes width = 170 height = 40 # Back button self.backButton = self.uiContainer.add_button("Back") self.backButton.rect = pygame.Rect( (110 - (width / 2), self.screenInfo.current_h - 50 - (height / 2), width, height)) self.backButton.baseColour = (0, 0, 0) hoverFill = 1 hoverColour = (255, 255, 255) clickColour = (128, 128, 128) for button in self.uiContainer.components: if isinstance(button, Button): button.hoverColour = hoverColour button.clickColour = clickColour button.hoverFill = hoverFill button.font = pygame.font.SysFont("monospace", 30) # Load title image if sys.platform == "win32": self.image = pygame.image.load( "WinMedia/Images/instructions.png").convert() else: self.image = pygame.image.load( "media/Images/instructions.png").convert() pass
def __init__(self, manager, screen, highscores): self.stateManager = manager self.window = screen self.highscores = highscores self.newHighScore = False self.uiContainer = UIContainer(self.window) self.uiContainer.horizontalStride = 0 self.uiContainer.verticalStride = 0 self.__selector = None self.titleFont = pygame.font.SysFont("monospace", 75, bold=True) self.textFont = pygame.font.SysFont("monospace", 25) self.textColour = (128, 128, 128) self.screenInfo = pygame.display.Info() pass
def __init__(self, manager, screen): self.stateManager = manager self.window = screen self.uiContainer = UIContainer(self.window) self.uiContainer.horizontalStride = 0 self.screenInfo = pygame.display.Info() # Button sizes and positions width = 250 height = 60 cx = self.screenInfo.current_w / 2 cy = self.screenInfo.current_h / 2 bx = cx - (width / 2) padding = 30 dy = padding / 2 + height # Start game button self.gameButton = self.uiContainer.add_button("Start Game", bx, 0, width, height) self.gameButton.rect.y = cy - height * 2 - padding / 2 + 30 # Instructions button self.instructionsButton = self.uiContainer.add_button("Instructions", bx, 0, width, height) self.instructionsButton.rect.y = self.gameButton.rect.y + dy # High Scores button self.highScoresButton = self.uiContainer.add_button("High Scores", bx, 0, width, height) self.highScoresButton.rect.y = self.instructionsButton.rect.y + dy # About button self.aboutButton = self.uiContainer.add_button("About", bx, 0, width, height) self.aboutButton.rect.y = self.highScoresButton.rect.y + dy self.quitButton = self.uiContainer.add_button("Quit", bx, 0, width, height) self.quitButton.rect.y = self.aboutButton.rect.y + dy # Set button colors baseColor = (0, 0, 0) hoverColor = (255, 255, 255) clickColor = (128, 128, 128) hoverFill = 1 for button in self.uiContainer.components: if isinstance(button, Button): button.font = pygame.font.SysFont("monospace", 30) button.baseColour = baseColor button.hoverColour = hoverColor button.clickColour = clickColor button.hoverFill = hoverFill # Load title image if sys.platform == "win32": self.titleImage = pygame.image.load("WinMedia/Images/titleXO.png").convert() else: self.titleImage = pygame.image.load("media/Images/titleXO.png").convert()
def __init__(self, window, message, width, height, x, y): self.message = message self.window = window self.width = width self.height = height self.X = x self.Y = y self.topleft = [x - width / 2, y - height / 2] self.uiContainer = UIContainer(self.window) self.baseColour = (16, 16, 16) self.borderColour = (255, 255, 255) self.textColour = (128, 128, 128) self.textFont = pygame.font.SysFont("monospace", 20) self.padding = 20 pass
def __init__(self, manager, highScoreManager, screen): self.stateManager = manager self.window = screen self.highScoreManager = highScoreManager self.uiContainer = UIContainer(self.window) self.uiContainer.horizontalStride = 5 self.titleFont = pygame.font.SysFont("monospace", 45, bold=True) self.scoreFont = pygame.font.SysFont("monospace", 30) self.textColour = (128, 128, 128) self.screenInfo = pygame.display.Info() self.addScore = 20 self.scoreLoss = 5 self.wrongAnswers = 0 self.rightAnswers = 0 self.totalRightAnswers = 0 self.score = 0 pass
def __init__(self, manager, screen): self.stateManager = manager self.window = screen self.uiContainer = UIContainer(self.window) self.uiContainer.verticalStride = 0 self.screenInfo = pygame.display.Info() self.titleFont = pygame.font.SysFont("monospace", 75, bold=True) self.scoreFont = pygame.font.SysFont("monospace", 45) self.textColour = (128, 128, 128) self.initiateReset = False # Create the message box centerX = self.screenInfo.current_w / 2 centerY = self.screenInfo.current_h / 2 # Button sizes width = 170 height = 40 # Back button self.backButton = self.uiContainer.add_button("Back") self.backButton.rect = pygame.Rect( (110 - (width / 2), self.screenInfo.current_h - 50 - (height / 2), width, height)) self.backButton.baseColour = (0, 0, 0) hoverFill = 1 hoverColour = (255, 255, 255) clickColour = (128, 128, 128) for button in self.uiContainer.components: if isinstance(button, Button): button.hoverColour = hoverColour button.clickColour = clickColour button.hoverFill = hoverFill button.font = pygame.font.SysFont("monospace", 30) # Load title image if sys.platform == "win32": self.image = pygame.image.load("WinMedia/Images/instructions.png").convert() else: self.image = pygame.image.load("media/Images/instructions.png").convert() pass
def __init__(self, window, message, width, height, x, y): self.message = message self.window = window self.width = width self.height = height self.X = x self.Y = y self.topleft = [x - width/2, y - height/2] self.uiContainer = UIContainer(self.window) self.baseColour = (16, 16, 16) self.borderColour = (255, 255, 255) self.textColour = (128, 128, 128) self.textFont = pygame.font.SysFont("monospace", 20) self.padding = 20; pass
def __init__(self, manager, scoreManager, screen): self.stateManager = manager self.highScoreManager = scoreManager self.window = screen self.uiContainer = UIContainer(self.window) self.uiContainer.verticalStride = 0 self.screenInfo = pygame.display.Info() self.titleFont = pygame.font.SysFont("monospace", 75, bold=True) self.scoreFont = pygame.font.SysFont("monospace", 45) self.textColour = (128, 128, 128) self.initiateReset = False # Create the message box centerX = self.screenInfo.current_w / 2 centerY = self.screenInfo.current_h / 2 message = "WARNING: That action cannot be undone. Are you positive you want to reset the scores to their default state?" self.resetMessage = MessageWindow(self.window, message, 650, 300, centerX, centerY) # Get most recent scores self.highScores = self.highScoreManager.getCurrentHighScores() self.initialHighScoreLoad = True self.updateHighScores() # Button sizes width = 170 height = 40 # Back button self.backButton = self.uiContainer.add_button("Back") self.backButton.rect = pygame.Rect( (110 - (width / 2), self.screenInfo.current_h - 50 - (height / 2), width, height)) # Reset button self.resetButton = self.uiContainer.add_button("Reset Highscores") self.resetButton.rect = pygame.Rect( self.screenInfo.current_w - 280 - (width / 2), self.screenInfo.current_h - 50 - (height / 2), width * 2, height) self.reset_acceptButton = self.resetMessage.addButton( "Yes, I'm sure", (-285, 95), (260, 30)) self.reset_cancelButton = self.resetMessage.addButton( "No", (165, 95), (130, 30)) self.backButton.baseColour = self.resetButton.baseColour = (0, 0, 0) self.reset_cancelButton.baseColour = self.reset_acceptButton.baseColour = ( 16, 16, 16) self.resetMessage.textFont = pygame.font.SysFont("monospace", 30) hoverFill = 1 hoverColour = (255, 255, 255) clickColour = (128, 128, 128) for button in self.uiContainer.components: if isinstance(button, Button): button.hoverColour = hoverColour button.clickColour = clickColour button.hoverFill = hoverFill button.font = pygame.font.SysFont("monospace", 30) for button in self.resetMessage.uiContainer.components: if isinstance(button, Button): button.hoverColour = hoverColour button.clickColour = clickColour button.hoverFill = hoverFill button.font = pygame.font.SysFont("monospace", 30)
class HighScoreScreen: def __init__(self, manager, scoreManager, screen): self.stateManager = manager self.highScoreManager = scoreManager self.window = screen self.uiContainer = UIContainer(self.window) self.uiContainer.verticalStride = 0 self.screenInfo = pygame.display.Info() self.titleFont = pygame.font.SysFont("monospace", 75, bold=True) self.scoreFont = pygame.font.SysFont("monospace", 45) self.textColour = (128, 128, 128) self.initiateReset = False # Create the message box centerX = self.screenInfo.current_w / 2 centerY = self.screenInfo.current_h / 2 message = "WARNING: That action cannot be undone. Are you positive you want to reset the scores to their default state?" self.resetMessage = MessageWindow(self.window, message, 650, 300, centerX, centerY) # Get most recent scores self.highScores = self.highScoreManager.getCurrentHighScores() self.initialHighScoreLoad = True self.updateHighScores() # Button sizes width = 170 height = 40 # Back button self.backButton = self.uiContainer.add_button("Back") self.backButton.rect = pygame.Rect( (110 - (width / 2), self.screenInfo.current_h - 50 - (height / 2), width, height)) # Reset button self.resetButton = self.uiContainer.add_button("Reset Highscores") self.resetButton.rect = pygame.Rect( self.screenInfo.current_w - 280 - (width / 2), self.screenInfo.current_h - 50 - (height / 2), width * 2, height) self.reset_acceptButton = self.resetMessage.addButton( "Yes, I'm sure", (-285, 95), (260, 30)) self.reset_cancelButton = self.resetMessage.addButton( "No", (165, 95), (130, 30)) self.backButton.baseColour = self.resetButton.baseColour = (0, 0, 0) self.reset_cancelButton.baseColour = self.reset_acceptButton.baseColour = ( 16, 16, 16) self.resetMessage.textFont = pygame.font.SysFont("monospace", 30) hoverFill = 1 hoverColour = (255, 255, 255) clickColour = (128, 128, 128) for button in self.uiContainer.components: if isinstance(button, Button): button.hoverColour = hoverColour button.clickColour = clickColour button.hoverFill = hoverFill button.font = pygame.font.SysFont("monospace", 30) for button in self.resetMessage.uiContainer.components: if isinstance(button, Button): button.hoverColour = hoverColour button.clickColour = clickColour button.hoverFill = hoverFill button.font = pygame.font.SysFont("monospace", 30) def start(self): if self.initialHighScoreLoad: self.initialHighScoreLoad = False else: self.updateHighScores() # Reset the reset variable - lol self.initiateReset = False pass def updateHighScores(self): """ Updates the high score tuple. """ self.highScores = self.highScoreManager.getCurrentHighScores() self.highScoreTuples = ((self.highScores["first"]["name"], self.highScores["first"]["score"]), (self.highScores["second"]["name"], self.highScores["second"]["score"]), (self.highScores["third"]["name"], self.highScores["third"]["score"]), (self.highScores["fourth"]["name"], self.highScores["fourth"]["score"]), (self.highScores["fifth"]["name"], self.highScores["fifth"]["score"]), (self.highScores["sixth"]["name"], self.highScores["sixth"]["score"]), (self.highScores["seventh"]["name"], self.highScores["seventh"]["score"]), (self.highScores["eighth"]["name"], self.highScores["eighth"]["score"]), (self.highScores["ninth"]["name"], self.highScores["ninth"]["score"]), (self.highScores["tenth"]["name"], self.highScores["tenth"]["score"])) def update(self): self.updateHighScores() if not self.initiateReset: if self.backButton.was_pressed() or xo_input.escape: self.stateManager.switchGameState("MainScreen") if self.resetButton.was_pressed(): self.initiateReset = True self.uiContainer.update() else: self.resetMessage.update() if self.reset_cancelButton.was_pressed(): self.initiateReset = False if self.reset_acceptButton.was_pressed(): self.highScoreManager.resetHighScores() self.updateHighScores() self.initiateReset = False def draw(self): # Draw the high scores self.drawText("HIGH SCORES", self.titleFont, 0, -225) y = -165 for tuple in self.highScoreTuples: self.drawTextWithTuple(tuple, self.scoreFont, 0, y) y += 45 # Draw the UI self.uiContainer.draw() # Draw the reset message if self.initiateReset: self.resetMessage.draw() def final(self): pass def drawText(self, text, font, offsetX, offsetY): label = font.render(text, 1, self.textColour) text_width, text_height = font.size(text) self.window.blit( label, ((self.screenInfo.current_w / 2) - (text_width / 2) + offsetX, (self.screenInfo.current_h / 2) - (text_height / 2) + offsetY)) def drawTextWithTuple(self, tuple, font, offsetX, offsetY): text_width, text_height = font.size(tuple[0]) text = str(tuple[0]) + " : " + str(tuple[1]) label = font.render(text, 1, self.textColour) self.window.blit( label, ((self.screenInfo.current_w / 2) - text_width + offsetX, (self.screenInfo.current_h / 2) - (text_height / 2) + offsetY))
class AboutScreen: def __init__(self, manager, screen): self.stateManager = manager self.window = screen self.uiContainer = UIContainer(self.window) self.uiContainer.verticalStride = 0 self.screenInfo = pygame.display.Info() self.titleFont = pygame.font.SysFont("monospace", 75, bold=True) self.scoreFont = pygame.font.SysFont("monospace", 45) self.textColour = (128, 128, 128) self.initiateReset = False # Create the message box centerX = self.screenInfo.current_w / 2 centerY = self.screenInfo.current_h / 2 # Button sizes width = 170 height = 40 # Back button self.backButton = self.uiContainer.add_button("Back") self.backButton.rect = pygame.Rect( (110 - (width / 2), self.screenInfo.current_h - 50 - (height / 2), width, height)) self.backButton.baseColour = (0, 0, 0) hoverFill = 1 hoverColour = (255, 255, 255) clickColour = (128, 128, 128) for button in self.uiContainer.components: if isinstance(button, Button): button.hoverColour = hoverColour button.clickColour = clickColour button.hoverFill = hoverFill button.font = pygame.font.SysFont("monospace", 30) pass def start(self): pass def update(self): if xo_input.escape or self.backButton.was_pressed(): self.stateManager.switchGameState("MainScreen") self.uiContainer.update() def draw(self): self.uiContainer.draw() self.drawTextCentered("About Number Munchers", self.titleFont, 0, -275) self.drawText("Created By:", self.scoreFont, 10, -150) self.drawText("Richard Selneck: UI and Gameplay Programmer", self.scoreFont, 10, -50) self.drawText("Steven Siewart: AI Programmer", self.scoreFont, 10, 25) self.drawText("Tyler Sargent: UI and Gameplay Programmer", self.scoreFont, 10, 100) self.drawText("Westley Waligora: UI and Gameplay Programmer", self.scoreFont, 10, 175) def final(self): pass def drawTextCentered(self, text, font, offsetX, offsetY): label = font.render(text, 1, self.textColour) text_width, text_height = font.size(text) self.window.blit( label, ((self.screenInfo.current_w / 2) - (text_width / 2) + offsetX, (self.screenInfo.current_h / 2) - (text_height / 2) + offsetY)) def drawText(self, text, font, offsetX, offsetY): label = font.render(text, 1, self.textColour) text_width, text_height = font.size(text) self.window.blit(label, (offsetX, (self.screenInfo.current_h / 2) - (text_height / 2) + offsetY))
class MessageWindow: def __init__(self, window, message, width, height, x, y): self.message = message self.window = window self.width = width self.height = height self.X = x self.Y = y self.topleft = [x - width / 2, y - height / 2] self.uiContainer = UIContainer(self.window) self.baseColour = (16, 16, 16) self.borderColour = (255, 255, 255) self.textColour = (128, 128, 128) self.textFont = pygame.font.SysFont("monospace", 20) self.padding = 20 pass def addButton(self, text, pos, size): button = self.uiContainer.add_button() button.rect = pygame.Rect([self.X + pos[0], self.Y + pos[1]], size) button.text = text return button def update(self): # Update the UI self.uiContainer.update() pass def draw(self): # Draw the background for the window pygame.draw.rect(self.window, self.baseColour, pygame.Rect(self.topleft, (self.width, self.height)), 0) # Draw the border for the window pygame.draw.rect(self.window, self.borderColour, pygame.Rect(self.topleft, (self.width, self.height)), 1) # Render the message self.drawText(self.message, self.textFont, 0, 0) # Draw the UI self.uiContainer.draw() pass def drawText(self, text, font, offsetX, offsetY): # Only works with a fixed-width font # Python text-wrap takes a character width for wrapping, not a pixel width # Which is dumb charWidth = (self.width - self.padding) / (font.size('a')[0]) wrappedText = textwrap.wrap(text, charWidth) counter = 0 for line in wrappedText: label = font.render(line, 1, self.textColour) text_width, text_height = font.size(line) self.window.blit( label, ((self.width / 2) - (text_width / 2) + offsetX + self.topleft[0], self.padding - (text_height / 2) + offsetY + self.topleft[1] + (counter * text_height))) counter += 1
def __init__(self, manager, scoreManager, screen): self.stateManager = manager self.highScoreManager = scoreManager self.window = screen self.uiContainer = UIContainer(self.window) self.uiContainer.verticalStride = 0 self.screenInfo = pygame.display.Info() self.titleFont = pygame.font.SysFont("monospace", 75, bold=True) self.scoreFont = pygame.font.SysFont("monospace", 45) self.textColour = (128, 128, 128) self.initiateReset = False # Create the message box centerX = self.screenInfo.current_w / 2 centerY = self.screenInfo.current_h / 2 message = "WARNING: That action cannot be undone. Are you positive you want to reset the scores to their default state?" self.resetMessage = MessageWindow(self.window, message, 650, 300, centerX, centerY) # Get most recent scores self.highScores = self.highScoreManager.getCurrentHighScores() self.initialHighScoreLoad = True self.updateHighScores() # Button sizes width = 170 height = 40 # Back button self.backButton = self.uiContainer.add_button("Back") self.backButton.rect = pygame.Rect((110 - (width / 2), self.screenInfo.current_h - 50 - (height / 2), width, height)) # Reset button self.resetButton = self.uiContainer.add_button("Reset Highscores") self.resetButton.rect = pygame.Rect(self.screenInfo.current_w - 280 - (width / 2), self.screenInfo.current_h - 50 - (height / 2), width*2, height) self.reset_acceptButton = self.resetMessage.addButton("Yes, I'm sure", (-285, 95), (260, 30)) self.reset_cancelButton = self.resetMessage.addButton("No", (165, 95), (130, 30)) self.backButton.baseColour = self.resetButton.baseColour = (0, 0, 0) self.reset_cancelButton.baseColour = self.reset_acceptButton.baseColour = (16, 16, 16) self.resetMessage.textFont = pygame.font.SysFont("monospace", 30) hoverFill = 1 hoverColour = (255, 255, 255) clickColour = (128, 128, 128) for button in self.uiContainer.components: if isinstance(button, Button): button.hoverColour = hoverColour button.clickColour = clickColour button.hoverFill = hoverFill button.font = pygame.font.SysFont("monospace", 30) for button in self.resetMessage.uiContainer.components: if isinstance(button, Button): button.hoverColour = hoverColour button.clickColour = clickColour button.hoverFill = hoverFill button.font = pygame.font.SysFont("monospace", 30)
class HighScoreScreen: def __init__(self, manager, scoreManager, screen): self.stateManager = manager self.highScoreManager = scoreManager self.window = screen self.uiContainer = UIContainer(self.window) self.uiContainer.verticalStride = 0 self.screenInfo = pygame.display.Info() self.titleFont = pygame.font.SysFont("monospace", 75, bold=True) self.scoreFont = pygame.font.SysFont("monospace", 45) self.textColour = (128, 128, 128) self.initiateReset = False # Create the message box centerX = self.screenInfo.current_w / 2 centerY = self.screenInfo.current_h / 2 message = "WARNING: That action cannot be undone. Are you positive you want to reset the scores to their default state?" self.resetMessage = MessageWindow(self.window, message, 650, 300, centerX, centerY) # Get most recent scores self.highScores = self.highScoreManager.getCurrentHighScores() self.initialHighScoreLoad = True self.updateHighScores() # Button sizes width = 170 height = 40 # Back button self.backButton = self.uiContainer.add_button("Back") self.backButton.rect = pygame.Rect((110 - (width / 2), self.screenInfo.current_h - 50 - (height / 2), width, height)) # Reset button self.resetButton = self.uiContainer.add_button("Reset Highscores") self.resetButton.rect = pygame.Rect(self.screenInfo.current_w - 280 - (width / 2), self.screenInfo.current_h - 50 - (height / 2), width*2, height) self.reset_acceptButton = self.resetMessage.addButton("Yes, I'm sure", (-285, 95), (260, 30)) self.reset_cancelButton = self.resetMessage.addButton("No", (165, 95), (130, 30)) self.backButton.baseColour = self.resetButton.baseColour = (0, 0, 0) self.reset_cancelButton.baseColour = self.reset_acceptButton.baseColour = (16, 16, 16) self.resetMessage.textFont = pygame.font.SysFont("monospace", 30) hoverFill = 1 hoverColour = (255, 255, 255) clickColour = (128, 128, 128) for button in self.uiContainer.components: if isinstance(button, Button): button.hoverColour = hoverColour button.clickColour = clickColour button.hoverFill = hoverFill button.font = pygame.font.SysFont("monospace", 30) for button in self.resetMessage.uiContainer.components: if isinstance(button, Button): button.hoverColour = hoverColour button.clickColour = clickColour button.hoverFill = hoverFill button.font = pygame.font.SysFont("monospace", 30) def start(self): if self.initialHighScoreLoad: self.initialHighScoreLoad = False else: self.updateHighScores() # Reset the reset variable - lol self.initiateReset = False pass def updateHighScores(self): """ Updates the high score tuple. """ self.highScores = self.highScoreManager.getCurrentHighScores() self.highScoreTuples = ((self.highScores["first"]["name"], self.highScores["first"]["score"]), (self.highScores["second"]["name"], self.highScores["second"]["score"]), (self.highScores["third"]["name"], self.highScores["third"]["score"]), (self.highScores["fourth"]["name"], self.highScores["fourth"]["score"]), (self.highScores["fifth"]["name"], self.highScores["fifth"]["score"]), (self.highScores["sixth"]["name"], self.highScores["sixth"]["score"]), (self.highScores["seventh"]["name"], self.highScores["seventh"]["score"]), (self.highScores["eighth"]["name"], self.highScores["eighth"]["score"]), (self.highScores["ninth"]["name"], self.highScores["ninth"]["score"]), (self.highScores["tenth"]["name"], self.highScores["tenth"]["score"])) def update(self): self.updateHighScores() if not self.initiateReset: if self.backButton.was_pressed() or xo_input.escape: self.stateManager.switchGameState("MainScreen") if self.resetButton.was_pressed(): self.initiateReset = True self.uiContainer.update() else: self.resetMessage.update() if self.reset_cancelButton.was_pressed(): self.initiateReset = False if self.reset_acceptButton.was_pressed(): self.highScoreManager.resetHighScores() self.updateHighScores() self.initiateReset = False def draw(self): # Draw the high scores self.drawText("HIGH SCORES", self.titleFont, 0, -225) y = -165 for tuple in self.highScoreTuples: self.drawTextWithTuple(tuple, self.scoreFont, 0, y) y += 45 # Draw the UI self.uiContainer.draw() # Draw the reset message if self.initiateReset: self.resetMessage.draw() def final(self): pass def drawText(self, text, font, offsetX, offsetY): label = font.render(text, 1, self.textColour) text_width, text_height = font.size(text) self.window.blit(label, ((self.screenInfo.current_w/2) - (text_width/2) + offsetX, (self.screenInfo.current_h/2) - (text_height/2) + offsetY)) def drawTextWithTuple(self, tuple, font, offsetX, offsetY): text_width, text_height = font.size(tuple[0]) text = str(tuple[0]) + " : " + str(tuple[1]) label = font.render(text, 1, self.textColour) self.window.blit(label, ((self.screenInfo.current_w/2) - text_width + offsetX, (self.screenInfo.current_h/2) - (text_height/2) + offsetY))
class MainScreen: """ Defines the main menu screen """ def __init__(self, manager, screen): self.stateManager = manager self.window = screen self.uiContainer = UIContainer(self.window) self.uiContainer.horizontalStride = 0 self.screenInfo = pygame.display.Info() # Button sizes and positions width = 250 height = 60 cx = self.screenInfo.current_w / 2 cy = self.screenInfo.current_h / 2 bx = cx - (width / 2) padding = 30 dy = padding / 2 + height # Start game button self.gameButton = self.uiContainer.add_button("Start Game", bx, 0, width, height) self.gameButton.rect.y = cy - height * 2 - padding / 2 + 30 # Instructions button self.instructionsButton = self.uiContainer.add_button( "Instructions", bx, 0, width, height) self.instructionsButton.rect.y = self.gameButton.rect.y + dy # High Scores button self.highScoresButton = self.uiContainer.add_button( "High Scores", bx, 0, width, height) self.highScoresButton.rect.y = self.instructionsButton.rect.y + dy # About button self.aboutButton = self.uiContainer.add_button("About", bx, 0, width, height) self.aboutButton.rect.y = self.highScoresButton.rect.y + dy self.quitButton = self.uiContainer.add_button("Quit", bx, 0, width, height) self.quitButton.rect.y = self.aboutButton.rect.y + dy # Set button colors baseColor = (0, 0, 0) hoverColor = (255, 255, 255) clickColor = (128, 128, 128) hoverFill = 1 for button in self.uiContainer.components: if isinstance(button, Button): button.font = pygame.font.SysFont("monospace", 30) button.baseColour = baseColor button.hoverColour = hoverColor button.clickColour = clickColor button.hoverFill = hoverFill # Load title image if sys.platform == "win32": self.titleImage = pygame.image.load( "WinMedia/Images/titleXO.png").convert() else: self.titleImage = pygame.image.load( "media/Images/titleXO.png").convert() def start(self): pass def update(self): if xo_input.escape: exit_game(1) if self.gameButton.was_pressed(): self.stateManager.switchGameState("GameScreen") if self.instructionsButton.was_pressed(): self.stateManager.switchGameState("InstructionsScreen") if self.highScoresButton.was_pressed(): self.stateManager.switchGameState("HighScoreScreen") if self.aboutButton.was_pressed(): self.stateManager.switchGameState("AboutScreen") if self.quitButton.was_pressed(): exit_game(1) self.uiContainer.update() def draw(self): self.window.blit(self.titleImage, ((self.screenInfo.current_w / 2) - (self.titleImage.get_rect().width / 2), 50)) # UI needs to be drawn LAST self.uiContainer.draw() def final(self): pass
def __init__(self, manager, screen): self.stateManager = manager self.window = screen self.uiContainer = UIContainer(self.window) self.uiContainer.horizontalStride = 0 self.screenInfo = pygame.display.Info() # Button sizes and positions width = 250 height = 60 cx = self.screenInfo.current_w / 2 cy = self.screenInfo.current_h / 2 bx = cx - (width / 2) padding = 30 dy = padding / 2 + height # Start game button self.gameButton = self.uiContainer.add_button("Start Game", bx, 0, width, height) self.gameButton.rect.y = cy - height * 2 - padding / 2 + 30 # Instructions button self.instructionsButton = self.uiContainer.add_button( "Instructions", bx, 0, width, height) self.instructionsButton.rect.y = self.gameButton.rect.y + dy # High Scores button self.highScoresButton = self.uiContainer.add_button( "High Scores", bx, 0, width, height) self.highScoresButton.rect.y = self.instructionsButton.rect.y + dy # About button self.aboutButton = self.uiContainer.add_button("About", bx, 0, width, height) self.aboutButton.rect.y = self.highScoresButton.rect.y + dy self.quitButton = self.uiContainer.add_button("Quit", bx, 0, width, height) self.quitButton.rect.y = self.aboutButton.rect.y + dy # Set button colors baseColor = (0, 0, 0) hoverColor = (255, 255, 255) clickColor = (128, 128, 128) hoverFill = 1 for button in self.uiContainer.components: if isinstance(button, Button): button.font = pygame.font.SysFont("monospace", 30) button.baseColour = baseColor button.hoverColour = hoverColor button.clickColour = clickColor button.hoverFill = hoverFill # Load title image if sys.platform == "win32": self.titleImage = pygame.image.load( "WinMedia/Images/titleXO.png").convert() else: self.titleImage = pygame.image.load( "media/Images/titleXO.png").convert()
class AboutScreen: def __init__(self, manager, screen): self.stateManager = manager self.window = screen self.uiContainer = UIContainer(self.window) self.uiContainer.verticalStride = 0 self.screenInfo = pygame.display.Info() self.titleFont = pygame.font.SysFont("monospace", 75, bold=True) self.scoreFont = pygame.font.SysFont("monospace", 45) self.textColour = (128, 128, 128) self.initiateReset = False # Create the message box centerX = self.screenInfo.current_w / 2 centerY = self.screenInfo.current_h / 2 # Button sizes width = 170 height = 40 # Back button self.backButton = self.uiContainer.add_button("Back") self.backButton.rect = pygame.Rect( (110 - (width / 2), self.screenInfo.current_h - 50 - (height / 2), width, height)) self.backButton.baseColour = (0, 0, 0) hoverFill = 1 hoverColour = (255, 255, 255) clickColour = (128, 128, 128) for button in self.uiContainer.components: if isinstance(button, Button): button.hoverColour = hoverColour button.clickColour = clickColour button.hoverFill = hoverFill button.font = pygame.font.SysFont("monospace", 30) pass def start(self): pass def update(self): if xo_input.escape or self.backButton.was_pressed(): self.stateManager.switchGameState("MainScreen") self.uiContainer.update() def draw(self): self.uiContainer.draw() self.drawTextCentered("About Number Munchers", self.titleFont, 0, -275) self.drawText("Created By:", self.scoreFont, 10, -150) self.drawText("Richard Selneck: UI and Gameplay Programmer", self.scoreFont, 10, -50) self.drawText("Steven Siewart: AI Programmer", self.scoreFont, 10, 25) self.drawText("Tyler Sargent: UI and Gameplay Programmer", self.scoreFont, 10, 100) self.drawText("Westley Waligora: UI and Gameplay Programmer", self.scoreFont, 10, 175) def final(self): pass def drawTextCentered(self, text, font, offsetX, offsetY): label = font.render(text, 1, self.textColour) text_width, text_height = font.size(text) self.window.blit(label, ((self.screenInfo.current_w/2) - (text_width/2) + offsetX, (self.screenInfo.current_h/2) - (text_height/2) + offsetY)) def drawText(self, text, font, offsetX, offsetY): label = font.render(text, 1, self.textColour) text_width, text_height = font.size(text) self.window.blit(label, (offsetX, (self.screenInfo.current_h / 2) - (text_height / 2) + offsetY))
class InstructionsScreen: def __init__(self, manager, screen): self.stateManager = manager self.window = screen self.uiContainer = UIContainer(self.window) self.uiContainer.verticalStride = 0 self.screenInfo = pygame.display.Info() self.titleFont = pygame.font.SysFont("monospace", 75, bold=True) self.scoreFont = pygame.font.SysFont("monospace", 45) self.textColour = (128, 128, 128) self.initiateReset = False # Create the message box centerX = self.screenInfo.current_w / 2 centerY = self.screenInfo.current_h / 2 # Button sizes width = 170 height = 40 # Back button self.backButton = self.uiContainer.add_button("Back") self.backButton.rect = pygame.Rect( (110 - (width / 2), self.screenInfo.current_h - 50 - (height / 2), width, height)) self.backButton.baseColour = (0, 0, 0) hoverFill = 1 hoverColour = (255, 255, 255) clickColour = (128, 128, 128) for button in self.uiContainer.components: if isinstance(button, Button): button.hoverColour = hoverColour button.clickColour = clickColour button.hoverFill = hoverFill button.font = pygame.font.SysFont("monospace", 30) # Load title image if sys.platform == "win32": self.image = pygame.image.load( "WinMedia/Images/instructions.png").convert() else: self.image = pygame.image.load( "media/Images/instructions.png").convert() pass def start(self): pass def update(self): if xo_input.escape or self.backButton.was_pressed(): self.stateManager.switchGameState("MainScreen") self.uiContainer.update() def draw(self): self.uiContainer.draw() #self.drawTextCentered("Instructions", self.titleFont, 0, -300) #self.drawTextCentered("To play Number Munchers you have ", self.scoreFont, 0, -250) #self.drawTextCentered("to \"munch\" on the multiples of", self.scoreFont, 0, -200) #self.drawTextCentered("the fraction shown at the top ", self.scoreFont, 0, -150) #self.drawTextCentered("of the screen until you get them", self.scoreFont, 0, -100) #self.drawTextCentered(" all. When all of the fraction", self.scoreFont, 0, -50) #self.drawTextCentered(" multiples on the screen are ", self.scoreFont, 0, 0) #self.drawTextCentered(" munched on a new fraction and ", self.scoreFont, 0, 50) #self.drawTextCentered("board will be made. You will keep ", self.scoreFont, 0, 100) #self.drawTextCentered("playing until you get three wrong ", self.scoreFont, 0, 150) #self.drawTextCentered("fractions in a level. The higher ", self.scoreFont, 0, 200) #self.drawTextCentered("your score the better. ", self.scoreFont, 0, 250) self.window.blit(self.image, ((self.screenInfo.current_w / 2) - (self.image.get_rect().width / 2), (self.screenInfo.current_h / 2) - (self.image.get_rect().height / 2))) pass def final(self): pass def drawTextCentered(self, text, font, offsetX, offsetY): label = font.render(text, 1, self.textColour) text_width, text_height = font.size(text) self.window.blit( label, ((self.screenInfo.current_w / 2) - (text_width / 2) + offsetX, (self.screenInfo.current_h / 2) - (text_height / 2) + offsetY))
class InstructionsScreen: def __init__(self, manager, screen): self.stateManager = manager self.window = screen self.uiContainer = UIContainer(self.window) self.uiContainer.verticalStride = 0 self.screenInfo = pygame.display.Info() self.titleFont = pygame.font.SysFont("monospace", 75, bold=True) self.scoreFont = pygame.font.SysFont("monospace", 45) self.textColour = (128, 128, 128) self.initiateReset = False # Create the message box centerX = self.screenInfo.current_w / 2 centerY = self.screenInfo.current_h / 2 # Button sizes width = 170 height = 40 # Back button self.backButton = self.uiContainer.add_button("Back") self.backButton.rect = pygame.Rect( (110 - (width / 2), self.screenInfo.current_h - 50 - (height / 2), width, height)) self.backButton.baseColour = (0, 0, 0) hoverFill = 1 hoverColour = (255, 255, 255) clickColour = (128, 128, 128) for button in self.uiContainer.components: if isinstance(button, Button): button.hoverColour = hoverColour button.clickColour = clickColour button.hoverFill = hoverFill button.font = pygame.font.SysFont("monospace", 30) # Load title image if sys.platform == "win32": self.image = pygame.image.load("WinMedia/Images/instructions.png").convert() else: self.image = pygame.image.load("media/Images/instructions.png").convert() pass def start(self): pass def update(self): if xo_input.escape or self.backButton.was_pressed(): self.stateManager.switchGameState("MainScreen") self.uiContainer.update() def draw(self): self.uiContainer.draw() #self.drawTextCentered("Instructions", self.titleFont, 0, -300) #self.drawTextCentered("To play Number Munchers you have ", self.scoreFont, 0, -250) #self.drawTextCentered("to \"munch\" on the multiples of", self.scoreFont, 0, -200) #self.drawTextCentered("the fraction shown at the top ", self.scoreFont, 0, -150) #self.drawTextCentered("of the screen until you get them", self.scoreFont, 0, -100) #self.drawTextCentered(" all. When all of the fraction", self.scoreFont, 0, -50) #self.drawTextCentered(" multiples on the screen are ", self.scoreFont, 0, 0) #self.drawTextCentered(" munched on a new fraction and ", self.scoreFont, 0, 50) #self.drawTextCentered("board will be made. You will keep ", self.scoreFont, 0, 100) #self.drawTextCentered("playing until you get three wrong ", self.scoreFont, 0, 150) #self.drawTextCentered("fractions in a level. The higher ", self.scoreFont, 0, 200) #self.drawTextCentered("your score the better. ", self.scoreFont, 0, 250) self.window.blit(self.image, ((self.screenInfo.current_w/2) - (self.image.get_rect().width/2), (self.screenInfo.current_h/2) - (self.image.get_rect().height/2))) pass def final(self): pass def drawTextCentered(self, text, font, offsetX, offsetY): label = font.render(text, 1, self.textColour) text_width, text_height = font.size(text) self.window.blit(label, ((self.screenInfo.current_w/2) - (text_width/2) + offsetX, (self.screenInfo.current_h/2) - (text_height/2) + offsetY))
class GameScreen: def __init__(self, manager, highScoreManager, screen): self.stateManager = manager self.window = screen self.highScoreManager = highScoreManager self.uiContainer = UIContainer(self.window) self.uiContainer.horizontalStride = 5 self.titleFont = pygame.font.SysFont("monospace", 45, bold=True) self.scoreFont = pygame.font.SysFont("monospace", 30) self.textColour = (128, 128, 128) self.screenInfo = pygame.display.Info() self.addScore = 20 self.scoreLoss = 5 self.wrongAnswers = 0 self.rightAnswers = 0 self.totalRightAnswers = 0 self.score = 0 pass def start(self): #if not self.initializedUI: # Button sizes padding = 10 side_margin = 50 top_margin = 150 width = (self.screenInfo.current_w - (padding * 5) - (side_margin * 2)) / 5 height = (self.screenInfo.current_h - (padding * 5) - (top_margin)) / 5 self.enemy = Enemy(self.window, width, height) self.fractionAnswers = [] self.equalFractions = [] self.answer = frac_random() for x in range(1, 30): self.fractionAnswers.append(frac_random()) self.equalFractions.append(self.fractionAnswers[x - 1].get_equal_fraction()) self.right_fracs, self.wrong_fracs = create_goal(self.answer) self.totalRightAnswers = len(self.right_fracs) temp_frac = frac_random() temp_right_fracs = deepcopy(self.right_fracs) temp_wrong_fracs = deepcopy(self.wrong_fracs) for i in range(0, 5): for j in range(0, 5): if(len(temp_right_fracs) > 0 and len(temp_wrong_fracs) > 0): if(self.array_random(1,2) == 1): rand = self.array_random(0,len(temp_right_fracs)-1) temp_frac = temp_right_fracs[rand] temp_right_fracs.remove(temp_right_fracs[rand]) else: rand = self.array_random(0, len(temp_wrong_fracs) - 1) temp_frac = temp_wrong_fracs[rand] temp_wrong_fracs.remove(temp_wrong_fracs[rand]) elif(len(temp_right_fracs) > 0): rand = self.array_random(0, len(temp_right_fracs) - 1) temp_frac = temp_right_fracs[rand] temp_right_fracs.remove(temp_right_fracs[rand]) else: rand = self.array_random(0, len(temp_wrong_fracs) - 1) temp_frac = temp_wrong_fracs[rand] temp_wrong_fracs.remove(temp_wrong_fracs[rand]) button = self.uiContainer.add_button(str(temp_frac), (padding * i) + (width * i) + side_margin, (padding * j) + (height * j) + top_margin, width, height) button.font = pygame.font.SysFont("monospace", 30) button.baseColour = (0, 0, 0) button.hoverColour = (255, 255, 255) button.clickColour = (128, 128, 128) button.hoverFill = 1 def update(self): if xo_input.escape: self.resetLevel() self.score = 0 self.wrongAnswers = 0 self.stateManager.switchGameState("MainScreen") return endLoop = False for button in self.uiContainer.components: if button.text != "" and button.was_pressed(): btnFrac = frac_parse(button.text) if btnFrac == self.answer: self.score += self.addScore self.rightAnswers += 1 button.text = "" else: self.score -= self.scoreLoss self.wrongAnswers += 1 button.text = "" if self.rightAnswers == self.totalRightAnswers: self.resetLevel() self.stateManager.switchGameState("GameScreen") if(self.wrongAnswers == 3): setScore(self.score) self.wrongAnswers = 0 self.resetLevel() self.score = 0 self.stateManager.switchGameState("WinScreen") self.uiContainer.update() def draw(self): x = (-self.screenInfo.current_w / 2) + 90 y = (-self.screenInfo.current_h / 2) + 110 self.drawText("Score: " + str(self.score), self.scoreFont, x, y) self.drawText("Lives: " + str(3 - self.wrongAnswers), self.scoreFont, x + 500, y) self.drawText("Find these multiples: " + str(self.answer), self.titleFont, 0, (-self.screenInfo.current_h/2) + 70) # UI needs to be drawn LAST self.uiContainer.draw() #returns true if colliding with enemy if self.enemy.update(self.uiContainer.selectedIndex): self.score -= self.scoreLoss self.wrongAnswers += 1 def final(self): pass def drawText(self, text, font, offsetX, offsetY): label = font.render(text, 1, self.textColour) text_width, text_height = font.size(text) x = (self.screenInfo.current_w/2) - (text_width/2) + offsetX y = (self.screenInfo.current_h/2) - (text_height/2) + offsetY self.window.blit(label, (x, y)) def array_random(self, start, end): if(start == end): return start return randint(start, end) def resetLevel(self): self.uiContainer.components = [] self.rightAnswers = 0 self.totalRightAnswers = 0
class GameScreen: def __init__(self, manager, highScoreManager, screen): self.stateManager = manager self.window = screen self.highScoreManager = highScoreManager self.uiContainer = UIContainer(self.window) self.uiContainer.horizontalStride = 5 self.titleFont = pygame.font.SysFont("monospace", 45, bold=True) self.scoreFont = pygame.font.SysFont("monospace", 30) self.textColour = (128, 128, 128) self.screenInfo = pygame.display.Info() self.addScore = 20 self.scoreLoss = 5 self.wrongAnswers = 0 self.rightAnswers = 0 self.totalRightAnswers = 0 self.score = 0 pass def start(self): #if not self.initializedUI: # Button sizes padding = 10 side_margin = 50 top_margin = 150 width = (self.screenInfo.current_w - (padding * 5) - (side_margin * 2)) / 5 height = (self.screenInfo.current_h - (padding * 5) - (top_margin)) / 5 self.enemy = Enemy(self.window, width, height) self.fractionAnswers = [] self.equalFractions = [] self.answer = frac_random() for x in range(1, 30): self.fractionAnswers.append(frac_random()) self.equalFractions.append( self.fractionAnswers[x - 1].get_equal_fraction()) self.right_fracs, self.wrong_fracs = create_goal(self.answer) self.totalRightAnswers = len(self.right_fracs) temp_frac = frac_random() temp_right_fracs = deepcopy(self.right_fracs) temp_wrong_fracs = deepcopy(self.wrong_fracs) for i in range(0, 5): for j in range(0, 5): if (len(temp_right_fracs) > 0 and len(temp_wrong_fracs) > 0): if (self.array_random(1, 2) == 1): rand = self.array_random(0, len(temp_right_fracs) - 1) temp_frac = temp_right_fracs[rand] temp_right_fracs.remove(temp_right_fracs[rand]) else: rand = self.array_random(0, len(temp_wrong_fracs) - 1) temp_frac = temp_wrong_fracs[rand] temp_wrong_fracs.remove(temp_wrong_fracs[rand]) elif (len(temp_right_fracs) > 0): rand = self.array_random(0, len(temp_right_fracs) - 1) temp_frac = temp_right_fracs[rand] temp_right_fracs.remove(temp_right_fracs[rand]) else: rand = self.array_random(0, len(temp_wrong_fracs) - 1) temp_frac = temp_wrong_fracs[rand] temp_wrong_fracs.remove(temp_wrong_fracs[rand]) button = self.uiContainer.add_button( str(temp_frac), (padding * i) + (width * i) + side_margin, (padding * j) + (height * j) + top_margin, width, height) button.font = pygame.font.SysFont("monospace", 30) button.baseColour = (0, 0, 0) button.hoverColour = (255, 255, 255) button.clickColour = (128, 128, 128) button.hoverFill = 1 def update(self): if xo_input.escape: self.resetLevel() self.score = 0 self.wrongAnswers = 0 self.stateManager.switchGameState("MainScreen") return endLoop = False for button in self.uiContainer.components: if button.text != "" and button.was_pressed(): btnFrac = frac_parse(button.text) if btnFrac == self.answer: self.score += self.addScore self.rightAnswers += 1 button.text = "" else: self.score -= self.scoreLoss self.wrongAnswers += 1 button.text = "" if self.rightAnswers == self.totalRightAnswers: self.resetLevel() self.stateManager.switchGameState("GameScreen") if (self.wrongAnswers == 3): setScore(self.score) self.wrongAnswers = 0 self.resetLevel() self.score = 0 self.stateManager.switchGameState("WinScreen") self.uiContainer.update() def draw(self): x = (-self.screenInfo.current_w / 2) + 90 y = (-self.screenInfo.current_h / 2) + 110 self.drawText("Score: " + str(self.score), self.scoreFont, x, y) self.drawText("Lives: " + str(3 - self.wrongAnswers), self.scoreFont, x + 500, y) self.drawText("Find these multiples: " + str(self.answer), self.titleFont, 0, (-self.screenInfo.current_h / 2) + 70) # UI needs to be drawn LAST self.uiContainer.draw() #returns true if colliding with enemy if self.enemy.update(self.uiContainer.selectedIndex): self.score -= self.scoreLoss self.wrongAnswers += 1 def final(self): pass def drawText(self, text, font, offsetX, offsetY): label = font.render(text, 1, self.textColour) text_width, text_height = font.size(text) x = (self.screenInfo.current_w / 2) - (text_width / 2) + offsetX y = (self.screenInfo.current_h / 2) - (text_height / 2) + offsetY self.window.blit(label, (x, y)) def array_random(self, start, end): if (start == end): return start return randint(start, end) def resetLevel(self): self.uiContainer.components = [] self.rightAnswers = 0 self.totalRightAnswers = 0
class MessageWindow: def __init__(self, window, message, width, height, x, y): self.message = message self.window = window self.width = width self.height = height self.X = x self.Y = y self.topleft = [x - width/2, y - height/2] self.uiContainer = UIContainer(self.window) self.baseColour = (16, 16, 16) self.borderColour = (255, 255, 255) self.textColour = (128, 128, 128) self.textFont = pygame.font.SysFont("monospace", 20) self.padding = 20; pass def addButton(self, text, pos, size): button = self.uiContainer.add_button() button.rect = pygame.Rect([self.X + pos[0], self.Y + pos[1]], size) button.text = text return button def update(self): # Update the UI self.uiContainer.update() pass def draw(self): # Draw the background for the window pygame.draw.rect(self.window, self.baseColour, pygame.Rect(self.topleft, (self.width, self.height)), 0) # Draw the border for the window pygame.draw.rect(self.window, self.borderColour, pygame.Rect(self.topleft, (self.width, self.height)), 1) # Render the message self.drawText(self.message, self.textFont, 0, 0) # Draw the UI self.uiContainer.draw() pass def drawText(self, text, font, offsetX, offsetY): # Only works with a fixed-width font # Python text-wrap takes a character width for wrapping, not a pixel width # Which is dumb charWidth = (self.width - self.padding)/(font.size('a')[0]) wrappedText = textwrap.wrap(text, charWidth) counter = 0; for line in wrappedText: label = font.render(line, 1, self.textColour) text_width, text_height = font.size(line) self.window.blit(label, ((self.width/2) - (text_width/2) + offsetX + self.topleft[0], self.padding - (text_height/2) + offsetY + self.topleft[1] + (counter * text_height))) counter += 1
class MainScreen: """ Defines the main menu screen """ def __init__(self, manager, screen): self.stateManager = manager self.window = screen self.uiContainer = UIContainer(self.window) self.uiContainer.horizontalStride = 0 self.screenInfo = pygame.display.Info() # Button sizes and positions width = 250 height = 60 cx = self.screenInfo.current_w / 2 cy = self.screenInfo.current_h / 2 bx = cx - (width / 2) padding = 30 dy = padding / 2 + height # Start game button self.gameButton = self.uiContainer.add_button("Start Game", bx, 0, width, height) self.gameButton.rect.y = cy - height * 2 - padding / 2 + 30 # Instructions button self.instructionsButton = self.uiContainer.add_button("Instructions", bx, 0, width, height) self.instructionsButton.rect.y = self.gameButton.rect.y + dy # High Scores button self.highScoresButton = self.uiContainer.add_button("High Scores", bx, 0, width, height) self.highScoresButton.rect.y = self.instructionsButton.rect.y + dy # About button self.aboutButton = self.uiContainer.add_button("About", bx, 0, width, height) self.aboutButton.rect.y = self.highScoresButton.rect.y + dy self.quitButton = self.uiContainer.add_button("Quit", bx, 0, width, height) self.quitButton.rect.y = self.aboutButton.rect.y + dy # Set button colors baseColor = (0, 0, 0) hoverColor = (255, 255, 255) clickColor = (128, 128, 128) hoverFill = 1 for button in self.uiContainer.components: if isinstance(button, Button): button.font = pygame.font.SysFont("monospace", 30) button.baseColour = baseColor button.hoverColour = hoverColor button.clickColour = clickColor button.hoverFill = hoverFill # Load title image if sys.platform == "win32": self.titleImage = pygame.image.load("WinMedia/Images/titleXO.png").convert() else: self.titleImage = pygame.image.load("media/Images/titleXO.png").convert() def start(self): pass def update(self): if xo_input.escape: exit_game(1) if self.gameButton.was_pressed(): self.stateManager.switchGameState("GameScreen") if self.instructionsButton.was_pressed(): self.stateManager.switchGameState("InstructionsScreen") if self.highScoresButton.was_pressed(): self.stateManager.switchGameState("HighScoreScreen") if self.aboutButton.was_pressed(): self.stateManager.switchGameState("AboutScreen") if self.quitButton.was_pressed(): exit_game(1) self.uiContainer.update() def draw(self): self.window.blit(self.titleImage, ((self.screenInfo.current_w/2) - (self.titleImage.get_rect().width/2), 50)) # UI needs to be drawn LAST self.uiContainer.draw() def final(self): pass