【项目】某内Java巩固基础项目⑨(飞机大战)一

前言

半个月学习完Java基础课程后,飞机大战是第一阶段项目,主要为了巩固Java基础学习的知识

目录结构

请添加图片描述

创建6个对象,并创建world类进行测试

  • 小蜜蜂
/** 小蜜蜂 */
public class Bee {
    
    
    int width;
    int height;
    int x;
    int y;
    int xSpeed; //x坐标移动速度
    int ySpeed; //y坐标移动速度
    int awardType; //奖励类型(0或1)
    /** 构造方法 */
    Bee(){
    
    
        width = 60;
        height = 51;
        Random rand = new Random();
        x = rand.nextInt(400-this.width); //x:0到(400-小蜜蜂宽)之间的随机数
        y = -this.height; //y:负的小蜜蜂的高
        xSpeed = 1;
        ySpeed = 2;
        awardType = rand.nextInt(2); //0或1
    }

    /** 小蜜蜂移动 */
    void step() {
    
    
        System.out.println("小蜜蜂的x坐标移动了:"+xSpeed+",y坐标移动了:"+ySpeed);
    }
}
  • 小敌机
/**
 * 小敌机
 */
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-this.width); //x:0到(400-小敌机宽)之间的随机数
        y = -this.height; //y:负的小敌机的高
        speed = 2;
    }

    /** 小敌机移动 */
    void step() {
    
    
        System.out.println("小敌机的y坐标向下移动了:"+speed);
    }
    
}
  • 大敌机
/** 大敌机 */
public class BigAirplane {
    
    
    int width;
    int height;
    int x;
    int y;
    int speed; //移动速度
    /** 构造方法 */
    BigAirplane(){
    
    
        width = 66;
        height = 89;
        Random rand = new Random();
        x = rand.nextInt(400-this.width); //x:0到(400-大敌机宽)之间的随机数
        y = -this.height; //y:负的大敌机的高
        speed = 2;
    }

    /** 大敌机移动 */
    void step() {
    
    
        System.out.println("大敌机的y坐标向下移动了:"+speed);
    }
}
  • 天空
/** 天空 */
public class Sky {
    
    
    int width;
    int height;
    int x;
    int y;
    int speed; //移动速度
    int y1;    //第2张图的y坐标
    /** 构造方法 */
    Sky(){
    
    
        width = 400;
        height = 700;
        x = 0;
        y = 0;
        speed = 1;
        y1 = -700;
    }

    /** 天空移动 */
    void step() {
    
    
        System.out.println("天空的y坐标和y1坐标向下移动了:"+speed);
    }
}
  • 子弹
/** 子弹 */
public class Bullet {
    
    
    int width;
    int height;
    int x;
    int y;
    int speed; //移动速度
    /** 构造方法 */
    Bullet(int x,int y){
    
    
        width = 8;
        height = 20;
        this.x = x;
        this.y = y;
        speed = 3;
    }

    /** 子弹移动 */
    void step() {
    
    
        System.out.println("子弹的y坐标向上移动了:"+speed);
    }
}
  • 英雄机
/** 英雄机 */
public class Hero {
    
    
    int width;
    int height;
    int x;
    int y;
    int life; //命数
    int doubleFire; //火力值
    /** 构造方法 */
    Hero(){
    
    
        width = 97;
        height = 139;
        x = 140;
        y = 400;
        life = 3;
        doubleFire = 0;
    }

    /** 英雄机随着鼠标移动 x/y:鼠标的x坐标/y坐标 */
    void moveTo(int x,int y) {
    
    
        System.out.println("英雄机随着鼠标移动啦!");
    }
    /** 英雄机移动 */
    void step() {
    
    
        System.out.println("英雄机切换图片啦!");
    }

}
  • 窗口
/**
 * 整个窗口
 */
public class World {
    
    
    Sky sky;
    Hero hero;
    Airplane[] as;
    BigAirplane[] bas;
    Bee[] bs;
    Bullet[] bts;

    void action() {
    
     //测试代码
        as = new Airplane[4];
        as[0] = new Airplane();
        as[1] = new Airplane();
        as[2] = new Airplane();
        as[3] = new Airplane();
        for (int i = 0; i < as.length; i++) {
    
     //遍历所有小敌机
            Airplane a = as[i]; //获取每个小敌机
            System.out.println(a.x + "," + a.y);
            a.step();
        }

    }

    public static void main(String[] args) {
    
    
        World world = new World();
        world.action();
    }
}

认识构造方法、变量、数组、方法的调用、this关键字

Guess you like

Origin blog.csdn.net/weixin_45511500/article/details/120310419