コード例 #1
0
def main():
    #initialize all imported pygame modules
    pygame.init()    
    #create the main screen
    screen = Screen('Break IT', 'img/star.png', 
                    'img/outerspace.png',600, 600,Colors.WHITE)

    #Create a score object
    score = Score(450, 25,Colors.GOLD , 2500, 
                  "sounds/BonusChime.ogg", screen.surface)	

    #Create a lives object
    lives = Lives(50, 25, 3,"img/heart3.png", screen.surface)	

    #Create a level object
    level = Level(230, 25,Colors.GOLD, 40, screen.surface)

    #Create a paddle object
    paddle = Paddle(80, 20, Colors.AQUAMARINE,
                    "sounds/beep1.ogg", screen.surface)	
    #Create Ball here
    ball = Ball(40, 0, 0, 600, 600,"img/star.png",
                "sounds/beep4.ogg", screen.surface)	



    #Initialize clock
    clock = pygame.time.Clock() 	
    #indicates if the game is paused
    paused = False	
    #set the variable that controls the main game loop
    playing = True
    #start the game loop
    while playing:    
        #maintain a frame rate of 100 fps
        clock.tick(100)	
        #get all the events since last time 
        #the events were checked
        for event in pygame.event.get():
            #if the user click exit stop playing	    
            if event.type == QUIT:
                playing = False			
            #if this is a keydown (pressing down of a key) 
            if event.type == KEYDOWN:
                if event.key == K_q:
                    playing = False	
                #switch the pause feature on or off	
                if event.key == K_ESCAPE:
                    paused = not paused	
                #set the paddle to move to the left	
                if event.key == K_LEFT:
                    paddle.setMoveLeft()					
                #set the paddle to move to the right		
                if event.key == K_RIGHT:
                    paddle.setMoveRight()
            #if this is a keyup (the player release 
            #the key so it is no longer down) 
            if event.type == KEYUP:
                #since the player released the key 
                #the paddle should be stationary
                if event.key == K_LEFT or event.key == K_RIGHT:
                    paddle.stop()  
        #move and draw only
        #if pause feature off	
        if not paused:

            #update and move things
            ball.move()
            paddle.move()   
            #Check for paddle hit
            if ball.checkCollision(paddle.rect):
                paddle.playSound()
                score.addScore(10)
            #if the ball leaves the screen
            if ball.lost():
                ball.reset()			
                lives.subtractLives(1)	
                if lives.getLives() == 0:				
                    #we are out of lives check for restart
                    if screen.restartPrompt("img/GameOver4.png"):
                        lives.reset()
                        score.reset()
                    else:
                        playing = False	
               


            #draw things
            #draw screen first so it does not
            #overwrite other objects
            screen.draw()
            score.draw()
            lives.draw()
            level.draw()
            ball.draw()
            paddle.draw()


            #update the changes to 
            #show on the main screen
            pygame.display.update()



    #Uninitialized all pygame modules that
    #have previously been initialized
    pygame.quit()	    
コード例 #2
0
def main():
    #initialize all imported pygame modules
    pygame.init()
    #create the main screen
    screen = Screen('Break it!', 'img/star.png', 'img/outerspace.png', 600,
                    600, Colors.WHITE)
    score = Score(450, 25, Colors.FIREBRICK, 1, "sounds/BonusChime.ogg",
                  screen.Surface)
    #create a lives object'
    lives = Lives(50, 25, 3, "img/heart3.png", screen.Surface)
    level = Level(230, 25, Colors.AQUAMARINE, 40, screen.Surface)
    #create a ball object
    paddle = Paddle(160, 30, Colors.DEEPSKYBLUE, "sounds/beep4.ogg",
                    screen.Surface)
    ball = Ball(40, 0, 0, 600, 600, "img/earth-icon.png",
                "sounds/BonusSymbol.ogg", screen.Surface)
    #list of colors for brick
    colors = [Colors.RED, Colors.BLUE, Colors.GREEN, Colors.YELLOW]
    values = [100, 75, 50, 25]
    #create bricks
    bricks = Brick.createBricks(17, 100, 4, 10, 2, colors, values, 55, 20,
                                screen.Surface)

    #rate to increase speed
    speedLevel = 15
    #initialize clock
    clock = pygame.time.Clock()

    #indicates if the game is paused
    paused = False
    #set the variable that controls the main game loop
    playing = True
    while playing:
        #maintain a frame rate of 100 fps
        clock.tick(100)
        #the events since last time
        for event in pygame.event.get():
            #if the user clicks exit stop playing
            if event.type == QUIT:
                playing = False
            if event.type == KEYDOWN:
                if event.key == K_q:
                    playing = False
                if event.key == K_p:
                    paused = not paused
                if event.key == K_LEFT:
                    #move left
                    paddle.setMoveLeft()
                    #move right
                if event.key == K_RIGHT:
                    paddle.setMoveRight()

            if event.type == KEYUP:
                if event.key == K_LEFT or event.key == K_RIGHT:
                    paddle.stop()

        #move and draw only if the pause feature is off
        if not paused:
            #update and move things
            ball.move()
            paddle.move()

            #check for paddle hit
            if ball.checkCollision(paddle.rect):
                paddle.playSound()
                score.addScore(100)

            #if ball leaves screen
            if ball.lost():
                ball.reset()
                lives.subtractLives(1)
                if lives.getLives() == 0:
                    if screen.restartPrompt("img/GameOver4.png"):
                        lives.reset()
                        score.resetScore()

            #loop through all the bricks
            for brick in bricks:
                #is there a collision?
                if (brick.checkBallCollision(ball)):
                    if score.addScore(brick.value):
                        lives.addLives(0)
                if brick.gone:
                    bricks.remove(brick)
                    if level.addPartialLevel(1):
                        bricks = Brick.createBricks(17, 100, 4, 10, 2, colors,
                                                    values, 55, 20,
                                                    screen.Surface)
                        ball.reset()

            #draw things
            #draw screen first so it does nor overwrite other objects
            #screen.draw

            screen.draw()
            score.draw()
            lives.draw()
            level.draw()
            paddle.draw()
            ball.draw()
            #loops through all the bricks
            for brick in bricks:
                brick.draw()

            #update the changes to show on the main screen
            pygame.display.update()

    #Uninialize all pygame modules that have previously beeen inialized
    pygame.quit()
コード例 #3
0
class PongGame(object):
    def __init__(self):
        pygame.init()
        self.clock = pygame.time.Clock()

        self.gameDisplay = pygame.display.set_mode(
            (constant.WIDTH, constant.HEIGHT))

        self.text, self.text2, self.font, self.font2, self.textRect, self.text2Rect = None, None, None, None, None, None
        self.ball, self.paddle1, self.paddle2, self.player1, self.player2 = None, None, None, None, None

        self.initializeFont()
        self.initializeObject()

        self.paddlespeed = 5
        self.terminate = False

        while not self.terminate:
            self.checkEvent()
            self.collisionCheck()
            self.checkScore()
            self.updateObject()
            self.updateText()
            self.renderText()
            self.renderObject()

            pygame.display.update()
            self.clock.tick(120)

        pygame.quit()

    def initializeFont(self):
        self.font = pygame.font.Font("freesansbold.ttf", 32)
        self.font2 = pygame.font.Font("freesansbold.ttf", 15)
        self.text = self.font.render("PONG", True, (255, 255, 255))
        self.text2 = self.font2.render("press r to reset game", True,
                                       (255, 255, 255))
        self.textRect = self.text.get_rect()
        self.text2Rect = self.text2.get_rect()
        self.text2Rect.center = (constant.WIDTH // 2, 80)
        self.textRect.center = (constant.WIDTH // 2, 40)

    def initializeObject(self):
        self.ball = Ball(constant.WIDTH // 2, constant.HEIGHT // 2, 10, 5,
                         random.uniform(0, math.pi), self.gameDisplay)
        self.paddle1 = Paddle(30, 20, 150, self.gameDisplay)
        self.paddle2 = Paddle(constant.WIDTH - 50, 20, 150, self.gameDisplay)
        self.player1 = Player(self.paddle1)
        self.player2 = Player(self.paddle2)

    def resetGame(self):
        self.ball.reset()
        self.player1.reset()
        self.player2.reset()

    def checkEvent(self):
        for event in pygame.event.get():  # check events
            if event.type == pygame.QUIT:
                self.terminate = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_r:
                    self.resetGame()
                if event.key == pygame.K_UP:
                    if self.paddle2.y > 0:
                        self.paddle2.setChange(-self.paddlespeed)

                if event.key == pygame.K_DOWN:
                    if self.paddle2.y < constant.HEIGHT - self.paddle2.h:
                        self.paddle2.setChange(self.paddlespeed)

                if event.key == pygame.K_w:
                    if self.paddle1.y > 0:
                        self.paddle1.setChange(-self.paddlespeed)
                if event.key == pygame.K_s:
                    if self.paddle1.y < constant.HEIGHT - self.paddle1.h:
                        self.paddle1.setChange(self.paddlespeed)
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    self.paddle2.stop()
                elif event.key == pygame.K_w or event.key == pygame.K_s:
                    self.paddle1.stop()
            print(event)

    def collisionCheck(self):
        if (self.ball.x >= self.paddle1.x + self.ball.r and
                self.ball.x <= self.paddle1.x + self.paddle1.w + self.ball.r
            ) and (self.ball.y >= self.paddle1.y
                   and self.ball.y <= self.paddle1.y + self.paddle1.h):
            self.ball.a = random.uniform(math.pi * 7 / 4, math.pi * 9 / 4)
            self.ball.speed = random.randint(4, 8)
            #print(ball.a)
            self.ball.updateSpeeds()
        if (self.ball.x >= self.paddle2.x - self.ball.r and
                self.ball.x <= self.paddle2.x + self.paddle2.w - self.ball.r
            ) and (self.ball.y >= self.paddle2.y
                   and self.ball.y <= self.paddle2.y + self.paddle2.h):
            self.ball.a = random.uniform(math.pi * 3 / 4, math.pi * 5 / 4)
            self.ball.speed = random.randint(4, 8)
            #print(ball.a)
            self.ball.updateSpeeds()

    def renderText(self):
        self.gameDisplay.fill((0, 0, 0))
        self.gameDisplay.blit(self.text, self.textRect)
        self.gameDisplay.blit(self.text2, self.text2Rect)
        self.gameDisplay.blit(self.score1text, self.score1Rect)
        self.gameDisplay.blit(self.score2text, self.score2Rect)

    def updateText(self):
        self.score1text = self.font.render(str(self.player1.score), True,
                                           (255, 255, 255))
        self.score2text = self.font.render(str(self.player2.score), True,
                                           (255, 255, 255))
        self.score1Rect = self.score1text.get_rect()
        self.score1Rect.center = (40, 40)
        self.score2Rect = self.score2text.get_rect()
        self.score2Rect.center = (constant.WIDTH - 40, 40)

    def updateObject(self):
        self.paddle1.update()
        self.paddle2.update()
        self.ball.update()

    def checkScore(self):
        if self.ball.x <= self.ball.r:
            self.player2.winRound()
            self.ball.reset()
            self.paddle1.reset()
        elif self.ball.x >= constant.WIDTH - self.ball.r:
            self.player1.winRound()
            self.ball.reset()
            self.paddle2.reset()

    def renderObject(self):
        self.ball.render()
        self.paddle1.render()
        self.paddle2.render()