這是飛機大戰遊戲的基礎工具類代碼及解釋,其他的也在博客中
基本類有三類,統一放在包com.airbattle.gameobject下
類 | 用途 |
Aircraft | 飛行器,遊戲中所有的飛行物(英雄機和敵機)都繼承該類 |
Bullet |
子彈類,主要是子彈的構造和移動 |
HostilePlane | 敵軍飛機,繼承了飛行器類 |
這些類也被寫殘了,因爲我的水平太差,只對後面的一些繼承有約束作用,其他意義不大
Aircraft類
package com.airbattle.gameobject;
import com.airbattle.gameinterface.FlyingObjectInterface;
import com.airbattle.gameproperty.Image;
import com.airbattle.gameproperty.Position;
/**
*
* @author William
* 所有飛行器的父類
* 英雄機和敵機都繼承這個類
*
*/
public abstract class Aircraft implements FlyingObjectInterface{
//飛行器的位置
public Position pos = new Position(0,0);
public Aircraft(Position pos) {
this.pos = pos;
}
public Aircraft() {
}
}
Bullet類
package com.airbattle.gameobject;
import com.airbattle.gameinterface.GameObjectInterface;
import com.airbattle.gameproperty.Image;
import com.airbattle.gameproperty.Position;
public class Bullet implements GameObjectInterface{
int damage; //子彈的傷害,沒用
public Position pos; //子彈的位置
static {
//System.out.println("子彈的圖像文件已經被加載");
}
public Bullet(int damage, Position pos) {
if (damage <= 0)
damage = 1;
this.damage = damage;
this.pos = pos;
//System.out.println("子彈的傷害已設置");
}
//子彈移動函數
public void moveStep(int dir) {
this.pos.x += dir;
}
public void moveStep() {
}
}
HostilePlane類
package com.airbattle.gameobject;
import com.airbattle.game.Property;
import com.airbattle.gameinterface.FlyingObjectInterface;
import com.airbattle.gameinterface.GameObjectInterface;
import com.airbattle.gameproperty.Image;
import com.airbattle.gameproperty.Position;
import com.airbattle.gameproperty.Rect;
public class HostilePlane extends Aircraft implements FlyingObjectInterface, GameObjectInterface{
public HostilePlane(Position pos) {
super(pos);
}
public Bullet fire() {
return null;
}
//獲取敵方飛機的矩形框
public Rect getRect() {
return new Rect(this.pos.x, this.pos.y, Property.ENEMY_IMAGE_HEIGHT, Property.ENEMY_IMAGE_WIDTH);
}
public void moveStep() {
}
public void onHit(int numHit) {
}
}