java--飞机大战完整代码

1.Award类


public class Award {
	int ax,ay;
	Image aImg;
	int aSpeed;
	int kind;
	PlaneJPanel mainview;
	public Award(int ax, int ay, Image aImg, int aSpeed, int kind,PlaneJPanel mainview) {
		super();
		this.ax = ax;
		this.ay = ay;
		this.aImg = aImg;
		this.aSpeed = aSpeed;
		this.kind = kind;
		this.mainview = mainview;
	}
	public void drawAward(Graphics g) {
		g.drawImage(aImg, ax, ay, null);
	}
	public void moveAward() {
		ay+=aSpeed;
		hint0();
		hint1();
		hint2();
	}
	public void hint0() {
		if (kind==0) {//跟血量碰撞
			if (ax+aImg.getWidth(null)>mainview.px&&ax<mainview.px+mainview.p[0].getWidth()&&ay<mainview.py+mainview.p[0].getHeight()&&ay+aImg.getHeight(null)>mainview.py) {
				mainview.awards.remove(this);
				mainview.blood++;
			}
		}
	}
	public void hint1() {//子弹增加
		if (kind ==1) {
			if (ax+aImg.getWidth(null)>mainview.px&&ax<mainview.px+mainview.p[0].getWidth()&&ay<mainview.py+mainview.p[0].getHeight()&&ay+aImg.getHeight(null)>mainview.py) {
				mainview.awards.remove(this);
				mainview.threeBullet = true;
			}
		}
	}
	public void hint2() {//速度
		if (kind == 2) {
			if (ax+aImg.getWidth(null)>mainview.px&&ax<mainview.px+mainview.p[0].getWidth()&&ay<mainview.py+mainview.p[0].getHeight()&&ay+aImg.getHeight(null)>mainview.py) {
				mainview.awards.remove(this);
				mainview.bulletSpeed = 10;
			}
		}
	}
}

2.Bullet类


public class Bullet {
	int bx,by;
	Image bImg;
	int bSpeed;
	int bDirection;
	boolean exist=true;
	public Bullet(int bx,int by,Image bImg,int bSpeed,int bDirection) {
		super();
		this.bx = bx;
		this.by = by;
		this.bImg = bImg;
		this.bSpeed = bSpeed;
		this.bDirection = bDirection;
	}
	//画子弹的方法
	public void drawBullet(Graphics g) {
		g.drawImage(bImg, bx, by, null);
	}
	//子弹移动的方法
	public void moveBullet0() {
		by-=bSpeed;
		if (by<0) {
			exist=false;
		}
	}
	public void moveBullet1() {
		bx-=bSpeed;
		by-=bSpeed;
		if (by<0) {
			exist=false;
		}
	}
	public void moveBullet2() {
		bx+=bSpeed;
		by-=bSpeed;
		if (by<0) {
			exist=false;
		}
	}
}

3.Enemy类


public class Enemy {
	int ex;
	int ey;
	Image eImg;
	int eSpeed;
	PlaneJPanel mainview;
	boolean exist = true;
	int eTime = 10;
	//飞机和敌机撞击的标识
	boolean Y = false;
	public Enemy(int ex, int ey, Image eImg, int eSpeed, PlaneJPanel mainview) {
		super();
		this.ex = ex;
		this.ey = ey;
		this.eImg = eImg;
		this.eSpeed = eSpeed;
		this.mainview = mainview;
	}
	//画敌机
	public void drawEnemy(Graphics g) {
		g.drawImage(eImg, ex, ey, null);
	}
	//敌机移动
	public void moveEnemy() {
		ey+=eSpeed;
		if(ey>mainview.getHeight()) {
			exist = false;
		}
	}
	//子弹和敌机撞击
	public void hint(Bullet bullet,Enemy enemy) {
		int lx = Math.abs((bullet.bx+bullet.bImg.getWidth(null)/2)-(enemy.ex+enemy.eImg.getWidth(null)/2));
		int ly = Math.abs((bullet.by+bullet.bImg.getHeight(null)/2)-(enemy.ey+enemy.eImg.getHeight(null)/2));
		boolean H = lx<= (bullet.bImg.getWidth(null)/2+enemy.eImg.getWidth(null)/2);
		boolean R=ly<=(bullet.bImg.getHeight(null)/2+enemy.eImg.getHeight(null)/2);
		if (R&&H) {
			enemy.exist = false;
			bullet.exist = false;
		}
	}
	//飞机和敌机撞击
	public void hint1(PlaneJPanel panel,Enemy enemy) {
		int lx = Math.abs((enemy.ex+enemy.eImg.getWidth(null)/2)-(panel.px+panel.p[0].getWidth(null)/2));
		int ly = Math.abs((enemy.ey+enemy.eImg.getHeight(null)/2)-(panel.py+panel.p[0].getHeight(null)/2));
		boolean H= lx<=(panel.p[0].getWidth(null)/2+enemy.eImg.getWidth(null)/2);
		boolean R= ly<=(panel.p[0].getHeight(null)/2+enemy.eImg.getHeight(null)/2);
		if(H&&R) {
			enemy.exist = false;
			Y=true;//飞机和敌机撞击敌机死亡的标识
			panel.Pexist=false;//飞机和敌机撞击飞机撞击的标识
		}
	}
}

4.PlaneJFrame类


public class PlaneJFrame extends JFrame{
	public PlaneJFrame(){
		this.setTitle("飞机大战");
		this.setSize(420,640);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		PlaneJPanel jpanel = new PlaneJPanel();
		this.add(jpanel);
		this.setVisible(true);
	}
	public static void main(String[] args) {
		new PlaneJFrame();
	}
}

5.PlaneJPanel类


//流式访问文件
public class PlaneJPanel extends JPanel implements Runnable, MouseMotionListener, MouseListener {
	Thread t = null;
	static Image startImg;
	// 定义变量存储背景图片
	int x = 0, y = 0;
	// 游戏开始之前
	boolean ck = true;
	// 读取飞机图片
	static BufferedImage[] p = new BufferedImage[2];
	// 飞机坐标
	int px = 100, py = 100;
	int pc = 0;
	// 暂停开关
	boolean suspend = false;
	// 子弹存储图片对象
	static Image bImg;
	List<Bullet> bullets = new ArrayList<Bullet>();
	int count;
	// 奖励存储图片对象
	List<Award> awards = new ArrayList<Award>();
	static BufferedImage[] awa = new BufferedImage[3];
	// 飞机的血量
	int blood = 1;
	// 三个子弹标识
	boolean threeBullet = false;
	// 子弹存在的时间
	int threeTime;
	// 子弹加速
	int bulletSpeed = 3;
	int bulletTime;
	// 存储敌机
	List<Enemy> enemies = new ArrayList<Enemy>();
	// 控制血量
	boolean Pexist = true;
	int time = 15;
	// 得分
	int score = 0;
	static {// 静态代码块中进行最先的加载
		try {
			startImg = ImageIO.read(new File("image/GameInterface/interface_1.png"));
			p[0] = ImageIO.read(new File("image/1.png"));
			p[1] = ImageIO.read(new File("image/2.png"));
			bImg = ImageIO.read(new File("image/bullet/bullet_1.png"));

			awa[0] = ImageIO.read(new File("image/award/award_1.png"));
			awa[1] = ImageIO.read(new File("image/award/award_2.png"));
			awa[2] = ImageIO.read(new File("Image/award/award_3.png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public PlaneJPanel() {
		t = new Thread(this);
		// 添加鼠标监听
		addMouseListener(this);
		addMouseMotionListener(this);
	}

	public void paint(Graphics g) {
		super.paint(g);
		g.setColor(Color.white);
		g.drawImage(startImg, x, y, null);
		// 画飞机
		if (ck == false) {
			pc = pc == 0 ? 1 : 0;
			g.drawImage(p[pc], px, py, null);
		}
		for (int i = 0; i < bullets.size(); i++) {
			Bullet bullet = bullets.get(i);
			bullet.drawBullet(g);
		}
		// 画奖励
		for (int i = 0; i < awards.size(); i++) {
			Award award = awards.get(i);
			award.drawAward(g);
		}
		// 画血量
		if (ck == false) {
			if (blood > 0 && blood <= 5) {
				for (int i = 0; i < blood; i++) {
					g.drawImage(awa[0], 37 * i, 10, null);
				}
			} else if (blood <= 0) {
				try {
					p[0] = ImageIO.read(new File("image/blast/blast_2.png"));
					p[1] = ImageIO.read(new File("image/blast/blast_2.png"));
					startImg = ImageIO.read(new File("image/GameInterface/jeimian_2.png"));
					time--;
					if (time <= 0) {
						t.stop();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			} else {
				for (int i = 0; i < 6; i++) {
					g.drawImage(awa[0], 20 * i, 10, null);
				}
			}
		}
		// 画敌机
		if (ck == false) {
			for (int i = 0; i < enemies.size(); i++) {
				Enemy enemy = enemies.get(i);
				enemy.drawEnemy(g);
			}
		}
		// 写得分
		if (ck == false) {
			g.setFont(new Font("仿宋", Font.BOLD, 40));
			g.drawString("得分:" + score, 0, 80);
			//g.setColor(Color.white);
		}
	}

	public void run() {
		synchronized (this) {
			while (true) {
				count++;
				if (suspend) {
					try {
						wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				y++;
				if (y >= 0 && time > 0) {
					y = -5400;
				} else if (blood <= 0) {
					y = 0;
				}
				// 子弹移动的方法
				for (int i = 0; i < bullets.size(); i++) {
					Bullet bullet = bullets.get(i);
					if (bullet.bDirection == 0) {
						bullet.moveBullet0();
					}
					if (bullet.bDirection == 1) {
						bullet.moveBullet1();
					}
					if (bullet.bDirection == 2) {
						bullet.moveBullet2();
					}
				}
				// 创建子弹对象
				if (count % 20 == 0) {
					Bullet bullet0 = new Bullet(px + p[pc].getWidth() / 2 - bImg.getWidth(null) / 2, py, bImg,
							bulletSpeed, 0);
					bullets.add(bullet0);
					if (threeBullet) {
						Bullet bullet1 = new Bullet(px + p[pc].getWidth() / 2 - bImg.getWidth(null) / 2, py, bImg,
								bulletSpeed, 1);
						Bullet bullet2 = new Bullet(px + p[pc].getWidth() / 2 - bImg.getWidth(null) / 2, py, bImg,
								bulletSpeed, 2);
						bullets.add(bullet1);
						bullets.add(bullet2);
					}
				}
				// 奖励的创建
				if (count % 100 == 0) {
					int h = (int) (Math.random() * 3);
					int aSpeed = (int) (Math.random() * 5 + 3);
					Award award = new Award((int) (Math.random() * 300), -50, awa[h], aSpeed, h, this);
					awards.add(award);
				}
				// 奖励的移动
				for (int i = 0; i < awards.size(); i++) {
					Award award = awards.get(i);
					award.moveAward();
				}
				// 子弹变成三发
				if (threeBullet) {
					threeTime++;
				}
				if (threeTime == 300) {
					threeBullet = false;
					threeTime = 0;
				}
				// 子弹加速
				if (bulletSpeed == 10) {
					bulletTime++;
				}
				if (bulletTime == 300) {
					bulletSpeed = 3;
					bulletTime = 0;
				}
				// 创建敌机
				if (count % 20 == 0) {
					int eh = (int) (Math.random() * 5 + 2);
					Enemy enemy = new Enemy((int) (Math.random() * 350), -50,
							new ImageIcon("image/LittlePlane/plane" + eh + ".png").getImage(), eh, this);
					enemies.add(enemy);
				}
				for (int i = 0; i < enemies.size(); i++) {
					Enemy enemy = enemies.get(i);
					enemy.moveEnemy();
				}
				// 敌机和子弹碰撞
				for (int i = 0; i < enemies.size(); i++) {
					Enemy enemy = enemies.get(i);
					for (int j = 0; j < bullets.size(); j++) {
						Bullet bullet = bullets.get(j);
						enemy.hint(bullet, enemy);
					}
				}

				// 敌机和子弹碰撞之后敌机消失
				for (int i = 0; i < enemies.size(); i++) {
					Enemy enemy = enemies.get(i);
					if (enemy.exist == false && enemy.eTime == 0) {
						score++;
						enemies.remove(i);
					} else if (enemy.exist == false) {
						enemy.eTime--;
						enemy.eImg = new ImageIcon("image/blast/blast_1.png").getImage();
					}
				}
				// 子弹消失
				for (int i = 0; i < bullets.size(); i++) {
					Bullet bullet = bullets.get(i);
					if (bullet.exist == false) {
						bullets.remove(i);
					}
				}
				// 飞机和敌机撞击
				for (int i = 0; i < enemies.size(); i++) {
					Enemy enemy = enemies.get(i);
					enemy.hint1(this, enemy);
					if (enemy.exist == false && enemy.Y) {
						enemies.remove(i);
					}
				}
				// 血量减少
				if (Pexist == false) {
					blood--;
					Pexist = true;
				}
				try {
					Thread.sleep(20);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				repaint();
			}
		}
	}

	private Object Pexist(boolean b) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// 在开始游戏范围内点击
		if (ck && e.getX() >= 132 && e.getX() <= 259 && e.getY() >= 392 && e.getY() <= 432) {
			ck = false;
			try {
				startImg = ImageIO.read(new File("image/background/background_1.png"));
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			// 改变背景图片坐标
			y = -5400;
			t.start();
		}
		if (ck == false && e.getModifiers() == e.BUTTON3_MASK) {
			suspend = suspend ? resume() : suspend();
		}

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		// 鼠标移动到这个范围内改变状态
		if (ck && e.getX() >= 132 && e.getX() <= 259 && e.getY() >= 392 && e.getY() <= 432) {
			setCursor(new Cursor(Cursor.HAND_CURSOR));// 将鼠标的状态变成小手
		} else if (ck == false) {
			setCursor(new Cursor(Cursor.HAND_CURSOR));// 将鼠标的状态变成小手
		} else {
			setCursor(new Cursor(Cursor.DEFAULT_CURSOR));// 将鼠标状态变成箭头
		}
		px = e.getX() - p[pc].getWidth() / 2;
		py = e.getY() - p[pc].getHeight() / 2;

		if (px <= 0) {
			px = 0;
		}
		if (py <= 0) {
			py = 0;
		}
		if (px >= 400 - p[pc].getWidth()) {
			px = 400 - p[pc].getWidth();
		}
		if (py >= 600 - p[pc].getHeight()) {
			py = 600 - p[pc].getHeight();
		}
	}

	// 定义两个方法
	public boolean suspend() {
		suspend = true;
		return suspend;
	}

	public synchronized boolean resume() {
		suspend = false;
		notify();
		return suspend;
	}
}

package cn.feike.shoot; import java.awt.Graphics; import java.awt.image.BufferedImage; public abstract class FlyingObject { protected double x;//物体的x坐标 protected double y;//物体的y坐标 protected double width;//物体的宽 protected double heigth;//物体的高 protected BufferedImage image;//当前正在显示的图片 protected int index = 0;//图片数组下标序号,子类中使用 protected double step;//飞行物每次(1/24秒)移动的距离 protected int life;//命 protected int state;//飞行物的状态 public static final int ACTIVE=0;//活着状态 public static final int DEAD=1;//死亡状态 public static final int REMOVE=2;//回收状态 //默认构造器 public FlyingObject() { life = 1; state = ACTIVE; } //有参构造器 public FlyingObject(double width,double heigth){ this();//调用无参数的构造器,必须写在第一行. this.x = (int)(Math.random()*(480-width)); this.y = -heigth; this.width = width; this.heigth = heigth; step = Math.random()*3+0.8;//初始化step为[0.8,3.8)之间的数 } //重写toString方法 public String toString() { return x+","+y+","+width+","+heigth+","+image; } //重写paint,方便子类对象的使用 public void paint(Graphics g) { g.drawImage(image, (int)x, (int)y, null);//绘制图片 } //飞行物移动的move方法 /** * 重构了move,方法实现播放销毁动画功能 */ public void move(){ if(state == ACTIVE){ y += step; return ; } if(state == DEAD){ //从子类对象中获取下一张照片 BufferedImage img = nextImage(); if(img == null){ state = REMOVE;//没有照片则回收 }else{ image = img;//否则把子类的图片传给image } //越界则销毁 if(y>=825){ state = REMOVE; } } } /** * 子类中必须有的方法,返回下一个要播放的照片引用, * 如果返回null表示没有可播放的照片了. */ protected abstract BufferedImage nextImage(); /** * 飞行物被打了一下 */ public void hit(){ if(life>0){ life--; } if(life==0){ state = DEAD; } } /** * 碰撞检测的方法 * 检测物体的位置是否在碰撞的范围内. * (子弹是否在飞行物的碰撞范围内) */ public boolean duang(FlyingObject obj){ //this(x,y,w,h) //obj(x,y,w,h) double x1 = this.x - obj.width; double x2 = this.x + this.width; double y1 = this.y - obj.width; double y2 = this.y + this.heigth; return x1<obj.x&&obj;.x<x2&&y1;<obj.y&&obj;.y<y2; } /** 重构FlyingObject,添加了状态检查方法 */ /** 检查飞行物死了吗 */ public boolean isDead(){ return state == DEAD; } /** 检查飞行物是否活动的 */ public boolean isActive(){ return state == ACTIVE; } /** 检查飞行是否可以被删除*/ public boolean canRemove(){ return state == REMOVE; } /** 飞行物添加"去死"方法*/ public void goDead(){ if(isActive()){ state = DEAD; } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值