shoot射击游戏项目二
1.需求分析
A:设计一个父类、超类;并且让6个对象继承超类,测试
B:给超类设计构造方法;让6个对象分别调用超类;
C:设置数组,进行测试
D:在6个子类、派生类中重写
E:画窗体;
2.技术分析
重写和重载(面试笔试题)
重写:(Override)
发生在父子类中,方法名相同、参数类表相同、方法体不同。
遵循“运行期绑定”,看对象类型来调用方法;
重载:(overlord)
发生在一个类中,方法名相同,参数类表不相同、方法体不同。
遵循“编译期的绑定”,看参数、引用列表
3.代码实现
英雄机
public class Hero extends FlyingObject{
int life;//生命值
int doubleFile;//火力
/**
* 构造方法
*/
public Hero() {
super(97,124,140,400);
life = 5;
doubleFile = 0;//单倍火力
}
/**
* 英雄级移动
*/
void moveTo() {
System.out.println("英雄级移动");
}
}
小敌机
public class Airplane extends FlyingObject {
int speed;// 速度
/**构造方法 */
public Airplane() {
super(49, 36);
this.speed = 3;
}
/** 移动 */
void step() {
System.out.println("小敌机移动了!"+speed);
}
}
大敌机
public class Bigplane extends FlyingObject{
int speed;// 速度
/**
* 构造函数
*/
public Bigplane() {
super(49, 36);
this.speed = 2;
}
}
蜜蜂
public class Bee extends FlyingObject{
int xspeed;// x轴速度
int yspeed;// Y轴速度
int awardType;//获取奖励
public Bee() {
super(29, 36);
this.xspeed = 1;
this.yspeed = 2;
Random random= new Random();
awardType = random.nextInt(2);
}
/**
* 移动
*/
void step() {
System.out.println("蜜蜂移动了!"+xspeed);
}
}
背景
public class Sky extends FlyingObject{
int speed;// 速度
/**
* 构造方法
*/
public Sky() {
super(400, 700,0,0);
this.speed = 1;
}
void step() {
System.out.println("天空移动");
}
}
子弹
public class Bullet extends FlyingObject{
int speed;// 速度
public Bullet(int x ,int y) {
super(8,4,x,y);
this.speed = 2;
}
/**
* 子弹移动
*/
void step() {
System.out.println("子弹移动");
}
}
父类:飞行物
public class FlyingObject {
//成员变量
int width;//宽
int height;//高
int x;//x轴
int y;//y轴
/**
* 提供敌人(小敌机、大敌机、蜜蜂)
*/
public FlyingObject(int width,int height) {
this.width = width;
this.height = height;
Random random= new Random();
this.x = random.nextInt(400-this.height);
this.y = -this.height;
}
/**
* 提供英雄机、子弹、天空的构造
*/
public 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("飞行物移动");
}
}
主类
public class ShootMain extends JPanel{
//创建对象,进行方法调用,移动测试。
Sky sky = new Sky();
FlyingObject[] flyingObject = new FlyingObject[0];
/**
* 测试方法
*/
public void action() {
sky.step();
flyingObject = new FlyingObject[5];
flyingObject[0] = new Airplane();
flyingObject[1] = new Hero();
flyingObject[2] = new Bigplane();
flyingObject[3] = new Bee();
flyingObject[4] = new Sky();
for (int i = 0; i < flyingObject.length; i++) {
flyingObject[i].step();
}
}
//花对象
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.blue);
g.drawOval(150, 150,100, 100);
g.drawOval(250, 150,100, 100);
g.drawOval(350, 150,100, 100);
g.drawOval(400, 150,100, 100);
g.drawImage(sky.loadImage("background.png"),100,150,null);
for (int i = 0; i < flyingObject.length; i++) {
flyingObject[i]
}
}
/**
* 主函数,程序入口
*/
public static void main(String[] args) {
ShootMain shootMain = new ShootMain();
shootMain.action();
//窗体
JFrame jFrame= new JFrame();
jFrame.add(shootMain);
jFrame.setSize(500,700);
jFrame.setTitle("Shoot");
jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE);
jFrame.setLocationRelativeTo(null);//居中显示
jFrame.setVisible(true);
shootMain.action();
}
}