java碰撞方法_JAVA游戲簡單碰撞原理

一、游戲效果圖:玩家飛機與敵機碰撞 或子彈與敵機碰撞,產生爆炸效果,敵機與子彈在爆炸后消失。

09beeddb9f6c3e40dc21b9150dfe771d.jpe

二、實現原理:檢測原理的兩個對象是否碰撞,JAVA提供了Rectangle這個類,里面的intersects(Rectangle r)方法可以完成這種需求。目前的做法是,把屏幕內的子彈與飛機進行遍歷,在遍歷的過程用這個方法去檢測是否有碰撞出現。

三、敵機與子彈碰撞代碼:

package com.jack;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.Rectangle;

import java.awt.image.ImageObserver;

import javax.swing.JPanel;

import com.jack.imp.IPlan;

/**

*

* @author laughing

* @date 2014年11月20日 下午11:03:11

*/

public class Enemy3 implements IPlan {

private static final Image[]Enemy1_IMAGES= ImageManager.getInstance()

.getImagesByType("e3");

private static final Image[]BOMB_IMAGES= ImageManager.getInstance()

.getImagesByType("bomb");

private intx= 370;

private inty= 0;

private static final intSPEED= 20;

private booleanoutOfBounds= false;

private booleanisCollision= false;

@Override

public void drawMyself(Graphics g, JPanel p) {

if (isCollision) {

for (int i = 0; i < BOMB_IMAGES.length; i++) {

g.drawImage(BOMB_IMAGES[i], x, y, (ImageObserver) p);

}

} else {

g.drawImage(Enemy1_IMAGES[0], x, y, (ImageObserver) p);

}

}

@Override

public void move() {

if (y >= GamePanel.HEIGHT) {

setOutOfBounds(true);

} else {

y += SPEED;

}

}

@Override

public void shut() {

// TODO Auto-generated method stub

}

@Override

public boolean collison(Rectangle r) {

Rectangle enemy1 = new Rectangle(x,

y,

Enemy1_IMAGES[0].getWidth(null),

Enemy1_IMAGES[0].getHeight(null));

if (enemy1.intersects(r)) {

setCollision(true);

return true;

}

return false;

}

/**

* @return the outOfBounds

*/

public boolean isOutOfBounds() {

return outOfBounds;

}

/**

* @param outOfBounds

* the outOfBounds to set

*/

public void setOutOfBounds(boolean outOfBounds) {

this.outOfBounds = outOfBounds;

}

/*

* (non-Javadoc)

*

* @see com.jack.imp.IPlan#isOutofBounds()

*/

@Override

public boolean isOutofBounds() {

// TODO Auto-generated method stub

return outOfBounds;

}

/**

* @return the isCollision

*/

public boolean isCollision() {

return isCollision;

}

/**

* @param isCollision

* the isCollision to set

*/

public void setCollision(boolean isCollision) {

this.isCollision = isCollision;

}

}

package com.jack;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.Rectangle;

import java.awt.image.ImageObserver;

import javax.swing.JPanel;

import com.jack.imp.IDraw;

/**

* 游戲子彈

*

* @author laughing

* @date 2014年11月20日 上午10:27:12

*/

public class Bullet implements IDraw {

public static final Image[]BULLET_IMAGES= ImageManager.getInstance()

.getImagesByType("bullet");

public static final Image[]BOMB_IMAGES= ImageManager.getInstance()

.getImagesByType("bomb");

public static final intFLY_SPEED= 30;

private booleanoutOfBounds= false;

private booleanisCollision= false;

public intx;

public inty;

public Bullet(int x, int y) {

this.x = x + 38;

this.y = y;

}

public boolean outOfBounds() {

if (this.y == 0) {

this.outOfBounds = true;

return true;

}

return false;

}

public Rectangle castRectangle() {

return new Rectangle(x,

y,

BULLET_IMAGES[0].getWidth(null),

BULLET_IMAGES[0].getHeight(null));

}

/*

* (non-Javadoc)

*

* @see com.jack.imp.IProcess#drawMyself(java.awt.Graphics,

* javax.swing.JPanel)

*/

@Override

public void drawMyself(Graphics g, JPanel p) {

if (isCollision()) {

g.drawImage(BOMB_IMAGES[0],

x,

y - BOMB_IMAGES[0].getHeight(null),

(ImageObserver) p);

} else {

g.drawImage(BULLET_IMAGES[0],

x,

y - BULLET_IMAGES[0].getHeight(null),

(ImageObserver) p);

}

}

/*

* (non-Javadoc)

*

* @see com.jack.imp.IProcess#move()

*/

@Override

public void move() {

if (!outOfBounds())

this.y -= FLY_SPEED;

}

/**

* @return the y

*/

public int getY() {

return y;

}

/**

* @return the x

*/

public int getX() {

return x;

}

/**

* @return the outOfBounds

*/

public boolean isOutOfBounds() {

return outOfBounds;

}

/**

* @param outOfBounds

* the outOfBounds to set

*/

public void setOutOfBounds(boolean outOfBounds) {

this.outOfBounds = outOfBounds;

}

/**

* @return the isCollision

*/

public boolean isCollision() {

return isCollision;

}

/**

* @param isCollision

* the isCollision to set

*/

public void setCollision(boolean isCollision) {

this.isCollision = isCollision;

}

/*

* (non-Javadoc)

*

* @see java.lang.Object#toString()

*/

@Override

public String toString() {

return "Bullet [outOfBounds=" + outOfBounds + ", isCollision=" + isCollision + ", x=" + x + ", y=" + y + "]";

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值