在我的乒乓球重制中,我的球拍没有移动 python

My paddles aren't moving in python in my pong remake

我改变了代码的方式,现在我的桨停止移动了。有人知道为什么吗?暂时忽略球,我还没有完成编码。在我更改我的代码之前,我没有定义我的形状我只是画了它们。现在我定义了它们,什么都不会移动。我只包含相关代码。

    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)
    def drawshapes():   
        pygame.draw.rect(DISPLAY, WHITE, paddle1)
        pygame.draw.rect(DISPLAY, WHITE, paddle2)
        pygame.draw.rect(DISPLAY, WHITE, ball)
    DISPLAY = pygame.display.set_mode((640,480),0,32)
    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
        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

        ballX += ballXvel
        ballY += ballYvel
        rectY1 += Y1change
        rectY2 += Y2change
        DISPLAY.fill(BLACK)
        drawshapes()
        pygame.display.update()
        fpsClock.tick(FPS)

代码从不更新 Paddle rect。当然 rectY1rectY2 已更新,但这从未应用于 paddle1paddle2

drawshapes() 之前,将更改应用于球拍矩形:

    rectY1 += Y1change
    rectY2 += Y2change
    paddle1.y = rectY1   # <-- HERE - Update the paddle's rect
    paddle2.y = rectY2   #            which is used to draw it
    DISPLAY.fill(BLACK)

但我认为如果直接在 paddle1.xpaddle1.y

上跟踪更改,您的代码会更简单