java-飞机大战

杭涵映
2023-12-01

java-飞机大战

import java.util.Random;
//敌机:既是飞行物,也是敌人
public class Airplane extends FlyingObject  implements Enemy{
	private int speed = 2;
	public Airplane() {
		image = Shootgame.airplane;
		width = image.getWidth();
		height   = image.getHeight();
		Random rand = new Random();//随机参数
		x = rand.nextInt(Shootgame.WIDTH-this.width);//窗口宽-敌机
		//y = -this.height;//负的敌机高
		y = -this.height;
		 
	}
	public int getScore() {
		return 5;//打掉一个低级的五分
	}
	public void step() {
		y+=speed; 
		
	}
  public boolean outOfBounds(){//重写
	  return this.y>=Shootgame.HEIGHT;//敌机的窗口高
	  
  }
}
public interface Award {
	public int DOUBLE_FIRE=0;//火力值
	public int LIFE=1;//命
	public int getType();//获取奖励类型,返回0为火力值,返回1为命
	
}

import java.awt.image.*;

public abstract class FlyingObject {
	protected BufferedImage image;//圖片
	protected int width;//
	protected int height;
	protected int x;
	protected int y;
	
	public abstract void step();
	public abstract boolean outOfBounds();//检查是否越界
	//敌人被子弹攻击
	public boolean shootBy(Bullet bullet) {
		int x1 = this.x ;
		int x2 = this.x+this.width;
		int y1 = this.y;
		int y2 = this.y+this.height;
		int x = bullet.x ;
		int y = bullet.y;
		return x>x1 && x<x2
				&&
				y>y1 && y<y2;//x在x1和x2之间
				

import java.awt.image.*;
import java.util.Random;

//英雄机:飞行物
public class Hero extends FlyingObject {
	private int life;
	private int doubleFire;// 火力值
	private BufferedImage[] images;
	private int index;// 协助图片切换

	public Hero() {
		image = Shootgame.hero0;
		width = image.getWidth();
		height = image.getHeight();
		Random rand = new Random();// 随机参数
		x = 150;
		y = 400;// 固定方法
		life = 3;
		doubleFire = 0;// 为零既单倍火力
		images = new BufferedImage[] { Shootgame.hero0, Shootgame.hero1 };
		index = 0;

	}

	public void step() {// 10毫秒走一次

		image = images[index++ / 10 % images.length];// 没一百毫秒切换一次

	}

	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);
			return bs;
		} else {// 单倍火力
			Bullet[] bs = new Bullet[1];
			bs[0] = new Bullet(this.x + 2 * xStep, this.y - yStep);
			doubleFire-=2;//发射一次双倍火力减二
			return bs;
		}

	}
	//英雄机随着鼠标动
	public void moveTo(int x,int y) {
		this.x = x-this.width/2;
		this.y = y- this.height/2;
		
	}
	public boolean outOfBounds(){
		 return false;//子弹的y小于等于-的子弹的高
	  }
	public void addLife() {
		life++;
	}
	public void subtractLife() {
		life--;
	}
	public void clearDoubleFire() {
		doubleFire=0;//火力值归零
	}
	public int getLife() {
		return life;
	}
	public void addDoubleFire() {
		doubleFire+=40;

	
	}//英雄机撞敌人 this :英雄机 obj :敌人
	public boolean hit(FlyingObject obj) {
		int x1 = obj.x-this.width/2;
		int x2 = obj.width+this.width/2;
		int y1 = obj.y-this.height/2;
		int y2 = obj.y+obj.y+this.height/2;
		int x = this.x+this.width/2;
		int y= this.y+this.height/2;
		return x>x1 && x<x2
				&&
				y>y1&& y<y2;
		
		
	}
}
import java.awt.image.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Arrays;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.color.*;
import java.awt.Color;
import java.awt.Font;
//主程序类 

public class Shootgame extends JPanel {
	public static final int WIDTH = 400;// 窗口宽
	public static final int HEIGHT = 654;// 窗口高
	public static BufferedImage background;// 静态变量 背景图
	public static BufferedImage start;// 静态变量 背景图
	public static BufferedImage pause;// 静态变量 背景图
	public static BufferedImage gameover;// 静态变量 背景图上
	public static BufferedImage hero0;// 静态变量 背景图
	public static BufferedImage hero1;// 静态变量 背景图
	public static BufferedImage airplane;// 静态变量 背景图
	public static BufferedImage bullet;// 静态变量 背景图
	public static BufferedImage bee;// 静态变量 背景图
	public static final int START = 0;
	public static final int RUNNING = 1;
	public static final int PAUSE = 2;
	public static final int GAMEOVER = 3;
	private int state = START;
	private Hero hero = new Hero();
	private FlyingObject[] flyings = {};// 敌人和小蜜蜂
	private Bullet[] bullets = {}; // 子弹数组

	static {// 初始化静态资源图片

		try {
			background = ImageIO.read(Shootgame.class.getResource("background.png"));
			start = ImageIO.read(Shootgame.class.getResource("start.png"));
			pause = ImageIO.read(Shootgame.class.getResource("pause.png"));
			gameover = ImageIO.read(Shootgame.class.getResource("gameover.png"));
			hero0 = ImageIO.read(Shootgame.class.getResource("hero0.png"));
			hero1 = ImageIO.read(Shootgame.class.getResource("hero1.png"));
			airplane = ImageIO.read(Shootgame.class.getResource("airplane.png"));
			bullet = ImageIO.read(Shootgame.class.getResource("bullet.png"));
			bee = ImageIO.read(Shootgame.class.getResource("bee.png"));

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 重写paint g:画笔
	// 生成敌人对象

	public FlyingObject nextOne() {

		Random rand = new Random();
		int type = rand.nextInt(20);// 生成0到19之间的随机数
		if (type < 5) {// 随机数小于四时生成蜜蜂对象
			return new Bee();

		} else {// 随机数在5到十九之间返回敌机对象
			return new Airplane();
		}

	}

	int flyEnteredIndex = 0;

	public void enterAction() {
		// 生成敌人对象,将对象添加到数组中
		flyEnteredIndex++;// 每10毫秒加1
		if (flyEnteredIndex % 40 == 0) {
			// 多长时间走一次s
			FlyingObject one = nextOne();
			flyings = Arrays.copyOf(flyings, flyings.length + 1);
			flyings[flyings.length - 1] = one;// 将生成的敌人添加数组到最后一位

		}
	}

	// 飞行物走一步
	public void stepAction() {
		hero.step();
		for (int i = 0; i < flyings.length; i++) {
			flyings[i].step();
		}
		for (int i = 0; i < bullets.length; i++) {
			bullets[i].step();// 让子弹走一步
		}

	}

	int shootIndex = 0;

	public void shootAction() {
		shootIndex++;// 10毫秒走一次
		if (shootIndex % 30 == 0) {
			// 创建子弹对象 将子弹对象添加到bullet;
			Bullet[] bs = hero.shoot();
			bullets = Arrays.copyOf(bullets, bullets.length + bs.length);// 扩容根据bs来弹性增加
			System.arraycopy(bs, 0, bullets, bullets.length - bs.length, bs.length);// 数组的追加
		}

	}

	public void outOfBoundsAction() {
		int index = 0;// 不越界敌人的个数
		FlyingObject[] flyingLives = new FlyingObject[flyings.length];
		for (int i = 0; i < flyings.length; i++) {
			FlyingObject f = flyings[i];
			if (!f.outOfBounds()) {// 若不越界
				flyingLives[index] = f;
				index++;

			}

		}
		flyings = Arrays.copyOf(flyingLives, index);// 将不越界的都复制进去
		index = 0;
		Bullet[] bulletLives = new Bullet[bullets.length];
		for (int i = 0; i < bullets.length; i++) {
			Bullet b = bullets[i];
			if (!b.outOfBounds()) {
				bulletLives[index] = b;
				index++;
			}
		}

	}

	public void bangAction() {
		for (int i = 0; i < bullets.length; i++) {
			Bullet b = bullets[i];
			bang(b);

		}

	}

	int score = 0;

	public void bang(Bullet b) {
		int index = -1;// 被撞敌人下标
		for (int i = 0; i < flyings.length; i++) {
			FlyingObject f = flyings[i];
			if (f.shootBy(b)) {
				index = i;
				break;// 其余敌人不在比较
			}

		}
		if (index != -1) {// 撞上了
			FlyingObject one = flyings[index];// 获取被撞得敌人
			if (one instanceof Enemy) {// 若是敌人
				Enemy e = (Enemy) one;// 将one强转为的人
				score += e.getScore();// 玩家得分

			}
			if (one instanceof Award) {
				Award a = (Award) one;// 强转为奖励
				int type = a.getType();// 获取奖励类型
				switch (type) {// 根据奖励类型来获取不同的奖励
				case Award.DOUBLE_FIRE:// 若为火力
					hero.addDoubleFire();// 英雄机增加火力
					break;
				case Award.LIFE:
					hero.addLife();// 为英雄机增加生命
				}
			} // 将被撞敌人数组最后一个元素交换
			FlyingObject t = flyings[index];
			flyings[index] = flyings[flyings.length - 1];
			flyings[flyings.length - 1] = t;
			flyings = Arrays.copyOf(flyings, flyings.length - 1);
		}

	}

	public void checkGameOverAction() {
		if (isGameOver()) {

		}
	}

	public boolean isGameOver() {
		for (int i = 0; i < flyings.length; i++) {
			FlyingObject f = flyings[i];
			if (hero.hit(f)) {
				hero.subtractLife();
				hero.clearDoubleFire();
				FlyingObject t = flyings[i];
				flyings[i] = flyings[flyings.length - 1];
				flyings[flyings.length - 1] = t;
				flyings = Arrays.copyOf(flyings, flyings.length - 1);
			}
		}
		return hero.getLife() <= 0;

	}

	public void action() {
		MouseAdapter l = new MouseAdapter() {
			public void mouseMoved(MouseEvent e) {
				// 英雄机随着动
				int x = e.getX();
				int y = e.getY();
				hero.moveTo(x, y);// 英雄机跟着鼠标动
			}
		};
		this.addMouseListener(l);// 处理鼠标操作
		this.addMouseMotionListener(l);// 处理鼠标滑动时间
		Timer timer = new Timer();
		int intervel = 10;// 时间间隔(以毫秒为单位)
		timer.schedule(new TimerTask() {
			public void run() {
				enterAction();
				stepAction();// 飞行物走一步
				shootAction();// 子弹入场
				outOfBoundsAction();

				bangAction();// 子弹与敌人的碰撞
				checkGameOverAction();
				repaint();// 重画--调用paint()1111111
			}
		}, intervel, intervel);

	}

	public void paint(Graphics g) {
		g.drawImage(background, 0, 0, null);
		paintHero(g);// 英雄机
		paintFlyingObjects(g);// 画敌人
		paintBullets(g);// 子弹对象
		paintScoreAndLife(g);
		paintState(g);
	}

	private void paintBullets(Graphics g) {
		for (int i = 0; i < bullets.length; i++) {
			Bullet b = bullets[i];
			g.drawImage(b.image, b.x, b.y, null);

		}
		g.drawImage(hero.image, hero.x, hero.y, null);

	}

	private void paintHero(Graphics g) {
		// TODO Auto-generated method stub

	}

	private void paintFlyingObjects(Graphics g) {
		for (int i = 0; i < flyings.length; i++) {
			FlyingObject f = flyings[i];// 获取每一个敌人
			g.drawImage(f.image, f.x, f.y, null);

		}
		// TODO Auto-generated method stub

	}

	public void paintScoreAndLife(Graphics g) {
		g.setColor(new Color(0xFF0000));
		g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24));
		g.drawString("SCORE:" + score, 10, 25);
		g.drawString("LIFE:" + hero.getLife(), 10, 45);

	}

	public static void main(String[] args) {
		JFrame frame = new JFrame("Fly");// 创建了一个窗口
		Shootgame game = new Shootgame();// 创建一个面板
		frame.add(game);// 将面板添加到窗口上
		frame.setSize(WIDTH, HEIGHT);//
		frame.setAlwaysOnTop(true);// 设置在最上面
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//
		frame.setLocationRelativeTo(null);
		frame.setVisible(true); // 设置窗口可见

		game.action();// 启动程序执行的

	}

	public void paintState(Graphics g) {
		switch (state) {
		case START:
			g.drawImage(start, 0, 0, null);
			break;
		case PAUSE:
			g.drawImage(pause, 0, 0, null);
			break;
		case GAMEOVER:
			g.drawImage(gameover, 0, 0, null);
			break;
		}

	}
}
import java.util.Random;

//子弹:飞行物 
public class Bullet extends FlyingObject {
	private int speed = 3;// 走步的步数

	public Bullet(int x, int y) {
		image = Shootgame.bullet;
		width = image.getWidth();
		height = image.getHeight();
		Random rand = new Random();// 随机参数
		this.x = x;
		this.y =  y;
		// x:子弹的x跟随英雄机的位置计算

	}

	public void step() {
		y-=speed;

	}
	 public boolean outOfBounds(){
		 return this.y<=this.height;//子弹的y小于等于-的子弹的高
	  }
}

import java.util.Random;

public class Bee extends FlyingObject implements Award {
	private int xSpeed = 1;// x
	private int ySpeed = 2;//
	private int awardType;// 奖励类型
	
  
	public Bee() {
		image = Shootgame.bee;
		width = image.getWidth();
		height = image.getHeight();
		Random rand = new Random();// 随机参数
		x = rand.nextInt(Shootgame.WIDTH - this.width);// 窗口宽-敌机
		// y = -this.height;//负的蜜蜂的高
		awardType = rand.nextInt(2);// 0到1之间的随机数
		y = -this.height;
	}

	/** 重写 */
	public int getType() {
		return awardType;
	}

	public void step() {
		x+=xSpeed;//x+(向左或向右)
		y+=ySpeed;//y+(向下)
		if(x>=Shootgame.WIDTH-this.width) {//窗口宽-蜜蜂宽
			xSpeed =-1;
		}
		if (x<=0) {// x<=0 时,x+(向右)
			xSpeed = 1;
		
		}
		
	}
	 public boolean outOfBounds(){
		  return this.y>=Shootgame.HEIGHT;
	  }
}

分为七个模块来写

  1. 敌机:既是飞行物,也是敌人
  2. 奖励类型
  3. 飞行物
  4. 英雄级也是:飞行物
  5. 主程序 入口
  6. 子弹
  7. 蜜蜂

java萌新
欢迎提出意见
共同学习
一起进步
书似青山常乱叠,灯如红豆更相思!

 类似资料: