2014-11-01 79 views
0

我一直在試圖使這個彈跳球遊戲在java中,這裏是我的代碼。 當球撞到牆上時,我無法理解速度變化。 當我試圖改變速度的球只是不動,但停留在一個位置,振動。我不知道問題出在哪裏,但球沒有從牆上反彈..任何人都可以幫助我?java彈跳球碰撞不起作用

import java.awt.Color; 
    import java.awt.Dimension; 
    import java.awt.Font; 
    import java.awt.Graphics2D; 
    import java.awt.Graphics; 
    import java.awt.Rectangle; 
    import java.awt.RenderingHints; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.awt.event.KeyEvent; 
    import java.awt.event.KeyListener; 
    import java.util.ArrayList; 
import java.util.TimerTask; 

import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

import ballGame.SimpleSprite; 


public class mainGame extends JPanel 
{ 


    public static void main(String args[]) 
    { 
     JFrame frame = new JFrame("hello"); 

     mainGame mainPanel = new mainGame(600, 400); 

     SimpleSprite st = new SimpleSprite(mainPanel); 

     frame.add(mainPanel); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 

    } 


    private ArrayList<SimpleSprite> sprites = new ArrayList<SimpleSprite>(); 

    final static int timer_msec=30; 

mainGame(int width, int height) 
{ 
    setPreferredSize(new Dimension(width, height)); 
    sprites.add(new SimpleSprite(Color.blue, 0, 35, 2, 1)); 

    startTimer(); 

} 

public void startTimer() 
{ 
    java.util.Timer timer = new java.util.Timer(); 
    TimerTask timerTask = new TimerTask() 
    { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      updateSimulation(); 
     } 

    }; 
    timer.scheduleAtFixedRate(timerTask, 0, timer_msec); 

} 

public void updateSimulation() 
{ 
    for(SimpleSprite s: sprites) 
    { 
     s.update(); 
    } 
    repaint(); 
} 

public void paintComponent(Graphics g) 
{ 
    Graphics2D g2d = (Graphics2D) g; 
    paintBackground(g2d); 
    for (SimpleSprite s : sprites) { 
     s.draw(g2d); 
} 
} 

public void paintBackground(Graphics2D g2d) 
{ 
    g2d.setColor(Color.LIGHT_GRAY); 
    g2d.fillRect(0, 0, getWidth(), getHeight()); 
} 

} 

class SimpleSprite extends JPanel 
{ 
     // basic x,y movement at constant velocity 
     float x, y, dx, dy; // position and velocity (pixels/TIMER_MSEC) 
     Color color; 
     private static float width=600;; 
     SimpleSprite(mainGame g) 
     { 
      width=g.getWidth(); 
     } 

     public SimpleSprite(Color color, float x, float y, float dx, float dy) { 
      // initial position and velocity 
      this.color = color; 
      this.x = x; 
      this.y = y; 
      this.dx = dx; 
      this.dy = dy; 
     } 
     public void update() { // update position and velocity every n milliSec 

      x += dx; // velocity in x direction 
      y += dy; // velocity in y direction 

      if(x<0) 
      { 
      dx=+dx; 
      } 
      if(x>width) 
      { 
       dx=-dx; 
      } 





      if(y<0) 
      { 
       dy=+dy; 
      } 
      if(y>width) 
      { 
       dy=-dy; 
      } 


     } 
     public void draw(Graphics2D g2d) { 
      g2d.setColor(color); 
      g2d.fillOval((int) x, (int) y, 100, 100); // draw this at latest position. 
     } 
    } 

回答

0

dx=+dxdy=+dy什麼也不做。

如果你想扭轉方向做

dx=-dx;

這仍然是正確的,無論DX是積極的還是之前的變化負。

0

請注意,如果某個物體出於某種原因不在屏障之外,那麼它會在看起來處於相同位置時晃動(由於計算機運行的相當高的幀率),因爲它會不斷變化方向( s)每一幀。你可能想要的是檢查射彈是否要穿過下一幀的障礙,例如,如果p(x-b) != p(x+dx-b),其中b是x邊界位置,則反轉x速度,其中p是特殊函數,也定義爲相當於x>0?1:0。 (p與符號函數非常相似,但省略0除外,因爲否則對象可能會完全粘在邊界上)。

此外,爲了提高準確性,您應該在此幀中存儲要覆蓋邊界的絕對距離,並將其加到下一幀的速度的兩倍。例如。 tx = -2(x-b)邊界在b(碰撞發生在某一幀'期間')。