面向对象飞机大战基础学习

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.awt.Graphics;

/** 飞行物 */
public abstract class FlyingObject {
	protected int width;
	protected int height;
	protected int x;
	protected int y;
	
	public static final int LIFE = 0;
	public static final int DEAD = 1;
	public static final int REMOVE = 2;
	protected int state = LIFE;
	
	public abstract void step();
	
	public abstract BufferedImage getImage();
	
	public void paint(Graphics g){
		g.drawImage(getImage(),x,y,null);
	}
	
	public boolean isLife(){
		return state==LIFE;
	}
	
	public boolean isDead(){
		return state==DEAD;
	}
	
	public boolean isRemove(){
		return state==REMOVE;
	}
 
	public void goDead(){
		state = DEAD;
	}
	
	public boolean hit(FlyingObject other){
		int x1 = this.x-other.width;
		int x2 = this.x+this.width;
		int y1 = this.y-other.height;
		int y2 = this.y+this.height;
		int x = other.x;
		int y = other.y;
		
		return x>=x1 && x<=x2 && y>=y1 && y<=y2;
	}
	
	public abstract boolean outOfBounds();
}

public interface Award {
	public int DOUBLE_FIRE = 0;
	public int LIFE = 1;
	public int getType();
}

```java得分接口
public interface Enemy {
	public int getScore();
}
 

```java敌机源码
import java.awt.image.BufferedImage;

/** 小敌机 */
public class Airplane extends FlyingObject implements Enemy {
	private int step;
	
	/** 构造方法 */
	public Airplane(){
		width = 48;
		height = 50;
		x = (int)(Math.random()*(World.WIDTH-this.width));
		y = -this.height;
		step = 2; //移动速度
	}
	
	/** 小敌机走步step() */
	public void step(){
		y+=step;
	}
	
	int deadIndex = 1;
	public BufferedImage getImage(){
		if(isLife()){
			return Images.airplanes[0];
		}else if(isDead()){
			BufferedImage img = Images.airplanes[deadIndex++];
			if(deadIndex==Images.airplanes.length){
				state = REMOVE;
			}
			return img;
		}
		return null;
	}
	
	public int getScore(){
		return 1;
	}
	
	public boolean outOfBounds(){
		return this.y>=World.HEIGHT;
	}
	
}

大敌机源码

import java.awt.image.BufferedImage;
/** 大敌机 */
public class BigAirplane extends FlyingObject implements Enemy {
	private int step;
	
	/** 构造方法 */
	public BigAirplane(){
		width = 66;
		height = 89;
		x = (int)(Math.random()*(World.WIDTH-this.width));
		y = -this.height;
		step = 2;
	}
	
	/** 大敌机走步step() */
	public void step(){
		y+=step;
	}
	
	int deadIndex = 1;
	public BufferedImage getImage(){
		if(isLife()){
			return Images.bigairplanes[0];
		}else if(isDead()){
			BufferedImage img = Images.bigairplanes[deadIndex++];
			if(deadIndex==Images.bigairplanes.length){
				state = REMOVE;
			}
			return img;
		}
		return null;
	}
	

	public int getScore(){
		return 3;
	}
	
	public boolean outOfBounds(){
		return this.y>=World.HEIGHT;
	}
}

小蜜蜂源码

import java.awt.image.BufferedImage;
/** 小蜜蜂 */
public class Bee extends FlyingObject implements Award {
	private int xStep; //x坐标走步
	private int yStep; //y坐标走步
	private int awardType; //奖励类型
	
	/** 构造方法 */
	public Bee(){
		width = 60;
		height = 51;
		x = (int)(Math.random()*(World.WIDTH-this.width));
		y = -this.height;
		xStep = 1;
		yStep = 2;
		awardType = (int)(Math.random()*2);
	}
	
	/** 蜜蜂走步step() */
	public void step(){
		x+=xStep;
		y+=yStep;
		if(x>=World.WIDTH-this.width || x<=0){
			xStep*=-1;
		}
	}

	int deadIndex = 1;
	public BufferedImage getImage(){
		if(isLife()){
			return Images.bees[0];
		}else if(isDead()){
			BufferedImage img = Images.bees[deadIndex++];
			if(deadIndex==Images.bees.length){
				state = REMOVE;
			}
			return img;
		}
		return null;
	}
	
	public int getType(){
		return awardType;
	}
	
	public boolean outOfBounds(){
		return this.y>=World.HEIGHT;
	}
}

子弹源码

import java.awt.image.BufferedImage;
/** 子弹 */
public class Bullet extends FlyingObject  {
	private int step;
	
	/** 构造方法 */
	public Bullet(int x,int y){
		width = 8;
		height = 20;
		this.x = x;
		this.y = y;
		step = 3;
	}
	
	/** 子弹走步step() */
	public void step(){
		y-=step;
	}
	
	public BufferedImage getImage(){
		if(isLife()){
			return Images.bullet;
		}
		if(isDead()){
			state = REMOVE;
		}
		return null;
	}
	
	public boolean outOfBounds(){
		return this.y<=-this.height;
	}
}

英雄级源码

import java.awt.image.BufferedImage;
/** 英雄机 */
public class Hero extends FlyingObject  {
	private int life; //命
	private int doubleFire; //火力值
	
	/** 构造方法 */
	public Hero(){
		width = 97;
		height = 139;
		x = 140;
		y = 400;
		life = 3;
		doubleFire = 0;
	}
	
	public void step(){
	}
	
	/** 英雄机随着鼠标动   x:鼠标的x坐标  y:鼠标的y坐标 */
	public void moveTo(int x,int y){
		this.x = x-this.width/2;
		this.y = y-this.height/2;
	}
	
	private int index = 0;
	public BufferedImage getImage(){
		if(isLife()){
			return Images.heros[index++%Images.heros.length];
		}
		return null;
	}
	
	public Bullet[] shoot(){
		int xStep = this.width/4;
		int yStep = 20;
		if(doubleFire>0){
			Bullet[] bs = new Bullet[2];
			bs[0] = new Bullet(this.x+1*xStep,this.y-yStep);
			bs[1] = new Bullet(this.x+3*xStep,this.y-yStep);
			doubleFire-=2;
			return bs;
		}else{
			Bullet[] bs = new Bullet[1];
			bs[0] = new Bullet(this.x+2*xStep,this.y-yStep);
			return bs;
		}
	}
	
	public void addLife(){
		life++;
	}
	
	public void addDoubleFire(){
		doubleFire+=40;
	}
	
	public int getLife(){
		return life;
	}
	
	public void subtractLife(){
		life--;
	}
	
	public int getDoubleFire(){
		return doubleFire;
	}
	
	public void clearDoubleFire(){
		doubleFire = 0;
	}
	
	public boolean outOfBounds(){
		return false;
	}
	
}

初始化图片

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

/** 图片工具类 */
public class Images {
	public static BufferedImage sky;
	public static BufferedImage bullet;
	public static BufferedImage[] airplanes;
	public static BufferedImage[] bigairplanes;
	public static BufferedImage[] bees;
	public static BufferedImage[] heros;
	
	static{
		sky = loadImage("background.png");
		bullet = loadImage("bullet.png");
		
		airplanes = new BufferedImage[5];
		airplanes[0] = loadImage("airplane0.png");
		bigairplanes = new BufferedImage[5];
		bigairplanes[0] = loadImage("bigairplane0.png");
		bees = new BufferedImage[5];
		bees[0] = loadImage("bee0.png");
		
		for(int i=1;i<airplanes.length;i++){
			airplanes[i] = loadImage("bom"+i+".png");
			bigairplanes[i] = loadImage("bom"+i+".png");
			bees[i] = loadImage("bom"+i+".png");
		}
		
		heros = new BufferedImage[2];
		for(int i=0;i<heros.length;i++){
			heros[i] = loadImage("hero"+i+".png");
		}
	}
	
	public static BufferedImage loadImage(String fileName){
		try{
			BufferedImage img = ImageIO.read(FlyingObject.class.getResource(fileName));
			return img;
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
	
}

天空源码

import java.awt.image.BufferedImage;
import java.awt.Graphics;

/** 天空 */
public class Sky extends FlyingObject  {
	private int step;
	private int y1; //天空移动
	
	
	/** 构造方法 */
	public Sky(){
		width = World.WIDTH;
		height = World.HEIGHT;
		x = 0;
		y = 0;
		step = 1;
		y1 = -this.height;
	}
	
	/** 天空走步step() */
	public void step(){
		y+=step;
		y1+=step;
		if(y>=this.height){
			y=-this.height;
		}
		if(y1>=this.height){
			y1=-this.height;
		}
	}
	
	public BufferedImage getImage(){
		return Images.sky;
	}
	
	public void paint(Graphics g){
		g.drawImage(getImage(),x,y,null);
		g.drawImage(getImage(),x,y1,null);
	}
	
	public boolean outOfBounds(){
		return false;
	}
	
}

界面

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

/** 世界 */
public class World extends JPanel{
	public static final int WIDTH = 400;
	public static final int HEIGHT = 700;
	private Sky sky = new Sky();
	private Hero hero = new Hero();
	private FlyingObject[] enemies = {}; //敌人数组
	private Bullet[] bullets = {}; //子弹数组
	
	public static final int START = 0;
	public static final int RUNNING = 1;
	public static final int PAUSE = 2;
	public static final int GAME_OVER = 3;
	private int state = START;
	
	public static BufferedImage start;
	public static BufferedImage pause;
	public static BufferedImage gameover;
	static{
		start = Images.loadImage("start.png");
		pause = Images.loadImage("pause.png");
		gameover = Images.loadImage("gameover.png");
	}
	
	
	public FlyingObject nextOne(){
		Random rand = new Random();
		int type = rand.nextInt(20);
		if(type < 5){
			return new Bee();
		}else if(type < 12){
			return new Airplane();
		}else{
			return new BigAirplane();
		}
	}
	
	int flyEnterIndex = 0;
	public void enterAction(){
		flyEnterIndex++;
		if(flyEnterIndex%1==0){
			FlyingObject obj = nextOne();
			enemies = Arrays.copyOf(enemies,enemies.length+1);
			enemies[enemies.length-1] = obj;
		}
	}
	
	public void stepAction(){
		sky.step();
		for(int i=0;i<enemies.length;i++){
			enemies[i].step();
		}
		for(int i=0;i<bullets.length;i++){
			bullets[i].step();
		}
	}
	
	int shootIndex = 0;
	public void shootAction(){
		shootIndex++;
		if(shootIndex%3==0){
			Bullet[] bs = hero.shoot();
			bullets = Arrays.copyOf(bullets,bullets.length+bs.length);
			System.arraycopy(bs,0,bullets,bullets.length-bs.length,bs.length);
		}
	}
	
	private int score = 0;
	public void bangAction(){
		for(int i=0;i<bullets.length;i++){
			Bullet b = bullets[i];
			for(int j=0;j<enemies.length;j++){
				FlyingObject f = enemies[j];
				if(b.isLife() && f.isLife() && b.hit(f)){
					b.goDead();
					f.goDead();
					if(f instanceof Enemy){
						Enemy e = (Enemy)f;
						score += e.getScore();
					}
					if(f instanceof Award){
						Award a = (Award)f;
						int type = a.getType();
						switch(type){
						case Award.DOUBLE_FIRE:
							hero.addDoubleFire();
							break;
						case Award.LIFE:
							hero.addLife();
							break;
						}
					}
					
				}
			}
		}
	}
	
	public void outOfBoundsAction(){
		int index = 0;
		FlyingObject[] enemyLives = new FlyingObject[enemies.length];
		for(int i=0;i<enemies.length;i++){
			FlyingObject e = enemies[i];
			if(!e.outOfBounds() && !e.isRemove()){
				enemyLives[index++] = e;
			}
		}
		enemies = Arrays.copyOf(enemyLives,index);
		
		index = 0;
		Bullet[] bulletLives = new Bullet[bullets.length];
		for(int i=0;i<bullets.length;i++){
			Bullet b = bullets[i];
			if(!b.outOfBounds() && !b.isRemove()){
				bulletLives[index++] = b;
			}
		}
		bullets = Arrays.copyOf(bulletLives,index);
	}
	
	public void hitAction(){
		for(int i=0;i<enemies.length;i++){
			FlyingObject f = enemies[i];
			if(f.isLife() && hero.isLife() && hero.hit(f)){
				f.goDead();
				hero.subtractLife();
				hero.clearDoubleFire();
			}
		}
	}
	
	public void checkGameOverAction(){
		if(hero.getLife()<=0){
			state = GAME_OVER;
		}
	}
	
	int num = 1;
	public void action(){ //启动执行
		MouseAdapter l = new MouseAdapter(){
			public void mouseMoved(MouseEvent e){
				if(state==RUNNING){
					int x = e.getX();
					int y = e.getY();
					hero.moveTo(x, y);
				}
			}
			public void mouseClicked(MouseEvent e){
				switch(state){
				case START:
					state = RUNNING;
					break;
				case GAME_OVER:
					score = 0;
					hero = new Hero();
					enemies = new FlyingObject[0];
					bullets = new Bullet[0];
					state = START;
					break;
				}
			}
			public void mouseExited(MouseEvent e){
				if(state==RUNNING){
					state = PAUSE;
				}
			}
			public void mouseEntered(MouseEvent e){
				if(state==PAUSE){
					state = RUNNING;
				}
			}
		};
		this.addMouseListener(l);
		this.addMouseMotionListener(l);
		
public abstract class FlyingObject { public static final int LIFE = 0; //活着呢 public static final int DEAD = 1; //死了的(先爆破) public static final int REMOVE = 2; //删除了(爆破后删除) protected int state = LIFE; //当前状态(默认活着) protected int width; //宽 protected int height; //高 protected int x; //x坐标 protected int y; //y坐标 /** 无参构造方法 */ public FlyingObject(){ } /**专门给小敌机、大敌机、小蜜蜂提供的构造方法 */ public FlyingObject(int width,int height){ this.width = width; this.height = height; Random rand = new Random(); x = rand.nextInt(World.WIDTH-width); //x:0到(窗口-小敌机的宽)之间的随机数 y = -height; //y:负的小敌机的高 } /** 专门给英雄机、子弹、天空提供的构造方法 */ public FlyingObject(int width,int height,int x,int y){ this.width = width; this.height = height; this.x = x; this.y = y; } /** 读取图片 */ public static BufferedImage loadImage(String fileName){ try{ BufferedImage img = ImageIO.read(FlyingObject.class.getResource(fileName)); return img; }catch(Exception e){ e.printStackTrace(); throw new RuntimeException(); } } /** 飞行物移动了 */ public abstract void step(); public abstract BufferedImage getImage(); /** 判断是否活着 */ public boolean isLife(){ return state==LIFE; } /** 判断是否死了的 */ public boolean isDead(){ return state==DEAD; } /** 判断是否删除的 */ public boolean isRemove(){ return state==REMOVE; } /** 画对象 g:画笔 */ public void paintObject(Graphics g){ g.drawImage(getImage(), x, y, null); } /** 检测飞行物是否越界 */ public abstract boolean outOfBounds(); /** 敌人与子弹/英雄机的碰撞 this:敌人 other:子弹或英雄机 */ public boolean hit(FlyingObject other){ int x1 = this.x-other.width; //x1:敌人的x int x2 = this.x+this.width; //x2:敌人的x int y1 = this.y-other.height; //y1:敌人的y int y2 = this.y+this.height; //y2:敌人的y int x = other.x; //x:子弹的x int y = other.y; //y:子弹的y return x>=x1 && x=y1 && y<=y2; } public void goDead(){ state = DEAD; //修改当前状态为死了的 } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值