有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Pong:在窗口的顶部和底部边界反转球

我是一名编程初学者,希望能创作一款乒乓球游戏。我离得很近,两个桨都在移动,当它们相交时,球从它们身上反弹。但是,一旦球穿过屏幕的顶部和底部,我就很难逆转它的轨迹。我以为我已经解决了这个问题,但它似乎不起作用

任何帮助都将不胜感激。代码如下

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.newdawn.slick.Animation;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.geom.Rectangle;

public class PongAttempt extends BasicGame{
Image player1Img;
Image player2Img;
Image ball;

float ballx = 500;
float bally = 500;

float x = 0;
float y = 0;

float bx = 100;
float by = 40;

float velocityx = -.10f;
float velocityy = 0f;

float player1Vel = 0;
float player2Vel = 0;

Rectangle player1Dim;
Rectangle player2Dim;
Rectangle ballDim;

public PongAttempt(String gamename){super(gamename);}

@Override
public void init(GameContainer gc) throws SlickException {

    player1Img = new Image("res/troll.png");
    player2Img = new Image("res/troll.png");
    ball = new Image("res/ball.png");

    player1Dim = new Rectangle(x, y, player1Img.getWidth(), player1Img.getHeight());
    player2Dim = new Rectangle(bx, by, player2Img.getWidth(), player2Img.getHeight());
    ballDim = new Rectangle(ballx, bally, ball.getWidth(), ball.getHeight());

    bx = gc.getWidth() - player2Img.getWidth();
    by = 0;

    ballx = gc.getWidth() / 2;
    bally = gc.getHeight() / 2;

}


@Override
public void update(GameContainer gc, int i) throws SlickException {

}

@Override
public void render(GameContainer gc, Graphics g) throws SlickException
{

    g.setBackground(Color.black);

    //Player 1 move and generate boundaries

    if(gc.getInput().isKeyDown(Input.KEY_W) && y > 0 ) {
        y = y - 0.2f;
        player1Vel = -0.2f;
    }
    if(gc.getInput().isKeyDown(Input.KEY_S) && y < (gc.getHeight() - player1Img.getHeight())) {
        y = y + 0.2f;
        player1Vel = 0.2f;
    }



    //Player 2 Move and generate boundaries

    if(gc.getInput().isKeyDown(Input.KEY_O) && by > 0 ) {
        player2Vel = -0.2f;
        by = by + player2Vel;
    }
    if(gc.getInput().isKeyDown(Input.KEY_L) && by < (gc.getHeight() - player2Img.getHeight())) {
        player2Vel = 0.2f;
        by = by + player2Vel;
    }


    if(gc.getInput().isKeyDown(Input.KEY_ESCAPE)){
        gc.exit();
    }

    player1Dim.setX(x);
    player1Dim.setY(y);

    player2Dim.setX(bx);
    player2Dim.setY(by);

    ballDim.setX(ballx);
    ballDim.setY(bally);


    if(ballDim.intersects(player1Dim)){
        velocityx = velocityx * -1;
        velocityy = player1Vel;
    }

    if(ballDim.intersects(player2Dim)){
        velocityx = velocityx * -1;
        velocityy = player2Vel;
    }

    //This is where I tried to get the ball to bounce off the top and bottom

if(ballx == 0 - ball.getHeight()){
        velocityx = velocityx * -1;
    }

    if(ballx == gc.getHeight() - ball.getHeight()){
        velocityx = velocityx * -1;
    }

    ballx = ballx + velocityx;
    bally = bally + velocityy;

    player1Img.draw(x, y);
    player2Img.draw(bx, by);
    ball.draw(ballx, bally);

}

public static void main(String[] args)
{
    try
    {
        AppGameContainer appgc;
        appgc = new AppGameContainer(new PongAttempt("Simple Slick Game"));
        appgc.setDisplayMode(appgc.getScreenWidth(), appgc.getScreenHeight(), true);
        appgc.start();
    }
    catch (SlickException ex)
    {
        Logger.getLogger(PongAttempt.class.getName()).log(Level.SEVERE, null, ex);
    }
}
}

共 (1) 个答案

  1. # 1 楼答案

    您正在检查球的高度是否正好等于零(或窗口的高度)。调用渲染和球准确命中0的几率很低。你应该检查球是否已经过了0,然后改变它的速度

    它实际上比这稍微复杂一些,因为如果球超过0,你改变了速度,然后发生了一些事情使球变慢,当球仍在帧外时,可能会再次进行渲染,导致其速度反转并再次远离屏幕。这将导致它在窗口外持续振动,每帧的速度都相反

    你还可以做很多额外的错误检查,确保球的速度始终正确,并做更复杂的几何图形,使球在单帧中穿过墙壁时也能从墙上反弹