Ejemplo n.º 1
0
    def gameLoop(self, newGame):
        """
        Function houses a loop that isrepeated over and over to check
        for new events, such as key presses or events in game.
        """
        # Reset score and lives if user starting new game
        if newGame:
            self.lives = 3
            self.score = 0
            self.ballLaunched = False

        # Load our sprites
        self.loadSprites()

        # Set key repeat on
        pygame.key.set_repeat(5, 10)

        # Draw background to screen initially
        self.screen.blit(self.level.background, (0, 0))
        self.level.brickSprites.draw(self.screen)
        self.drawLives()
        self.drawScore()
        self.drawName()

        while 1:
            seconds = self.elapsed / 1000.0
            
            # First rotation hasn't occurred yet
            if self.level.rotated:
                rotatedScreen = pygame.transform.rotate(self.screen, -self.level.rotation)
                self.screen.blit(rotatedScreen, (0, 0))


            # Redraw where paddle and balls used to be
            self.screen.blit(self.level.background,
                             (self.paddle.rect.x, self.paddle.rect.y),
                             self.paddle.rect)
            for ball in self.ballSprites:
                self.screen.blit(self.level.background,
                                 (ball.rect.x, ball.rect.y),
                                 ball.rect)

############# EVENT CHECKER ###################################
            for event in pygame.event.get():
                # Window 'X' clicked
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()

                # Keys pressed
                if event.type == KEYDOWN:
                    # Quit game
                    if event.key == K_ESCAPE or event.key == K_q:
                        pygame.event.post(pygame.event.Event(QUIT))

                    # Pause game
                    if event.key == K_p:
                        pygame.key.set_repeat()
                        self.paused = True

                        self.screen.blit(self.pauseText, self.pauseTextPos)
                        self.paddleSprite.draw(self.screen)
                        self.ballSprites.draw(self.screen)
                        pygame.display.flip()

                        pygame.event.clear()

                        # If paused, flow control does not leave this
                        # loop until unpaused.
                        while self.paused:
                            for event in pygame.event.get():
                                if event.type == KEYDOWN:
                                    # Unpause game
                                    if event.key == K_p:
                                        pygame.key.set_repeat(5, 10)
                                        self.paused = False

                                        # Blit out text and old ball location
                                        self.screen.blit(self.level.background,
                                                         (self.pauseTextPos.x,
                                                          self.pauseTextPos.y),
                                                         self.pauseTextPos)
                                        for ball in self.ballSprites:
                                            self.screen.blit(self.level.background,
                                                             (ball.rect.x,
                                                              ball.rect.y),
                                                             ball.rect)
                                        pygame.display.flip()
                                    # Need to update elapsed time, or
                                    # else balls will end up somewhere
                                    # wacky.
                                    self.elapsed = self.clock.tick(60)

                    # Move paddle
                    if (event.key == K_LEFT or
                            event.key == K_RIGHT):
                        for ball in self.ballSprites:
                            self.paddle.move(event.key,
                                             self.width,
                                             self.height,
                                             ball,
                                             seconds)

                    # Start game by launching the ball
                    if event.key == K_SPACE:
                        if not self.ballLaunched:
                            for ball in self.ballSprites:
                                ball.launch()
                            self.ballLaunched = True

                    # Go back to menu
                    if event.key == K_m:
                        self.ballSprites.empty()
                        self.paddleSprite.empty()
                        self.level.brickSprites.empty()
                        return "menu"

############# BALL MOVER ###############################
            if self.ballLaunched:
                for ball in self.ballSprites:
                    ballLost = ball.move(self.width,
                                         self.height,
                                         seconds,
                                         self.paddle)

                    # Check if ball went past paddle
                    if ballLost:
                        ball.kill()
                        if not self.ballSprites:
                            self.mainBall = Ball((self.paddle.x, self.paddle.y),
                                                 self.paddle.height)
                            self.ballSprites.add(self.mainBall)
                            for i in range(self.numBalls):
                                ball = Ball((self.paddle.x, self.paddle.y),
                                                     self.paddle.height)
                                self.ballSprites.add(ball)
                            self.ballLaunched = False

                            # Lose a life, fill in circle where one used to be
                            self.lives -= 1
                            self.drawScore()
                            self.drawName()
                            pygame.draw.circle(self.screen,
                                       (0, 0, 0),
                                       (self.lifeX + ((self.lives - 1)*10), self.lifeY),
                                       self.lifeRadius)

                            # End game if lives are gone
                            if self.lives == 0:
                                return "lost"

############# COLLISION DETECTION ###########################
            # Collision detection between ball and paddle
            hitPaddle = pygame.sprite.spritecollide(self.paddle,
                                                    self.ballSprites,
                                                    False)
            if hitPaddle:
                # For levels that have constant rotation
                if self.level.alwaysRotating:
                    if self.level.rotation == 360:
                        self.level.rotation = 90
                    else:
                        self.level.rotation += 90
                    
                for ball in hitPaddle:
                    ball.paddleCollide(self.paddle)

            # Collision detection between ball and brick
            if not self.level.brickSprites:
                try:
                    BreakoutMain.levels[self.currentLevel + 1]
                    self.currentLevel += 1
                    self.ballSprites.empty()
                    self.paddleSprite.empty()
                    self.level.brickSprites.empty()
                    self.ballLaunched = False
                    return "next"
                except IndexError:
                    return "won"
            else:
                for ball in self.ballSprites:
                    hitBricks = pygame.sprite.spritecollide(ball,
                                                            self.level.brickSprites,
                                                            True)
                    ball.brickCollide(hitBricks)
                    for brick in hitBricks:
                        self.screen.blit(self.level.background,
                                         (brick.rect.x, brick.rect.y),
                                         brick.rect)
                        self.score += 1
                        # Redraw score
                        self.drawScore()

############# SPRITE REFRESH #################################
            # Redraw lives left
            self.drawLives()

            # Redraw sprites
            self.paddleSprite.draw(self.screen)
            self.ballSprites.draw(self.screen)
            if self.level.rotation != 0:
                rotatedScreen = pygame.transform.rotate(self.screen, self.level.rotation)
                self.screen.blit(rotatedScreen, (0, 0))
                self.level.rotated = True
            pygame.display.flip()

            # Keep track of time elapsed
            self.elapsed = self.clock.tick(60)