首先,我要说我已经很累了,已经连续22个小时了。无论如何,我在PONG游戏中遇到了这个时髦的物理问题。你知道古老的经典。我一直在尝试长时间的代码,尝试不同的变体,注释掉某些部分等,但我找不到该死的错误!

有人可以帮我吗?

这是球的物理问题。左移时,它可以从屏幕的顶部和底部弹起。

但是当它与从左侧来的桨碰撞时,我无法将其发送回右侧。它只是迫使弹跳而下,迫使它退回并越过划桨?!

但是,当我编程将球击中顶侧或底侧之一后立即弹跳时,我可以将球正确发送,因此我认为将球向右移动的实际代码没有任何问题。

但是,向右移动与游戏所需的物理效果相反,因此它无用,因为当它碰到左侧的球拍时,它应该弹起并向右移动,不应该将其自身“强迫”自己越过球拍和向左走。

如果您看到它,那实际上很有趣:)

有人可以理解这一点,并给我一个解释吗?

[码]

    # move the ball around

    # if the ball disappears off the either side of the screen, send it back heading  left
    if ballRect.left > WINDOWWIDTH or ballRect.right < 0:
        direction = getRandomDirection()
        ballRect.center = (WINDOWWIDTH - ballRect.width, random.randint(100, 200))

    if direction == 'downleft':
        ballRect.left -= BALLSPEEDX
        ballRect.top += BALLSPEEDY
    if direction == 'upleft':
        ballRect.left -= BALLSPEEDX
        ballRect.top -= BALLSPEEDY
    if direction == 'downright':
        ballRect.left += BALLSPEEDX
        ballRect.top += BALLSPEEDY
    if direction == 'upright':
        ballRect.left += BALLSPEEDX
        ballRect.top -= BALLSPEEDY

    if ballRect.top < 0:
        if direction == 'upleft':
            direction = 'downleft'
        if direction == 'upright':
            direction = 'downright'
    if ballRect.bottom > WINDOWHEIGHT:
        if direction == 'downleft':
            direction = 'upleft'
        if direction == 'downright':
            direction = 'upright'

    if paddleRect.colliderect(ballRect):
        if direction == 'upleft':
            direction = 'upright'
            ballRect.left += BALLSPEEDX
            ballRect.top -= BALLSPEEDY
        if direction == 'upright':
            direction = 'upleft'
            ballRect.left += BALLSPEEDX
            ballRect.top -= BALLSPEEDY
        if direction == 'downleft':
            direction = 'upleft'
            ballRect.left += BALLSPEEDX
            ballRect.top += BALLSPEEDY
        if direction == 'downright':
            direction = 'upright'
            ballRect.left -= BALLSPEEDX
            ballRect.top += BALLSPEEDY


[/码]

最佳答案

如果directions == 'upleft',这两个块都将执行

if paddleRect.colliderect(ballRect):
    if direction == 'upleft':
        direction = 'upright'
        ballRect.left += BALLSPEEDX * 4
        ballRect.top -= BALLSPEEDY * 4
    if direction == 'upright':
        direction = 'upleft'
        ballRect.left += BALLSPEEDX * 4
        ballRect.top -= BALLSPEEDY * 4


大概应该更像这样

if paddleRect.colliderect(ballRect):
    if direction == 'upleft':
        direction = 'upright'
        ballRect.left += BALLSPEEDX * 4
        ballRect.top -= BALLSPEEDY * 4
    elif direction == 'upright':
        direction = 'upleft'
        ballRect.left += BALLSPEEDX * 4
        ballRect.top -= BALLSPEEDY * 4
    elif ...

10-06 13:19
查看更多