码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA游戏简单碰撞原理

时间:2014-11-21 21:55:16      阅读:350      评论:0      收藏:0      [点我收藏+]

标签:java   swing   collision   碰撞   

一、游戏效果图:玩家飞机与敌机碰撞 或子弹与敌机碰撞,产生爆炸效果,敌机与子弹在爆炸后消失。


bubuko.com,布布扣

二、实现原理:检测原理的两个对象是否碰撞,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 int						x				= 370;
	private int						y				= 0;
	private static final int		SPEED			= 20;
	private boolean					outOfBounds		= false;
	private boolean					isCollision		= 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 int		FLY_SPEED		= 30;
	private boolean				outOfBounds		= false;
	private boolean				isCollision		= false;
	public int					x;
	public int					y;

	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 + "]";
	}

}


四、源码:点击打开链接

JAVA游戏简单碰撞原理

标签:java   swing   collision   碰撞   

原文地址:http://blog.csdn.net/laughing2me/article/details/41358669

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!