当前位置: 移动技术网 > IT编程>开发语言>Java > 飞机大战-java基础小程序(初学项目)06

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

2020年08月01日  | 移动技术网IT编程  | 我要评论
Shoot射击游戏第六天:1.设计窗口的宽和高为常量,适当地方做修改2.画对象:1)想画对象一定要获取对象的图片,每个对象都能获取图片, 意味着获取图片行为为共有行为,所以设计在FlyingObject超类中, 每个对象获取图片的行为都是不一样的,所以设计为抽象方法 ----在FlyingObject中设计抽象方法getImage()获取对象的图片2)获取图片时需要考虑对象的状态,因为在不同状态下所获取的图片是不同的, 每个对象都有状态,意味着状态为共有属性,所以设计在FlyingObjec

Shoot射击游戏第六天:

1.设计窗口的宽和高为常量,适当地方做修改

2.画对象:

1)想画对象一定要获取对象的图片,每个对象都能获取图片, 意味着获取图片行为为共有行为,所以设计在FlyingObject超类中, 每个对象获取图片的行为都是不一样的,所以设计为抽象方法 ----在FlyingObject中设计抽象方法getImage()获取对象的图片

2)获取图片时需要考虑对象的状态,因为在不同状态下所获取的图片是不同的, 每个对象都有状态,意味着状态为共有属性,所以设计在FlyingObject超类中, 状态一般都设计为常量,同时设计state变量表示当前状态 ----在FlyingObject中设计LIFE、DEAD、REMOVE常量,state变量 在获取图片时还需要判断对象的状态,每个对象都得判断状态, 意味着判断状态行为为共有行为,所以设计在FlyingObject超类中, 每个对象判断状态的行为都是一样的,所以设计为普通方法 ----在FlyingObject中设计isLife()、isDead()、isRemove()判断状态

3)重写getImage()获取对象的图片:

3.1)天空Sky,直接返回sky图片即可

3.2)英雄机Hero,直接返回heros[0]和heros[1]来回切换

3.3)子弹Bullet:

3.3.1)若活着的,直接返回bullet图片即可

3.3.2)若死了的,直接删除(不返回图片)

3.4)小敌机Airplane:

3.4.1)若活着的,直接返回airs[0]图片即可

3.4.2)若死了的,返回airs[1]到airs[4]的爆破图,airs[4]后删除

3.5)大敌机BigAirplane:

3.5.1)若活着的,直接返回bairs[0]图片即可

3.5.2)若死了的,返回bairs[1]到bairs[4]的爆破图,bairs[4]后删除

3.6)小蜜蜂Bee: 3.6.1)若活着的,直接返回bees[0]图片即可

3.6.2)若死了的,返回bees[1]到bees[4]的爆破图,bees[4]后删除

4)图片有了就可以开画了 ----在World类中重写paint()方法

知识点:

1.static final常量:

1)必须声明同时初始化

2)通过类名点来访问,不能被改变

3)建议:常量名所有字母都大写,多个单词用_分隔

4)编译器在编译时会将常量直接替换为具体的值,效率高

5)何时用:数据永远不变,并且经常使用

 

 

 

 

 

 

 

 

 

 

 

 

2.抽象方法:

1)由abstract修饰

2)只有方法的定义,没有具体的实现(连大括号都没有)

3.抽象类:

1)由abstract修饰

2)包含抽象方法的类必须是抽象类

3)抽象类不能被实例化(实例化就是new对象)

4)抽象类是需要被继承的,派生类:

4.1)重写所有抽象方法--------变不完整为完整

4.2)也声明为抽象类----------一般不这么用

 

 

 

 

 

 

 

 

 

 

 

 

 

5)抽象类的意义:

5.1)封装共有的属性和行为------------代码复用

5.2)为所有派生类提供统一的类型------向上造型

5.3)可以包含抽象方法,为派生类提供统一的入口(能点出来), 派生类的具体实现不同,但入口的是一致的

问:抽象方法有意义吗?是否是可有可无的呢?

答:抽象方法的意义在于: 当向上造型时,通过超类引用能点出来那个共有的方法, 但具体执行的还是派生类重写之后的

问:将方法设计为普通方法也能点出来,为什么要设计为抽象方法呢?

答:若设计为普通方法,则派生类可以重写也可以不重写 但设计为抽象方法,则可以强制派生类必须重写---一种强制的效果

 

 

 

 

 

 

 

 

 

 

 

这里实例化的意思就是不可以被new

 

代码:

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() {
		System.out.println("小敌机向下飞了");
	}

	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;
			

		}
		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(20, 50);
		//
		Random rand = new Random();
		// x= rand.nextInt(400-width);
		// y=-height;
		xspeed = 1;
		yspeed = 2;
		awardType = rand.nextInt(2);
	}

	public void step() {
		System.out.println("小蜜蜂下来了");
	}

	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;

//import java.util.Random;

public class BigAirplane extends FlyingObject {

	private int speed;// 移动速度

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

		// Random rand=new Random();
		// x= rand.nextInt(400-width);
		// y=-height;
		speed = 2;
	}

	public void step() {
		System.out.println("大敌机下来了");
	}

	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() {
		System.out.println("子弹往上飞了");
	}

	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;//注意这里应该写-height我们为了显示出来创建出来的敌人所以写成正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(139, 97, 140, 400);
		life = 3;
		fire = 0;
	}

	public void step() {
		System.out.println("英雄机出击了");
	}

	// 移动
	// 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
		 * 
		 * 
		 * 
		 * 
		 * 
		 * 
		 * 
		 * 
		 * 
		 * 
		 * 
		 * 
		 * 
		 */

	}
}
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[0] = 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() {
		System.out.println("天空出场");
	}

	public BufferedImage getImage() {
		return Images.sky;

	}

	public int getY1() {
		return y1;
	}

}
package cn.tude;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.image.BufferedImage;
import java.awt.Graphics;

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 = { new Airplane(), new BigAirplane(), new Bee() };
																						
	private Bullet[] bullet = { new Bullet(100, 200) };

	public void action() {

	}

	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 < bullet.length; i++) {
			Bullet b = bullet[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();
	}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(老规矩5遍,写不完明天继续写一定要有个大体映像)

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

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网