球没有像我想的那样移动

时间:2012-04-02 16:26:42

标签: java swing graphics

我想创建一个Pong游戏。我想以某种方式移动球,直到它碰到墙壁。如果它碰到墙壁,它会走另一条路。问题在于,当我开始比赛时,球以正确的方式进行,但当它接触到墙壁时,球方向相反但只有一个像素,因此球反转1个像素,然后方向改变角度,它接触墙壁再次。我的移动球代码是在initBall方法中。请帮帮我:(

这是我的playPanel类:

private int posX = SCREEN_WIDTH / 2;
private int posY;

public Point posMouse = new Point();
private Point posBall = new Point();

private int playPanelWidth;
private int playPanelHeight;

private int padPanelWidth;
private int padPanelHeight;

private int panPanelWidth;
private int panPanelHeight;

private JLabel player1Score = new JLabel("0");
private JLabel ComputerScore = new JLabel("0");

private JPanel panPlayer1;
public JPanel panComputer;

public JPanel padPlayer1;
public JPanel padComputer;

private JButton but_Escape = new JButton("Press escape to continue !");

/*
 * Constructor
 */
// ==============================================
public PlayPanel() {
    super(new BorderLayout());
    setBackground(PANPLAY_COLOR);

    panPlayer1 = new JPanel();
    panComputer = new JPanel();

    padPlayer1 = new JPanel();
    padComputer = new JPanel();

    padPlayer1.setBackground(Color.DARK_GRAY);
    padComputer.setBackground(Color.DARK_GRAY);

    padPlayer1.setPreferredSize(PADPANEL_SIZE);
    padComputer.setPreferredSize(PADPANEL_SIZE);

    panPlayer1.setBackground(PANPLAY_COLOR);
    panComputer.setBackground(PANPLAY_COLOR);

    panPlayer1.add(padPlayer1);
    panComputer.add(padComputer);

    add(panPlayer1, BorderLayout.WEST);
    add(panComputer, BorderLayout.EAST);

    addMouseMotionListener(this);

    panPlayer1.addComponentListener(new ComponentAdapter() {

        @Override
        public void componentResized(ComponentEvent arg0) {
            setPanPanelWidth(arg0.getComponent().getSize().width);
            setPanPanelHeight(arg0.getComponent().getSize().height);
        }

    });

    addComponentListener(new ComponentAdapter() {

        @Override
        public void componentResized(ComponentEvent arg0) {

            setPlayPanelWidth(arg0.getComponent().getSize().width);
            setPlayPanelHeight(arg0.getComponent().getSize().height);
        }

    });
}

/*
 * Setters and Getters
 */
// ==============================================

public int getPosX() {
    return posX;
}

public void setPosX(int posX) {
    this.posX = posX;
}

public int getPosY() {
    return posY;
}

public void setPosY(int posY) {
    this.posY = posY;
}

public JPanel getPanPlayer1() {
    return panPlayer1;
}

public void setPanPlayer1(JPanel panPlayer1) {
    this.panPlayer1 = panPlayer1;
}

public JPanel getPanComputer() {
    return panComputer;
}

public void setPanComputer(JPanel panComputer) {
    this.panComputer = panComputer;
}

public int getPlayPanelHeight() {
    return playPanelHeight;
}

public void setPlayPanelHeight(int playPanelHeight) {
    this.playPanelHeight = playPanelHeight;
}

public int getPlayPanelWidth() {
    return playPanelWidth;
}

public void setPlayPanelWidth(int playPanelWidth) {
    this.playPanelWidth = playPanelWidth;
}

public int getPadPanelWidth() {
    return padPanelWidth;
}

public void setPadPanelWidth(int padPanelWidth) {
    this.padPanelWidth = padPanelWidth;
}

public int getPadPanelHeight() {
    return padPanelHeight;
}

public void setPadPanelHeight(int padPanelHeight) {
    this.padPanelHeight = padPanelHeight;
}

public int getPanPanelWidth() {
    return panPanelWidth;
}

public void setPanPanelWidth(int panPanelWidth) {
    this.panPanelWidth = panPanelWidth;
}

public int getPanPanelHeight() {
    return panPanelHeight;
}

public void setPanPanelHeight(int panPanelHeight) {
    this.panPanelHeight = panPanelHeight;
}

/*
 * Add the ball
 */
// ==============================================
public void paintComponent(Graphics g) {

    super.paintComponent(g);

    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setColor(Color.BLACK);

    initBall(g2);

    // trait épais
    g2.setColor(Color.DARK_GRAY);

    g2.setStroke(new BasicStroke(10));
    g2.drawLine((getPlayPanelWidth() / 2) - 5, getPlayPanelHeight(),
            (getPlayPanelWidth() / 2) - 5, 0);
}

/*
 * Init ball
 */
// ==============================================
private void initBall(Graphics2D graphics2d) {

    int x = getPosX(), y = getPosY();
    boolean backX = false;
    boolean backY = false;

    Graphics2D g2 = graphics2d;

    g2.fillOval(posX, posY, BALL_WIDTH, BALL_HEIGHT);

    //posBall.setLocation(posX + BALL_WIDTH, posY + (BALL_HEIGHT / 2));

    if (x < 1)
        backX = false;
    if (x > getWidth() - 50)
        backX = true;

    if (y < 1)
        backY = false;
    if (y > getHeight() - 50)
        backY = true;


    if (!backX)
        setPosX(++x);

    else {
        setPosX(--x);
    }

    if (!backY)
        setPosY(++y);
    else
        setPosY(--y);

    repaint();

    try {
        Thread.sleep(2);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

@Override
public void mouseDragged(MouseEvent arg0) {
}

@Override
public void mouseMoved(MouseEvent arg0) {

    posMouse.setLocation(arg0.getX(), arg0.getY()
            - (getPadPanelHeight() / 2));

    padPlayer1.setLocation(getPanPanelWidth() - 15, (int) posMouse.getY());
    padComputer.setLocation(5, (int) posMouse.getY());
}

}

1 个答案:

答案 0 :(得分:3)

所以你有:

private void initBall(Graphics2D graphics2d) {

int x = getPosX(), y = getPosY();
boolean backX = false;
boolean backY = false;

一开始,所以无论球的方向如何,每次都将摊位布局设为假。然后,在

中设置后退选项时,您没有“Else”
    if (x < 1)
    backX = false;
if (x > getWidth() - 50)
    backX = true;

if (y < 1)
    backY = false;
if (y > getHeight() - 50)
    backY = true;

正在发生的事情是球正朝着正确的方向移动,直到撞到墙壁(我猜顶壁)。然后这称为:

if (y > getHeight() - 50)
    backY = true;

那么对于那次迭代,球因为

而回归
if (!backY)
    setPosY(++y);
else
    setPosY(--y);

但是你马上就把它设置回假。我建议你有

private boolean backX = false; //same for backY

在你的方法之外。