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

Shoot射击游戏第三天:
1.设计小敌机数组、大敌机数组、小蜜蜂数组、子弹数组,并测试
2.设计FlyingObject超类,6个对象类继承超类
3.给FlyingObject设计两个构造方法,6个对象类分别调用

今天需要用到的知识点为继承和数组
1.引用类型数组:
  1)Student[] stus = new Student[3]; //创建Student数组对象
    stus[0] = new Student("zs",25,"LF"); //创建Student对象
    stus[1] = new Student("ls",26,"JMS");
    stus[2] = new Student("ww",28,"SD");
    System.out.println(stus[0].name); //输出第1个学生的名字
    stus[1].age = 24; //将第2个学生的年龄修改为24
    stus[2].sayHi(); //第3个学生跟大家问好
    for(int i=0;i<stus.length;i++){ //遍历stus数组
      System.out.println(stus[i].name); //输出每个学生的名字
      stus[i].sayHi(); //每个学生跟大家问好
    }
  2)Student[] stus = new Student[]{
      new Student("zs",25,"LF"),
      new Student("ls",26,"JMS"),
      new Student("ww",28,"SD")
    };
2.继承:
  1)作用:代码复用
  2)通过extends来实现继承
  3)超类/父类:派生类所共有的属性和行为
    派生类/子类:派生类所特有的属性和行为
  4)派生类继承超类后,派生类具有:派生类的+超类的
  5)一个超类可以有多个派生类
    一个派生类只能有一个超类----单一继承
  6)具有传递性
  7)java规定:构造派生类之前必须先构造超类
    7.1)若派生类的构造方法中没有调用超类的构造方法,
        ----则默认super()调超类的无参构造方法
    7.2)若派生类的构造方法中调用了超类的构造方法,
        ----则不再默认提供
    注意:super()调用超类构造必须位于派生类构造方法的第一行
3.super:指代当前对象的超类对象
  super的用法:
    1)super.成员变量名-----访问超类的成员变量
    2)super.方法名()-------调用超类的方法
    3)super()--------------调用超类的构造方法

 

软件中的继承:
  代码不用自己写,自己也能用

生活中的继承:
1)继承财产:
    钱不用自己挣,自己也能花
2)继承皇位:
    江山不用自己打,自己也能坐
3)继承工作:
    工作不用自己找,自己也能干

 
继承说简单一点就是儿子继承爹,儿子只能有一个爹,但是爹可以有多个儿子继承,然后引用数组可以理解为把学生1,学生2,学生3放到一个数组里面这样new数组就行了

(下面上代码,通过代码可以更好的理解)

 

在写代码前我们要创建一个爸爸类Flyingobject

 

 

 

 

 

这里我们创建了一个爸爸类儿子类通过继承爸爸类就可以用这样就可以少写好多代码,但是爸爸类里面自能写儿子共有的属性/行为,比如所以儿子都有宽,高,x,y,另外有的儿子需要速度有的需要y1这些就不能放在爸爸里面写这是儿子所特有的行为写在儿子类自己里面.

注意:我们还需要养成一个良好的习惯就是在写的时候要先写爸爸类在写儿子类.

 

 


 

package cn.tedu.shoot;

import java.util.Random;

public class Flyingobject {
    int x;
    int y;
    int width;
    int height;
    Flyingobject(int width,int height){
        this.width=width;
        this.height=height;
        Random rand=new Random(); 
        x=rand.nextInt(400-width);
        y=-height;
        
    }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("plan");
    }

}

这里解释一下为什么写了两个FlyingObject,这是因为上面的是给大敌机,小敌机,蜜蜂用的,他们的xy不确定需要随机生成,我们看飞机大战也可以了解到敌人的出现位置是不确定的,下面的是给英雄机,子弹,天空用的.

 

 


 


package cn.tedu.shoot;
public class Airplane  extends Flyingobject{//通过用extends关键字来实现继承
    int speed;
    Airplane(){
        super(48,52);    
               speed=2;
    }

}


 

 

 


 

package cn.tedu.shoot;

import java.util.Random;

public class Bee extends Flyingobject {
    int xspeed;
    int yspeed;
    int awardType;
    Bee(){
        super(23,52);
        Random rand=new Random();       
        xspeed=2;
        yspeed=4;
         awardType = rand.nextInt(36);
        
    }

}






 


 

package cn.tedu.shoot;

import java.util.Random;

public class BigAirplane extends Flyingobject{
    int speed;
    BigAirplane(){
        super(52,51);      
        speed=1;
        
        
    }

}


 

 

 

 


 


package cn.tedu.shoot;

public class Bullet extends Flyingobject{
    
    int speed;
    
    Bullet(int x,int y){
        super(20,52,x,y);
        speed=10;
    }
    

}




 

 


 


package cn.tedu.shoot;

public class Hero extends Flyingobject {
    int life;
    int fire;
    Hero (){
        
        super(97,139,140,100);
        life=3;
        fire=0;
        
    }

}




 

 


 


package cn.tedu.shoot;

public class Sky extends Flyingobject{
    int speed;
    int y1;
    Sky(){
        super(20,700,22,2);
        speed=10;
        y1=-700;
    }

}






 

 

 


 

package cn.tedu.shoot;

public class World {
    Hero h;
    BigAirplane[] big;
    Airplane[] as;
    Bee[] bee;
    Bullet[] bul;
    Sky s;
    void action(){
        as=new Airplane[3];
        as[0]=new Airplane();
        as[1]=new Airplane();
        as[2]=new Airplane();
        for(int i=0;i<as.length;i++){
            System.out.println(as[i].x+","+as[i].y);
            as[i].step();
        }
    }
    
public static void main(String[] arge){
        World w =new World();
        w.action();
}
}

 

大家可以发现这样的好处是不用单独在儿子类里面写行为了,直接在爸爸类里面继承就好了,大大减少了代码量

最后的输出结果就不给大家展示了,大家可以填写一些其他的数检验一下

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值