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

168 阅读4分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

(飞机大战源码我已经上传到博客大家可以免费下载)

(这里先卖一下关子如果有下载下来的同学会发现这个有好多的子弹和敌人,只需要修改一下数据就可以,这样也是为了能引起你们的兴趣,大家可以先尝试的修改一下并不是多难,如果没找到怎么该也没有关系,学到最后我相信你一定一眼就看出来是哪里的问题)

链接

/**

*今天给大家介绍一下java的一个小项目,这也是我接触的第一个java小项目,写给初学java的同学们,希望我们一起成长

*一起进步,初学java没有太好的办法就是多敲,单靠理解是没有用的,有那个时间不如多敲敲,量变引起质变用在java上面是

*非常好的实现,敲着敲着就有了感觉.,下面给大家放了一张我练习的图片,希望大家多加练习.一个项目反复练习好多次才会有感觉!

*/

Shoot射击游戏第一天:

首先要创建6个对象类并 创建World类测试.

需求: 1)所参与的角色: 英雄机、子弹、小敌机、大敌机、小蜜蜂、天空 2)角色间的关系: 2.1)英雄机发射子弹(单倍火力、双倍火力) 2.2)子弹射击敌人(小敌机、大敌机、小蜜蜂),若射上了: 2.2.1)子弹去死、敌人去死 2.2.2)若射上的是小敌机,则玩家得1分 若射上的是大敌机,则玩家得3分 若射上的是小蜜蜂,则英雄机得奖励(1条命或40火力值) 2.3)敌人(小敌机、大敌机、小蜜蜂)与英雄机撞,若撞上了: 2.3.1)敌人去死 2.3.2)英雄机减命,同时清空火力值 2.4)英雄机、子弹、小敌机、大敌机、小蜜蜂都在天空上飞 注:养成好的习惯命名时最好见名知意,在创建好项目后先创建好包然后在同一个包下面创建类. 包的命名规则一般用域名的反写,因为域名不会重复避免出现类的类名冲突,建议包名用小写字母 myshoot cn.shoot Hero Airplane BigAirplane Bee Bullet Sky

World

下面给大家呈现一下创建好的图和类里面的代码

package cn.tedu.shoot;//包名,大家如果复制代码的时候报错需要把这句话删掉

/*小敌机/

public class Airplane { int height; int width; int x; int y; int speed;//小敌机的移动速度 void step(){ System.out.println("小敌机y坐标向下"); }

}

package cn.tedu.shoot;

/**小蜜蜂(敌人飞机的一种,幽默一点叫他小蜜蜂)*/

public class Bee { int height; int width; int x; int y; int xspeed; int yspeed; int awardType; void step(){ System.out.println("小蜜蜂"); }

}

package cn.tedu.shoot;

/*大敌机/

public class BigAirplane { int width; int height; int x; int y; int speed; void stup(){ System.out.println("大敌机y坐标向下"); }

}

package cn.tedu.shoot;

/*子弹/

public class Bullet { int width; int height; int x; int y; int speet; void step(){ System.out.println("子弹往上飞喽"); }

}

package cn.tedu.shoot;

/**英雄机(自己)*/

public class Hero { int height;//高 int width;//宽 int x; int y; int life;//命 int fire;//火力 //飞机的移动 void step(){ System.out.println("英雄机的移动"); }

}

package cn.tedu.shoot;

/*天空/

public class Sky { int width; int height; int x; int y; int speed;//移动速度 int y1;//第2张图的y坐标 void step(){ System.out.println("天空y坐标和y1坐标同时移动"); }

}

package cn.tedu.shoot;

/*窗口/

public class Worid { Hero h; Sky s; Airplane a1; Airplane a2; BigAirplane bi1; BigAirplane bi2; Bee b1; Bee b2; Bullet bu1; Bullet bu2;

    void action(){
        h=new Hero();
        h.width=58;
        h.height=63;
        h.life=3;
        h.x=52;
        h.y=63;
        h.fire=0;
        h.step();
        
        s=new Sky();
        s.height=10;
        s.width=20;
        s.x=10;
        s.y=10;
        s.y1=90;
        s.speed=87;
        s.step();
        
        a1=new Airplane();
        a1.width=10;
        a1.height=92;
        a1.x=23;
        a1.y=2;
        a1.speed=8;
        a1.step();
        
        a2=new Airplane();
        a2.width=23;
        a2.height=289;
        a2.x=2;
        a2.y=2;
        a2.speed=2;
        a2.step();
        
        b1=new Bee();
        b1.height=8;
        b1.width=2;
        b1.x=34;
        b1.y=44;
        b1.xspeed=23;
        b1.yspeed=234;
        b1.awardType=3;
        b1.step();
        
        b2=new Bee();
        b2.height=12;
        b2.width=32;
        b2.x=32;
        b2.y=23;
        b2.xspeed=12;
        b2.yspeed=352;
        b2.awardType=99;
        b2.step();

        bu1=new Bullet();
        bu1.height=56;
        bu1.width=34;
        bu1.x=2;
        bu1.y=34;
        bu1.speet=50;
        bu1.step();
            
    }
public static void main(String[] args) {
        Worid a=new Worid();
        a.action();
        
}

}

如果你得到的也是如上窗口就证明你成功了

第一天代码比较简单如果想学好的话就敲5遍

5遍是基础所有代码至少5遍

这里给大家讲解一下World里面写了什么东西有的同学不明白

void action(){ h=new Hero();

}

这是创建了一个Hero的对象

类 对象 月饼模子 月饼

生活中的对象----生活中真实存在的东西 软件中的对象----软件中真实存在的东西

简单来说 对象代表一个单一的东西/个体,类代表一个类型

例如: 对象:一个英雄机、一个天空、一堆子弹 一堆小敌机、一堆大敌机、一堆小蜜蜂

类: Hero英雄机类、Sky天空类、Bullet子弹类 Airplane小敌机类、BigAirplane大敌机类、Bee小蜜蜂类

注:一个java程序中只能有一个public修饰的类;类最好一个java程序中只写一个,这样方便我们修改(实在不懂就记住,我们老师也是这么教的,这是规定!!!)

基本类型、数组类型-------------------java给大家提供的 当我们觉得java给大家的类型不够时-----咱们可能自己造数据类型

类是一种引用数据类型,是咱们自己造的一个数据类型

class Hero{ //Hero是我自己造的一个数据类型 } class Sky{ } class Bullet{ } class Airplane{ } class BigAirplane{ } class Bee{ } (多敲明天见)