pygame pong:球员得分后产生新球

时间:2017-06-14 18:41:43

标签: python pygame

我希望学习如何重新开始/产生一个新球以继续游戏。当其中一名球员得分时 - 比赛结束时没有新球被制作/产生?在每个点之后我需要哪些代码才能继续游戏?

我的乒乓游戏代码:

    import pygame, sys
    import random
    import math
    pygame.init()

    #Screen Width/Height
    screen = pygame.display.set_mode((800,600))

    #Background Image
    background_image = pygame.image.load("tennis.jpg").convert()

    #Score Font
    pygame.font.init()
    font = pygame.font.Font("Bombing.TTF", 50)

    #Graphics Font
    graphicsScore = "Game Score!"
    graphicsFont = pygame.font.Font("Bombing.TTF", 50)

    #Paddle Player1
    paddle_player1 = pygame.Rect(5,50,5,60)
    player1Score = 0

    #Paddle Player2
    paddle_player2 = pygame.Rect(790,50,5,60)
    player2Score = 0

    #Ball
    ball = pygame.Rect(300,200,20,20)
    ballAngle = math.radians(0)
    ballSpeed = 10
    ballDirection = -1

    #frame
    clock = pygame.time.Clock()

    #Process Player Input
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

                timeChange = pygame.time.get_ticks() - totalTime
                totalTime = pygame.time.get_ticks()

            player1_up = pygame.key.get_pressed()[pygame.K_w] 
            player1_down = pygame.key.get_pressed()[pygame.K_s]

            player2_up = pygame.key.get_pressed()[pygame.K_UP]
            player2_down = pygame.key.get_pressed()[pygame.K_DOWN]

    #Updating Game State Logic
            if player1_up:
                paddle_player1.y += -50 
            if player1_down:
                paddle_player1.y += 50 
            if player2_up:
                paddle_player2.y += -50 
            if player2_down:
                paddle_player2.y += 50 

           if paddle_player1.y < 0:
                paddle_player1.y = 0
           if paddle_player2.y < 0:
                paddle_player2.y = 0

           if paddle_player1.y > screen.get_height() - paddle_player1.height:
                paddle_player1.y = screen.get_height() - paddle_player1.height
            if paddle_player2.y > screen.get_height() - paddle_player2.height:
                paddle_player2.y = screen.get_height() - paddle_player2.height


    #Update Ball
        ball.x += ballDirection * ballSpeed * math.cos(ballAngle)
        ball.y += ballDirection * ballSpeed * math.sin(ballAngle)
        if ball.x > 800 or ball.x < 0:
            ballSpeed *= 1
            if ball.x > 800:
                player1Score = 1
                applause = pygame.mixer.music.load('applause.wav')
                pygame.mixer.music.play()
            if ball.x < 0:
                player2Score = 1
                applause = pygame.mixer.music.load('applause.wav')
                pygame.mixer.music.play()

        if player1Score >= 10 or player2Score >= 10:
                   player1Score = 0
                   player2Score = 0

    #Paddle Collision
        if ball.colliderect(paddle_player1):
                if ball.x < 5:
                    ballDirection *= -1
                    ballAngle = random.randrange(20, 300)

                    pong = pygame.mixer.music.load('serve.wav')
                    pygame.mixer.music.play()
        if ball.colliderect(paddle_player2):
                if ball.x > 40:
                    ballDirection *= -1
                    ballAngle = random.randrange(20, 300)

                    pong = pygame.mixer.music.load('serve.wav')
                    pygame.mixer.music.play()
    #Wall Collision
        if ball.y <= 0:
            ballAngle *= -1
        if ball.y >= 600:
            ballAngle *= -1


   #Rendering
        screen.blit(background_image, [0,0])
        screen.blit(font.render(str(player1Score), 1, (178,34,34)),(200, 25))
        screen.blit(font.render(str(player2Score), 1, (178,24,24)),(600, 25))
        screen.blit(font.render(str(graphicsScore), 1, (245,255,250)), (330, 25))
        pygame.draw.rect(screen,(0,0,0), paddle_player1)
        pygame.draw.rect(screen,(0,0,0), paddle_player2)
        pygame.draw.rect(screen,(0,255,0), ball)

        clock.tick(50)
        pygame.display.flip()

1 个答案:

答案 0 :(得分:0)

一种快速将功能添加到您正在寻找的游戏中的方法就是将球的参数重置为您在主游戏循环之前设置的参数。

例如,将此功能添加到您的游戏中,然后在玩家得分时调用它:

def resetBall():
    ball.rect.x = 300
    ball.rect.y = 200
    ballAngle = math.radians(0)
    ballSpeed = 10
    ballDirection = -1

这应该只是将除分数以外的所有内容都设置回游戏开始状态。