Applet "pong" 游戏:如果球拍向球移动,球会直接穿过球拍

Applet "pong" game: ball goes right through paddle if paddle is moving towards the ball

通常在此类游戏中只有 Y 轴移动可用,但我决定采用 X 轴桨移动也允许的方式。如果当球击中球拍时我不在 X 轴上移动球拍(在这种情况下它会直接通过),游戏运行得非常好。如果我在球击中球拍之前停止 X 轴运动,球每次都会弹开。

起初我虽然这与像素有关,但我也尝试以多种方式改变桨碰撞但没有积极结果。无论我尝试多少次,球都不会从球拍上反弹。

代码非常简单,我每次只移动 1px:

public void drawBall(Graphics g) { //ball speed, movement and collision
    b.set(b.getX()+VX,b.getY()+VY); //velocity X and velocity Y both at 1px
    g.setColor(Color.black);
    b.draw(g);

    if(b.getY()<0 || b.getY()>sizeY-b.getRadius()){ //top & bottom collision
        VY = -VY;
    }

    if(b.getX()<0 || b.getX()>sizeX-b.getRadius()){ //left & right detection
        b.center();
        VX = -VX;
        x = (int) (Math.random()*2+0);
        if(x == 0)
            VY = -VY;
    }

    if(b.getX() == p1.getX()+p1.width && b.getY()>p1.getY() && b.getY()<p1.getY()+p1.height){ // p1 paddle detection
        VX = -VX;
    }

    if(b.getX()+b.getRadius()==p2.getX() && b.getY()>p2.getY() && b.getY()<p2.getY()+p2.height){ // p2 paddle detection
        VX = -VX;
    }
}

为了四处移动球拍,我使用了简单的标志,在按键时将运动设置为特定方向 "on",在按键释放时将运动设置为 "off"。我 运行 这在无限循环中所以它被不断地执行。

这是 "run()" 方法的样子:

public void run() {
    while(true){
        repaint();
        try {
            Thread.sleep(5);
            playerMovement(); //moves players paddle on X and/or Y axis
            playerLimits(); //limits movment of players to specific area
        } catch (InterruptedException e){
        }
    }
}

我再次尝试以不特定于像素的方式创建桨碰撞 "if(ball < than p1 paddle) then change ball direction",但没有成功。我想我在这里遗漏了一些重要的东西,但我似乎无法找出它是什么。

提前感谢您的帮助,非常感谢。

不要将您的游戏引擎代码与您的图形代码混在一起——两者必须分开。任何解决方案的关键是确保游戏引擎识别 "collision" 并正确且明确地处理它。与其切换速度方向,不如考虑根据碰撞发生的位置设置这些速度的绝对值。

例如,你有这个代码:

if(b.getY() < 0 || b.getY() > sizeY-b.getRadius()){ //top & bottom collision
    VY = -VY;
}

但是如果精灵被捕获超出边缘,这就有可能导致精灵被捕获。也许更好的做法是:

if (b.getY() < 0) {
    // note that variables should start with lower-case letters
    vy = Math.abs(vy); // make sure that it is going in a positive direction
}

if (b.getY() > sizeY - b.getRadius()) {
    vy = -Math.abs(vy);
}

x 方向也是如此

另请注意,如果您将图形与游戏引擎分开,您的引擎将变得可单独测试并更易于调试。