栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

目录结构

复习前面内容,运用到项目中 ,一到四天,基本都是一些简单的,后面稍微复杂一些,涉及线程、鼠标监听、等等

  • 设计小敌机数组、大敌机数组、小蜜蜂数组、子弹数组,并测试
  • 设计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;
	}
	
	
	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 
  • 飞行物父类

将飞行物共有的特性(例如:高,宽,坐标),进行封装,子类继承并重写父类方法

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关键字

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/349191.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号