Beispiel #1
0
    def pressedButton(self, x, y):

        #pressed start button
        if x > 829 and x < 952 and y > 461 and y < 500:
            #only start next level when player just started, or completed level
            if player.spriteBloons == None or len(player.spriteBloons) == 0:
                player.level += 1
                if player.level < 11:
                    player.spriteBloons = Levels.runLevel(player.level)
                else:
                    player.level = 10
                    player.gameWon = True

        #pressed monkey sidebar
        if x > 825 and x < 950 and y > 140 and y < 380:
            #check what monkey you clicked on
            player.selectedTower = Tower.clickedMonkeys(x, y)

        #pressed music button
        if x > Coord.music[0][0] and y < Coord.music[0][1]:
            if y > Coord.music[1][0] and y < Coord.music[1][1]:
                Music.play = not Music.play
                if Music.play == True:
                    pygame.mixer.music.unpause()
                else:
                    pygame.mixer.music.pause()

        #pressed home button
        if x > 840 and x < 872 and y > 538 and y < 568:
            self.splashScreenActive = True

        #place a monkey onto the canvas
        if player.selectedTower != None:
            monkeyName = player.selectedTower
            if player.money < towerTypes[monkeyName]["price"]:
                #you don't have enough money to buy it
                player.selectedTower = None
            else:
                monkey = Tower(player.selectedTower)
                #check if monkey is placed in a "legal" area
                if monkey.legalPlacements(x, y):
                    monkey.rect.center = [x, y]
                    monkey.originalImageRect.center = [x, y]
                    monkey.sellButtonRect.center = [x, y + 50]
                    monkey.upgradeButtonRect.center = [x, y - 50]
                    monkey.drawUpgradePriceRect.center = [x, y - 70]
                    player.towers.add(monkey)
                    player.money -= towerTypes[monkeyName]["price"]
                    player.selectedTower = None  #reset selected tower
Beispiel #2
0
    def run(self):

        clock = pygame.time.Clock()
        screen = pygame.display.set_mode((self.width, self.height))
        # set the title of the window
        pygame.display.set_caption(self.title)

        self.splashScreenActive = True

        # stores all the keys currently being held down
        self._keys = dict()

        # call game-specific initialization
        self.__init__()
        playing = True

        while playing:
            x, y = pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]

            if self.splashScreenActive == True:
                self.tutorial = False
                self.initializeSplashScreen()
                for event in pygame.event.get():
                    if event.type == pygame.MOUSEBUTTONDOWN:
                        if self.splashScreenMousePressed(event) != None:
                            if self.splashScreenMousePressed(
                                    event) == "tutorial":
                                self.tutorial = True
                                self.tutorialPage = 1
                                self.splashScreenActive = False
                                player.__init__()  #reset player values
                                player.spriteBloons == Levels.runLevel(0)
                            else:
                                map = self.splashScreenMousePressed(event)
                                self.map = pygame.image.load("images/%s.png" %
                                                             map)
                                self.map.convert()
                                player.game = map
                                self.splashScreenActive = False
                                player.__init__()  #reset player values
                                player.spriteBloons == Levels.runLevel(0)
                    elif event.type == QUIT:
                        pygame.quit()
                        sys.exit()

            elif self.tutorial == True:
                self.map = pygame.image.load("images/slide%d.png" %
                                             self.tutorialPage).convert()
                player.screen.blit(self.map, (0, 0))
                self.drawPlayerStats()
                self.drawPrices()
                for event in pygame.event.get():
                    if event.type == pygame.MOUSEBUTTONDOWN:
                        if self.tutorialPage == 7:
                            self.tutorial = False
                            self.splashScreenActive = True
                        else:
                            self.tutorialPage += 1
                    elif event.type == QUIT:
                        pygame.quit()
                        sys.exit()

            elif player.gameOver == True:
                self.lost = pygame.image.load("images/lost.png").convert()
                player.screen.blit(self.lost, (0, 0))
                for event in pygame.event.get():
                    if event.type == pygame.MOUSEBUTTONDOWN:
                        if x > 223 and x < 723:
                            if y > 343 and y < 410:
                                #go back to splash screen
                                player.gameOver = False
                                self.splashScreenActive = True
                    elif event.type == QUIT:
                        pygame.quit()
                        sys.exit()

            elif player.gameWon == True:
                self.won = pygame.image.load("images/won.png").convert()
                player.screen.blit(self.won, (0, 0))
                for event in pygame.event.get():
                    if event.type == pygame.MOUSEBUTTONDOWN:
                        if x > 223 and x < 723:
                            if y > 343 and y < 410:
                                #go back to splash screen
                                player.gameWon = False
                                self.splashScreenActive = True
                    elif event.type == QUIT:
                        pygame.quit()
                        sys.exit()

            else:
                self.initializeMap()
                self.timerFired()
                self.moveBloons(player.spriteBloons)

                if player.selectedTower != None:
                    monkeyName = player.selectedTower
                    if player.money >= towerTypes[monkeyName]["price"]:
                        monkeyName = player.selectedTower
                        monkey = Tower(monkeyName)
                        legal = monkey.legalPlacements(x, y)
                        monkey.drawMonkeyAndRange(legal, x, y)

                for monkey in player.towers:
                    if monkey.selected == True:
                        monkey.drawRangeAndSell(player.screen)

                for event in pygame.event.get():
                    if event.type == pygame.MOUSEBUTTONDOWN:
                        self.mousePressed(event)
                    elif event.type == KEYDOWN:
                        if event.key == K_ESCAPE:
                            #press "esc" key to deselect a tower
                            player.selectedTower = None
                    elif event.type == QUIT:
                        pygame.quit()
                        sys.exit()

                if player.spriteBloons == None or len(
                        player.spriteBloons) == 0:
                    pass

                self.redrawAll()
                self.drawPrices()
                player.checkGameOver(player.spriteBloons)
                player.checkGameWon(player.spriteBloons)

            pygame.display.flip()
            clock.tick(self.fps)

        pygame.quit()