飞机大战-java基础小程序(初学项目)07

2022-07-30,,,,

Shoot射击游戏第七天:
1.敌人入场:
  1)敌人是由窗口产生的,所以在World中设计nextOne()生成敌人对象
  2)敌人入场为定时发生的,所以在run中调用enterAction()实现敌人入场
    在enterAction()中:
      每400毫秒,获取敌人对象obj,enemies扩容,将obj装到enemies最后一个元素上
    注意:在run中调用enterAction()方法的下面一定要调用repaint()方法来重画
2.子弹入场:
  1)子弹是由英雄机发射出来的,所以在Hero中设计shoot()生成子弹对象
  2)子弹入场为定时发生的,所以在run中调用shootAction()实现子弹入场
    在shootAction()中:
      每300毫秒,获取子弹数组对象bs,bullets扩容,数组的追加
3.飞行物移动:
  1)飞行物移动为对象的共有行为,所以在FlyingObject中设计抽象方法step(),派生类中重写
  2)飞行物移动为定时发生的,所以在run中调用stepAction()实现飞行物移动
    在stepAction()中:
      天空动,遍历敌人敌人动,遍历子弹子弹动

 

 

知识点:

1.成员内部类: 应用率低,了解
  1)类中套类,外面的称为Outer外部类,里面的称为Inner内部类
  2)内部类通常只服务于外部类,对外不具备可见性
  3)内部类对象通常是在外部类中创建的
  4)内部类中可以直接访问外部类的成员(包括私有的)
    内部类中有个隐式的引用指向了创建它的外部类对象
      语法: 外部类名.this
2.匿名内部类: 大大的简化代码
  1)若想创建一个类(派生类)的对象,并且对象只创建一次,
    此时该类不必命名,称之为匿名内部类
  2)匿名内部类中若想访问外面的变量,该变量必须是final的(JDK1.7(含)以前的要求)

常见面试题:
 问:内部类有独立的.class吗?
 答:有

 

 

 

 

 

 

 

 

 

 

 

 

 

做功能的套路:
1.先写行为/方法:
  1)若为对象所特有的行为,则将方法设计在对应的类中
  2)若为对象所共有的行为,则将方法设计在超类中
2.窗口调用:
  1)定时触发的,在定时器中调用
  2)事件触发的,在侦听器中调用-------明天讲

问题常见bug、错误分三种:
1)编译错误:编译时发生,都是因为语法错误了
2)运行异常:运行时发生,异常后都有个at...,已经明确哪儿的异常了
3)程序运行的结果与你所预期的结果不同:
  ----打桩: System.out.println(数据);
  ----打桩得慢慢来,刚开始打不好,但是一定要有意识的自己打桩解决问题

假设enemies中有5个敌人了,现在又来了一个敌人
---将enemies扩容,原始5个敌人不变,末尾补个默认值null
---将新生成的敌人obj装到末尾(最后一个元素上)

public FlyingObject nextOne() {
  Random rand = new Random();
  int type = rand.nextInt(3); //0到2
  if(type==0) {
    return new Bee();
  }else if(type==1) {
    return new Airplane();
  }else {
    return new BigAirplane();
  }
}

敌人入场:
  敌人是由窗口产生的,所以将创建敌人方法设计在World类中
子弹入场:
  子弹是由英雄机发射出来的,所以将创建子弹方法设计在Hero类中
飞行物移动:
  飞行物移动为对象所共有的行为,所以将飞行物移动方法设计在FlyingObject类中

敌人入场:---------------自动发生的
子弹入场:---------------自动发生的
飞行物移动:-------------自动发生的

定闹表每天早晨7点响这个事-----只走1次

第一个10: 从程序启动到第1次触发的时间间隔

第二个10: 从第1次触发到第2次触发的时间间隔 
          从第2次触发到第3次触发的时间间隔
      从第3次触发到第4次触发的时间间隔
      ....

我们创建一个时间来每隔一段时间检测一遍代码,然后把需要添加的行为放到这个方法里面这样我们就可以动起来.

 

代码:

package cn.tude;


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

public class Airplane extends FlyingObject {

	private int speed;// 移动速度

	public Airplane() {
		super(48, 50);
		speed = 2;

	}

	public void step() {
		
		y+=speed;
	}

	private int index = 1;

	public BufferedImage getImage() {
		if (isLife()) {
			return Images.airs[0];

		} else if (isDead()) {// 若死了的
			BufferedImage img = Images.airs[index++];// 获取爆破图
			if (index == Images.airs.length) {
				state = REMOVE;
			}
			return img;
			/*
			 * index=1 10M img=airs[1] index=2 返回 airs[1] 20M img=airs[2]
			 * index=3 返回airs[2] 30M img=airs[3] index=4 返回airs[3] 40M
			 * img=airs[4] ingex=5 (REMOVE) 返回airs[4] 50M 返回null(不返回图片)
			 */

		}
		return null;
	}
}
package cn.tude;

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

public class Bee extends FlyingObject {

	private int xspeed;// x移动速度
	private int yspeed;// y移动速度
	private int awardType;// 奖励类型

	public Bee() {
		super(60, 51);
		//
		Random rand = new Random();
		// x= rand.nextInt(400-width);
		// y=-height;
		xspeed = 1;
		yspeed = 2;
		awardType = rand.nextInt(2);
	}

	public void step() {
		x+=xspeed;
		y+=yspeed;
		if(x<=0||x>=World.WIDTH-width){
			xspeed*=-1;
		}
		
		
		
		
		
		
		
	}
	private int index=1;

	public BufferedImage getImage() {
		if (isLife()) {
			return Images.bees[0];

		} else if (isDead()) {//若死了的
			BufferedImage img=Images.bees[index++];//获取爆破图 
			if(index==Images.bees.length){
				state=REMOVE;
			}
			return img;

		}
		return null;
	}
}
package cn.tude;

import java.awt.image.BufferedImage;



public class BigAirplane extends FlyingObject {

	private int speed;// 移动速度

	public BigAirplane() {
		super(66, 89);

		speed = 2;
		
	}

	public void step() {
		y+=speed;
	}
	private int index=1;

	public BufferedImage getImage() {
		if (isLife()) {
			return Images.bairs[0];

		} else if (isDead()) {//若死了的
			BufferedImage img=Images.bairs[index++];//获取爆破图 
			if(index==Images.bairs.length){
				state=REMOVE;
			}
			return img;

		}
		return null;
	}
}
package cn.tude;

import java.awt.image.BufferedImage;


public class Bullet extends FlyingObject {
	// 子弹

	private int speed;// 移动速度

	public Bullet(int x, int y) {
		super(8, 20, x, y);

		speed = 3;

	}

	public void step() {
		y-=speed;
	}

	public BufferedImage getImage() {
		if (isLife()) {
			return Images.bullet;
		} else if (isDead()) {
			state = REMOVE;

		}
		return null;
		/*
		 * 若活着的:返回子弹图片 若死了的:将状态修改为REMOVE;同时不返回图片 若删除了的:不返回图片
		 * 
		 */
	}

}
package cn.tude;

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

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;
	protected int y;

	public FlyingObject(int width, int height) {// 专门给小蜜蜂,大敌机,小敌机提供的,以为三种飞行器的x,y都是不同的,所以要写活
		this.width = width;
		this.height = height;
		Random rand = new Random();
		x = rand.nextInt(World.WIDTH - width);
		y =-height;
	}

	public FlyingObject(int width, int height, int x, int y) {// 专门给英雄机,天空子弹提供的,以为三种飞行器的x,y都是不同的,所以要写活
		this.width = width;
		this.height = height;
		this.x = x;
		this.y = y;
	}

	// 重写

	public abstract void step();// {
	// System.out.println("飞行物移动了");
	// }

	// 获取对象的图片
	public abstract BufferedImage getImage();

	public boolean isLife() {
		return state == LIFE;

	}

	public boolean isDead() {
		return state == DEAD;
	}

	public boolean isRemove() {
		return state == REMOVE;
	}

}
package cn.tude;

import java.awt.image.BufferedImage;

public class Hero extends FlyingObject {
	// int width;
	// int height;
	// int x;
	// int y;
	private int life;
	private int fire;

	public Hero() {
		super(97, 139, 140, 400);
		life = 3;
		fire = 0;
	}

	public void step() {
	}

	// 移动
	// void step(){//步
	// System.out.println("英雄机切换图片了");
	// }
	private int index = 0;

	public BufferedImage getImage() {
		return Images.heros[index++ % Images.heros.length];
		/*
		 * 
		 * index=0 10M 返回heros[0] index=1 20M 返回 heros[1] index=2 30M 返回heros[0]
		 * index=3 40M 返回heros[1] index=4 50M 返回heros[0] index=5
		 * 
		 * 
		 * 
		 * 
		 * 
		 * 
		 * 
		 * 
		 * 
		 * 
		 * 
		 * 
		 * 
		 */
	}

	public Bullet[] shoot() {
		int xspeed = this.width / 4;
		int yspeed = 20;

		if (fire > 0) {// 双
			Bullet[] bs = new Bullet[2];
			bs[0] = new Bullet(this.x + 1 * xspeed, this.y - yspeed);
			bs[1] = new Bullet(this.x + 3 * xspeed, this.y - yspeed);
			fire -= 2;
			return bs;
		} else {// 单
			Bullet[] bs = new Bullet[1];
			bs[0] = new Bullet(this.x + 2 * xspeed - 5, this.y - yspeed);
			return bs;
		}
		
	}

}
package cn.tude;

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

public class Images {
	public static BufferedImage sky;
	public static BufferedImage bullet;
	public static BufferedImage[] heros;
	public static BufferedImage[] airs;
	public static BufferedImage[] bairs;
	public static BufferedImage[] bees;

	static {// 初始化静态资源
			// sky =读取BufferedImage。png图片;
		sky = readImage("background.png");
		bullet = readImage("bullet.png");
		heros = new BufferedImage[2];
		heros[0] = readImage("hero0.png");
		heros[1] = readImage("hero1.png");
		airs = new BufferedImage[5];
		bairs = new BufferedImage[5];
		bees = new BufferedImage[5];
		airs[0] = readImage("airplane.png");
		bairs[0] = readImage("bigairplane.png");
		bees[0] = readImage("bee.png");
		for (int i = 1; i < airs.length; i++) {
			airs[i] = readImage("bom" + i + ".png");
			bairs[i] = readImage("bom" + i + ".png");
			bees[i] = readImage("bom" + i + ".png");
		}

	}

	public static BufferedImage readImage(String fileName) {
		try {
			BufferedImage img = ImageIO.read(FlyingObject.class.getResource(fileName));// 读取同包中的
			return img;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
}
package cn.tude;

import java.awt.image.BufferedImage;


public class Sky extends FlyingObject{
	
	private int speed;//移动速度
	private int y1;//第2张图的y坐标
	/**构造方法*/
	public Sky(){
		super(World.WIDTH,World.HEIGHT,0,0);
		
		speed=1;
		y1=-World.HEIGHT;
		
	}public void step(){
		y+=speed;
		y1+=speed;
		if(y>=World.HEIGHT){
			y=-World.HEIGHT;
		}
		
		if(y1>=World.HEIGHT){
			y1=-World.HEIGHT;
		}
	}
	
	public BufferedImage getImage(){
		return Images.sky;
				
	}
	public int getY1(){
		return y1;
	}

}
package cn.tude;


import javax.swing.JFrame;
import javax.swing.JPanel;

import org.omg.Messaging.SyncScopeHelper;


import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.util.TimerTask;
import java.util.Timer;
import java.util.Random;
import java.util.Arrays;
public class World extends JPanel {
	public static final int WIDTH = 400;
	public static final int HEIGHT = 700;

	private Hero hero = new Hero();
	private Sky sky = new Sky();
	private FlyingObject[] enemies = {};// 0
	// Flyingobject[] enemies=new Flyingobject[0];//0
	private Bullet[] bullets = {};

	/** 创建敌人(小敌机,大敌机,小蜜蜂)对象 */
	public FlyingObject nextOne() {
		Random rand = new Random();
		int type = rand.nextInt(20);
		if (type < 5) {
			return new Bee();
		} else if (type < 14) {
			return new Airplane();
      
		} else {
			return new BigAirplane();
		}

	}

	private int enterIndex=0;
	public void enterAction(){
		enterIndex++;
		if(enterIndex%40==0){
			FlyingObject obj =nextOne();
			enemies=Arrays.copyOf(enemies,enemies.length+1);
			enemies[enemies.length-1]=obj;
 			
		}
		
	}
	private int shootIndex=0;
	public void shootAction(){
		
		shootIndex++;
		if(shootIndex%30==0){
			Bullet[] bs=hero.shoot();
			bullets=Arrays.copyOf(bullets,bullets.length+bs.length);
		System.arraycopy(bs,0,bullets,bullets.length-bs.length,bs.length);
		}
		
	}
	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();
			
		}
		
	}
	public void action() {
		Timer timer = new Timer();
		int intervel = 10;// 定时间隔等于(10毫秒走一次)

		timer.schedule(new TimerTask() {
			public void run() {// 定时干的事
				enterAction();
				shootAction();
				stepAction();
				repaint();
			}
		}, intervel, intervel);

	}

	public void paint(Graphics g) {

		g.drawImage(sky.getImage(), sky.x, sky.y, null);
		g.drawImage(sky.getImage(), sky.x, sky.getY1(), null);
		g.drawImage(hero.getImage(), hero.x, hero.y, null);
		for (int i = 0; i <enemies.length; i++) {
			FlyingObject f = enemies[i];
			g.drawImage(f.getImage(), f.x, f.y, null);

		}
		for (int i = 0; i < bullets.length; i++) {
			Bullet b = bullets[i];
			g.drawImage(b.getImage(), b.x, b.y, null);

		}
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		World world = new World();
		frame.add(world);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(WIDTH, HEIGHT);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);

		world.action();
		
		
	}

}

测试后视频:

https://download.csdn.net/download/Theshy08/12740605

 

本文地址:https://blog.csdn.net/Theshy08/article/details/108186144

《飞机大战-java基础小程序(初学项目)07.doc》

下载本文的Word格式文档,以方便收藏与打印。