Java中的碰撞效果

时间:2016-12-13 04:33:03

标签: java

是否可以使用drawImage()方法在Java中为图像添加碰撞效果?如果是的话,我怎么能这样做? 例如:

  import java.awt.Graphics2D;
  import java.awt.Rectangle; 
  import java.awt.event.KeyEvent;

public class Racquet {
  private static final int Y = 330;
  private static final int WIDTH = 60;
  private static final int HEIGHT = 10;
  int x = 0;
  int xa = 0;
  private Game game;

  public Racquet(Game game) {
    this.game = game;
  }

  public void move() {
    if (x + xa > 0 && x + xa < game.getWidth() - WIDTH)
        x = x + xa;
  }

  public void paint(Graphics2D g) {
    g.fillRect(x, Y, WIDTH, HEIGHT);
  }

  public void keyReleased(KeyEvent e) {
    xa = 0;
  }

  public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_LEFT)
        xa = -1;
    if (e.getKeyCode() == KeyEvent.VK_RIGHT)
        xa = 1;
  }

  public Rectangle getBounds() {
    return new Rectangle(x, Y, WIDTH, HEIGHT);
  }

  public int getTopY() {
    return Y;
  }
}

球类:

    import java.awt.Graphics2D;
    import java.awt.Rectangle;

  public class Ball {
  private static final int DIAMETER = 30;
  int x = 0;
  int y = 0;
  int xa = 1;
  int ya = 1;
  private Game game;

  public Ball(Game game) {
    this.game= game;
  }

  void move() {
    if (x + xa < 0)
        xa = 1;
    if (x + xa > game.getWidth() - DIAMETER)
        xa = -1;
    if (y + ya < 0)
        ya = 1;
    if (y + ya > game.getHeight() - DIAMETER)
        game.gameOver();
    if (collision()){
        ya = -1;
        y = game.racquet.getTopY() - DIAMETER;
    }
    x = x + xa;
    y = y + ya;
  }

  private boolean collision() {
    return game.racquet.getBounds().intersects(getBounds());
  }

  public void paint(Graphics2D g) {
    g.fillOval(x, y, DIAMETER, DIAMETER);
  }

  public Rectangle getBounds() {
    return new Rectangle(x, y, DIAMETER, DIAMETER);
  }
  }

上面的代码检测到球拍和球之间的碰撞,现在,如果我的球拍/球拍是一张图像,我怎么能够检测到它与球的碰撞?此外,是否有更容易的碰撞检测方法?(只是问)

2 个答案:

答案 0 :(得分:0)

  

我怎么能够发现它与球的碰撞?

我发现您已经有getBounds()个方法返回Rectangle个对象。您可以使用它来检查碰撞。

你可以这样做:

if(racquet.getBounds().intersects(ball.getBounds()))
    //collision happens

如果球必须在球拍内,那么:

if(racquet.getBounds().contains(ball.getBounds()))
    //collision happens

但是,如果你的球拍对象的高度包括球拍手柄的长度,你可以为球拍实现这样的getHitBox()方法:

public Rectangle getHitBox(){
    return new Rectangle(x, Y, WIDTH, HEIGHT-handleHeight);
}
  

是否可以使用drawImage()方法在Java中为图像添加碰撞效果?如果是这样,我怎么能这样做?

不确定drawImage()是如何实施的,所以我无法对此发表评论。但是,绝对可以在自定义绘画中添加一些“碰撞效果”。每当检测到碰撞时,在特定位置(将发生碰撞的位置)播放动画。

您可以构建一个类似的方法:

playAnimation(int locX, int locY, Animation anima, int duration)

在这种情况下,Animation类可以从精灵表中播放精灵。

  

此外,是否有更容易的碰撞检测方法?(只是问)

你的游戏中可能只有1个球和最多4个球拍。在这种情况下,你只需要检查球是否击中任何一个球拍:

for(int x=0; x<allRacquets.size(); x++) 
    if(allRacquets.get(x).collidesWith(ball))
        collidedRacquet = allRacquets.get(x);

CollidesWith()方法可以通过上面提到的碰撞检测方法实现:

//implement within Racquet class
public boolean collidesWith(Ball ball){
    return (this.getBounds().intersects(ball.getBounds()));
}

答案 1 :(得分:0)

class MyImage extends BufferedImage {
  int x, y, xa, ya;

  public Rectangle getBounds() {
    return new Rectangle(x, y, getWidth(), getHeight());
  }

// all other methods in raquet remain the same except you remove paint()

private Game game;

  public MyImage(Game game) {
    this.game = game;
  }

  public void move() {
    if (x + xa > 0 && x + xa < game.getWidth() - WIDTH)
        x = x + xa;
  }

  public void keyReleased(KeyEvent e) {
    xa = 0;
  }

  public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_LEFT)
        xa = -1;
    if (e.getKeyCode() == KeyEvent.VK_RIGHT)
        xa = 1;
  }


  public int getY() {
    return y;
  }
}

要开始拍摄图像,请按照

进行阅读
MyImage myImage=null;
try {
  myImage=ImageIO.read(new File(...));
}
catch (Exception ex) { ex.printStackTrace(); }