Exemple #1
0
class GameOverScene(Scene):
    def __init__(self, score=0):
        super(GameOverScene, self).__init__()
        self.id = 3
        self.sceneName = "gameover"
        self.score = score

    def initialize(self):
        print(f"Initializing the '{self.sceneName}' level...")

        self.gameOverTxt = Text(400, 100, "courier new", 100, "GAME OVER!",
                                colour.LORANGE, Justify.CENTER)
        self.scoreTxt = Text(400, 200, "courier new", 50,
                             f"SCORE: {self.score}", colour.RED,
                             Justify.CENTER)

        self.againBtn = Button(225, 370, 350, 50, "PLAY AGAIN?")
        self.quitBtn = Button(330, 450, 140, 50, "QUIT")

        # assign button colour
        self.againBtn.set_button_colour(colour.LIGHTBLUE, None)
        self.quitBtn.set_button_colour(colour.LIGHTBLUE, None)

        # assign action functions to handle button click events
        self.againBtn.actionFunc = self.__onAgainBtnClicked__
        self.quitBtn.actionFunc = self.__onQuitBtnClicked__

        self.background = load_image("background1.gif")

        self.playAgain = False
        self.quitGame = False

    def handle_events(self, event):
        self.againBtn.handle_events()
        self.quitBtn.handle_events()

        if self.playAgain:
            ApplicationManager().load_scene(GameScene())

        if event.type == pygame.MOUSEBUTTONDOWN and self.quitGame:
            ApplicationManager().running = False

    def render(self, screen):
        screen.blit(self.background, (0, 0))
        self.gameOverTxt.render(screen)
        self.scoreTxt.render(screen)
        self.againBtn.render(screen)
        self.quitBtn.render(screen)

    def update(self):
        pygame.display.update()

    def __onAgainBtnClicked__(self):
        self.playAgain = True

    def __onQuitBtnClicked__(self):
        self.quitGame = True
Exemple #2
0
class TitleScene(Scene):
    def __init__(self):
        super(TitleScene, self).__init__()
        self.id = 1
        self.sceneName = "title"

    def initialize(self):
        print(f"Initializing the '{self.sceneName}' level...")

        self.startBtn = Button(310, 250, 160, 50, "START")
        self.infoBtn = Button(200, 350, 370, 50, "INSTRUCTIONS")
        self.quitBtn = Button(325, 450, 135, 50, "QUIT")
        self.backBtn = Button(20, 530, 130, 50, "BACK")

        # assign button colour
        self.startBtn.set_button_colour(colour.LIGHTBLUE, None)
        self.infoBtn.set_button_colour(colour.LIGHTBLUE, None)
        self.quitBtn.set_button_colour(colour.LIGHTBLUE, None)
        self.backBtn.set_button_colour(colour.LIGHTBLUE, None)

        # assign action functions to handle button click events
        self.startBtn.actionFunc = self.__onStartBtnClicked
        self.infoBtn.actionFunc = self.__onInfoBtnClicked
        self.quitBtn.actionFunc = self.__onQuitBtnClicked
        self.backBtn.actionFunc = self.__onBackBtnClicked

        self.background = load_image("intro.png")
        self.instructions = load_image("instructions.png")

        self.startGame = False
        self.quitGame = False
        self.showInstructions = False

    def handle_events(self, event):
        self.startBtn.handle_events()
        self.infoBtn.handle_events()
        self.quitBtn.handle_events()
        self.backBtn.handle_events()

        if self.startGame is True:
            ApplicationManager().load_scene(GameScene())

        if event.type == pygame.MOUSEBUTTONDOWN and self.quitGame:
            # QUIT button clicked, exit the program
            ApplicationManager().running = False

    def render(self, screen):

        if self.showInstructions is False:
            # Draw the title screen
            screen.blit(self.background, (0, 0))
            self.startBtn.render(screen)
            self.infoBtn.render(screen)
            self.quitBtn.render(screen)
        else:
            # Draw the info screen
            screen.blit(self.instructions, (0, 0))
            self.backBtn.render(screen)

    def update(self):
        pygame.display.update()

    def __onStartBtnClicked(self):
        self.startGame = True

    def __onInfoBtnClicked(self):
        self.showInstructions = True

    def __onBackBtnClicked(self):
        self.showInstructions = False

    def __onQuitBtnClicked(self):
        self.quitGame = True