2014-11-01 77 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(碰撞发生在某一帧'期间')。