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

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)可以包含抽象方法,为派生类提供统一的入口(能点出来), 派生类的具体实现不同,但入口的是一致的

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

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

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

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

 

 

 

代码:

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遍,写不完明天继续写一定要有个大体映像)

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、付费专栏及课程。

余额充值