2012-04-02 86 views
-1

我想創建一個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()); 
} 

}

+6

http://sscce.org/ – talnicolas 2012-04-02 16:28:14

+5

否否否:(永遠不要在GUI中使用'Thread.sleep(...)',總是使用'Timer' [javax.swing.Timer](http:/ /docs.oracle.com/javase/tutorial/uiswing/misc/timer.html)。爲了更好的幫助檢查這個線程的所有工作[示例](http://stackoverflow.com/questions/9849950/something-似乎是錯誤的與這個球Thingy :-)佈局jbutton顯示,意外,行爲在r)。最好是檢查這個[WONDERFUL示例](http://stackoverflow.com/a/9852739/1057230) – 2012-04-02 16:34:13

回答

3

所以,你必須:

private void initBall(Graphics2D graphics2d) { 

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

,這樣不管哪個方向球布爾布爾每次都被設置爲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); 

但隨後將其重新設置爲false的時候了。我建議你有

private boolean backX = false; //same for backY 

你的方法之外。

+0

+1,實際上對於漂亮的catch,我的評論是錯誤的,我的建議是:-),所以刪除它。 – 2012-04-02 16:56:15

+0

感謝上帝,那是我的問題。非常感謝 – MTHeadss 2012-04-02 16:58:05