目录结构
复习前面内容,运用到项目中 ,一到四天,基本都是一些简单的,后面稍微复杂一些,涉及线程、鼠标监听、等等
二
- 设计小敌机数组、大敌机数组、小蜜蜂数组、子弹数组,并测试
- 设计FlyingObject超类,6个对象类继承超类
- 给超类FlyingObject设计两个构造方法,6个派生类分别调用
编程
- 小蜜蜂
/** 小蜜蜂:是飞行物 */
public class Bee extends FlyingObject{
int xSpeed; //x坐标移动速度
int ySpeed; //y坐标移动速度
int awardType; //奖励类型(0或1)
/** 构造方法 */
Bee(){
super(60,51);
xSpeed = 1;
ySpeed = 2;
Random rand = new Random();
awardType = rand.nextInt(2); //0或1
}
}
- 小敌机
/** 小敌机:是飞行物 */
public class Airplane extends FlyingObject {
int speed; //移动速度
/** 构造方法 */
Airplane(){
super(48,50);
speed = 2;
}
}
- 大敌机
/** 大敌机:是飞行物 */
public class BigAirplane extends FlyingObject{
int speed; //移动速度
/** 构造方法 */
BigAirplane(){
super(66,89);
speed = 2;
}
}
- 天空
/** 天空:是飞行物 */
public class Sky extends FlyingObject {
int speed; //移动速度
int y1; //第2张图的y坐标
/** 构造方法 */
Sky(){
super(400,700,0,0);
speed = 1;
y1 = -700;
}
}
- 子弹
/** 子弹:是飞行物 */
public class Bullet extends FlyingObject{
int speed; //移动速度
/** 构造方法 */
Bullet(int x,int y){
super(8,20,x,y);
speed = 3;
}
}
- 英雄机
/** 英雄机 */
public class Hero extends FlyingObject {
int life; //命数
int doubleFire; //火力值
/** 构造方法 */
Hero(){
super(97,139,140,400);
life = 3;
doubleFire = 0;
}
/** 英雄机随着鼠标移动 x/y:鼠标的x坐标/y坐标 */
void moveTo(int x,int y) {
System.out.println("英雄机随着鼠标移动啦!");
}
}
- 窗口
/** 整个窗口 */
public class World {
Sky sky = new Sky(); //天空
Hero hero = new Hero(); //英雄机
FlyingObject[] enemies = {
}; //敌人(小敌机、大敌机、小蜜蜂)数组
Bullet[] bullets = {
}; //子弹数组
void action() {
//测试代码
enemies = new FlyingObject[5];
enemies[0] = new Airplane();
enemies[1] = new Airplane();
enemies[2] = new BigAirplane();
enemies[3] = new BigAirplane();
enemies[4] = new Bee();
for(int i=0;i<enemies.length;i++) {
//遍历所有敌人
FlyingObject f = enemies[i]; //获取每一个敌人
System.out.println(f.x+","+f.y);
f.step();
//f被子弹射击
//f和英雄机撞
}
}
public static void main(String[] args) {
World world = new World();
world.action();
}
}
- 飞行物父类
将飞行物共有的特性(例如:高,宽,坐标),进行封装,子类继承并重写父类方法
/** 飞行物 */
public class FlyingObject {
int width; //宽
int height; //高
int x; //x坐标
int y; //y坐标
/** 专门给小敌机、大敌机、小蜜蜂提供的 */
FlyingObject(int width,int height){
this.width = width;
this.height = height;
Random rand = new Random();
x = rand.nextInt(400-this.width); //x:0到(400-敌人宽)之间的随机数
y = -this.height; //y:负的敌人的高
}
/** 专门给英雄机、天空、子弹提供的 */
FlyingObject(int width,int height,int x,int y){
this.width = width;
this.height = height;
this.x = x;
this.y = y;
}
/** 飞行物移动 */
void step() {
System.out.println("飞行物移动啦!");
}
}
继承、封装、多态、super关键字