JAVA基础:面向对象之飞机大战(二)

Day02

飞机大战需求:

  1. 所参与的角色:英雄机、子弹、小敌机、大敌机、小蜜蜂、天空

  2. 角色间的关系:

    • 英雄机发射子弹(单倍火力、双倍火力)

    • 子弹打敌人(小敌机、大敌机、小蜜蜂),若击中了:

      • 子弹直接消失、敌人先爆破再消失

      • 若击中的是小敌机,则玩家得1分

        若击中的是大敌机,则玩家得3分

        若击中的是小蜜蜂,则英雄机得奖励(1条命,或,40火力值)

    • 敌人(小敌机、大敌机、小蜜蜂)撞英雄机,若撞上了:

      • 敌人先爆破再消失
      • 英雄机减1条命,同时清空火力值----当命数为0时,游戏结束
    • 英雄机、子弹、小敌机、大敌机、小蜜蜂,都在天空中飞

1. 给天空类、英雄机类、小敌机类、大敌机类、小蜜蜂类、子弹类添加构造方法
### 英雄机类:

public class Hero {
    int width;
    int height;
    int x;
    int y;
    int life;
    int fire;

    /**
     * 构造方法
     */
    Hero(){
        width = 97;     //飞机图片宽 PX
        height = 139;   //飞机图片高 PX
        x = 140;
        y = 400;
        life = 3;
        fire = 0;
    }
    void step(){
        System.out.println("英雄机");
    }
}


### 小敌机类
import java.util.Random;

public class Airplane {
    int width;  //宽
    int height; //高
    int x;      //坐标
    int y;      //坐标
    int speed;  //移动速度

    /**
     * 构造方法
     */
    Airplane() {
        width = 48;
        height = 50;
        Random rand = new Random(); //随机数对象
        x = rand.nextInt(400 - width + 1);//x:0到(窗口宽-小敌机宽)之间的距离
        y = -height;//Y:小飞机首次出现在世界图片的外面(之后会调用step方法移动进入世界)
        speed = 2;
    }

    void step() {
        System.out.println("小敌机");
    }
}


### 小蜜蜂类
import java.util.Random;

public class Bee {

    int width;
    int height;
    int x;
    int y;
    int xSpeed;     //x坐标移动速度
    int ySpeed;     //y坐标移动速度
    int awardType;  //奖励类型

    Bee() {
        width = 60;
        height = 51;
        Random random = new Random();
        x = random.nextInt(400 - width + 1); //x:0到(窗口宽-小蜜蜂宽)之内的随机数
        y = -height; //y:负的小蜜蜂的高
        xSpeed = 1;
        ySpeed = 2;
        awardType = random.nextInt(2); //0到1之间随机生成
    }

    void step() {
        System.out.println("x左右移动,Y向下移动");
    }

}

### 子弹类
public class Bullet {

    int width;
    int height;
    int x;
    int y;
    int speed;

    /**
     * 构造方法
     *
     * @param x 子弹可以有多个,子弹的初始坐标要依赖于当前英雄机的坐标位置
     * @param y 子弹可以有多个,子弹的初始坐标要依赖于当前英雄机的坐标位置
     */
    Bullet(int x, int y) {
        width = 8;
        height = 20;
        this.x = x;
        this.y = y;
        speed = 3;
    }

    void step() {
        System.out.println("子弹向上移动");
    }
}

### 天空类
public class Sky {
    int width;
    int height;
    int x;
    int y;
    int speed;
    int y1;         //第二张图片的y坐标


    /**
     * 构造方法
     */
    Sky() {
        width = 400;
        height = 700;
        x = 0;
        y = 0;
        speed = 1;
        y1 = -700;
    }

    void step() {
        System.out.println("天空的Y和Y1同时向下移动");
    }
}



2. 在main中:创建1个天空对象、1个英雄机对象、至少4个小敌机对象、2个大敌机对象、2个小蜜蜂对象、2个子弹对象,测试
### 世界类
public class World {

    /**
     * 整个游戏窗口
     */

    public static void main(String[] args) {
        Sky s = new Sky();
        Hero h = new Hero();
        Airplane a1 = new Airplane();
        Airplane a2 = new Airplane();
        Airplane a3 = new Airplane();
        Airplane a4 = new Airplane();
        BigAirplane ba1 = new BigAirplane();
        BigAirplane ba2 = new BigAirplane();
        Bee b1 = new Bee();
        Bee b2 = new Bee();
        Bullet bt1 = new Bullet(100, 200);
        Bullet bt2 = new Bullet(50, 300);
        System.out.println(h.width + "," + h.height + "," + h.x + "," + h.y + "," + h.life + "," + h.fire);
        System.out.println(a1.width + "," + a1.height + "," + a1.x + "," + a1.y + "," + a1.speed);
        System.out.println(a2.width + "," + a2.height + "," + a2.x + "," + a2.y + "," + a2.speed);
        System.out.println(a3.width + "," + a3.height + "," + a3.x + "," + a3.y + "," + a3.speed);
        System.out.println(a4.width + "," + a4.height + "," + a4.x + "," + a4.y + "," + a4.speed);

    }
}

笔记

  1. 构造方法:构造函数、构造器、构建器------------------复用给成员变量赋初始值的代码

    • 作用:给成员变量赋初始值
    • 语法:与类同名,没有返回值类型(连void都没有)
    • 调用:在创建(new)对象时被自动调用
    • 若自己不写构造方法,则编译器默认提供一个无参构造方法,若自己写了构造方法,则不再默认提供
    • 构造方法可以重载
  2. this:指代当前对象,哪个对象调用方法它指的就是哪个对象

    ​ 只能用在方法中,方法中访问成员变量之前默认有个this.

    this的用法:

    • this.成员变量名--------------------------访问成员变量

      注意:当成员变量与局部变量同名时,若想访问成员变量,则this不能省略

    • this.方法名()-------------------------------调用方法(一般不用,了解即可)

    • this()-----------------------------------------调用构造方法(一般不用,了解即可)

  3. null:表示空,没有指向对象。

    注意:若引用的值为null,则该引用不能进行任何点操作了,若操作则发生NullPointerException空指针异常

  4. 引用类型数组(上):------记住与基本类型数组的2点区别即可

    • 区别1:给引用类型数组的元素赋值,需要new个对象

    • 区别2:若想访问数组元素的数据,需要通过数组元素去打点

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值