当球与球拍碰撞时会停止,并在乒乓游戏中移动球拍后弹开

Ball will stop when it collides with paddle and bounce off once paddle is moved in pong game

我第一次在 python 中编写乒乓球代码,我试图让球在碰到球拍时反弹。现在,球在碰到球拍时停止,然后只有在球拍移动时才会弹开。我希望球一碰到球拍就反弹。关于如何解决这个问题的任何想法?任何帮助表示赞赏。谢谢!

import pygame, random, sys
from pygame.locals import *
#set constants
WINDOWWIDTH = 600
WINDOWHEIGHT = 600
TEXTCOLOR = (255, 255, 255)
BACKGROUNDCOLOR = (0, 0, 255)
FPS = 40
BLACK = (0,0,0)
RED = (255, 0, 0)
WHITE = (255, 255, 255)
#set variables
rectY1 = 240
rectY2 = 240
Y1change = 0
Y2change = 0
ballX = 320
ballY = 240
ballXvel = 0
ballYvel = 0
paddle1 = pygame.Rect(18,rectY1,10,120)
paddle2 = pygame.Rect(620,rectY2,10,120)
ball = pygame.Rect(ballX,ballY,30,30)
#create shapes
def drawshapes():   
    pygame.draw.rect(DISPLAY, WHITE, paddle1)
    pygame.draw.rect(DISPLAY, WHITE, paddle2)
    pygame.draw.rect(DISPLAY, WHITE, ball)

#set up display    
pygame.init
DISPLAY = pygame.display.set_mode((640,480),0,32)
pygame.display.set_caption('Pong')
fpsClock = pygame.time.Clock()
#main loop

while True:
    for event in pygame.event.get():
        if event.type==QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN :
            if event.key == K_SPACE :
                ballXvel += 10
                ballYvel += 10
    if ball.colliderect(paddle1) :
        ballX = paddle1.x
        ballXvel = ballXvel * -1

    if ball.colliderect(paddle2) :
        ballXvel = ballXvel * -1




        if ballX > 620 :
            ballXvel = ballXvel * -1
    ##    if ballX < 20 :
    ##        ballXvel = ballXvel * -1 
        if ballY > 460 :
            ballYvel = ballYvel * -1
        if ballY < 20 :
            ballYvel = ballYvel * -1

        keys = pygame.key.get_pressed()

        Y1change = 0
        Y2change = 0
    #make paddles move
        if keys[K_UP]:
            Y2change -= 10

        if keys[K_DOWN]:
            Y2change += 10

        if keys[ord('w')]:
            Y1change -= 10

        if keys[ord('s')]:
            Y1change += 10
    #stop paddles at edges 
        if rectY1 < 5 :
            rectY1 = 5
        if rectY1 > 355 :
            rectY1 = 355
        if rectY2 < 5 :
            rectY2 = 5
        if rectY2 > 355 :
            rectY2 = 355
        ball.y = ballY
        ball.x = ballX
        ballX += ballXvel
        ballY += ballYvel
        paddle1.y = rectY1
        paddle2.y = rectY2
        rectY1 += Y1change
        rectY2 += Y2change
        DISPLAY.fill(BLACK)
        drawshapes()
        pygame.display.update()
        fpsClock.tick(FPS)

既然你有桨和球的坐标,你所要做的就是检查球的某些部分是否位于"inside"桨的某些部分。

假设桨是从 (0,0) 到 (1,1) 的正方形。如果我们有一个半径为 1 个单位的球,如果球的中心在桨的任何边缘的 1 个单位以内,我们就知道它与桨相交。

编辑:This 回答更加详细。

在进行碰撞测试之前移动球,而不是之后。由于球的一步大于 1,因此球不会准确击中球拍。如果球击中了左侧球拍,则您必须将球的左侧放在球拍的右侧。如果球击中了右边的拍子,你必须把球的右边放在拍子的左边。
以类似的方式,你必须将球的位置限制在 window:
(注意ballXballY是球的左上角)

while True:
    # [...]

    ballX += ballXvel
    ballY += ballYvel

    if ball.colliderect(paddle1) :
        ballXvel *= -1
        ballX = paddle1.right

    if ball.colliderect(paddle2) :
        ballXvel *= -1
        ballX = paddle2.left - ball.width

    size = DISPLAY.get_size()
    if not 0 <= ballX < size[0] - ball.width:
        ballXvel *= -1
        ballX = max(0, min(size[0] - ball.width, ballX))
    if not 0 <= ballY < size[1] - ball.height:
        ballYvel *= -1
        ballY = max(0, min(size[1] - ball.height, ballY))

    # [...]

    ball.y = ballY
    ball.x = ballX
    # ballX += ballXvel <-- DELETE
    # ballY += ballYvel