⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tank.java

📁 J2ME超级坦克大战源码
💻 JAVA
字号:

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;


public abstract class Tank extends Sprite implements Actor{
    
    /**
     * Tank should know about the battle field.
     */
    protected static BattleField battleField;
    
    /**
     * Tank should know about the layer manager.
     */
    protected static LayerManager layerManager;
    
    /**
     * the speed of the tank, default speed is 6 ,half of the
     * width of each tile.
     */
    protected int speed=DEFAULT_SPEED;
    
    /**
     * The direction in which player is driving.
     */
    protected int direction = BattleField.NONE;
    
    /**
     * default speed for tank.
     */
    protected static final int DEFAULT_SPEED=ResourceManager.TILE_WIDTH/4;
    
    /**
     * maximun number of tanks in the battle field.
     */
    protected static final int POOL_SIZE = 21;
    
    /**
     * This pool store all tanks include player and enemy tanks.
     */
    protected static Tank TANK_POOL[];
    
    /**
     * time monitored to avoid the tank move to fast.
     */
    protected long driveStartTime = 0;
    
    /**
     * minimum time period between each move
     */
    private static final long minimumDrivePeriod = 40; 
    
    /**
     * Should the tank shoot?
     */
    protected boolean shoot = false;
    
    /**
     * new boun timer
     */
    protected int newBornTimer=0;
    
    /**
     * initial the tank pool ,the game will resume the tank object.
     * the first tank is the player's tank.
     */
    static {
        TANK_POOL = new Tank[POOL_SIZE];
        TANK_POOL[0] = new PlayerTank();
        //max 7 simple tanks
        for(int i=1;i<8;i++){
            TANK_POOL[i] = new SimpleTank(false);
        }
        //max 4 fast tanks
        for(int i=8;i<12;i++){
            TANK_POOL[i] = new FastTank(false);
        }
        //max 4 smart tanks
        for(int i=12;i<16;i++){
            TANK_POOL[i] = new SmartTank(false);
        }
        //max 4 heavy tanks
        for(int i=16;i<21;i++){
            TANK_POOL[i] = new HeavyTank(false);
        }
        
    }
    
    
   
    protected Tank(Image image,int frameWidth,int frameHeight){
        super(image,frameWidth,frameHeight);
        defineReferencePixel(frameWidth / 2, frameHeight / 2);
    }
    
    
    public void drive(){
        long tickTime = System.currentTimeMillis();
        boolean canDrive=(tickTime-driveStartTime)>minimumDrivePeriod;
        boolean onSnow=battleField.isOnSnow(getX(),getY());
        int extraPace=0;
        if(onSnow) extraPace=speed;
        if(canDrive){
            switch (direction) {
                case BattleField.NORTH:
                    if ((getY() > 0)&&
                            !battleField.containsImpassableArea
                            (getX(),getY() - speed, getWidth(), speed)) {
                        tryMove(0, -speed-extraPace);
                    }
                    break;
                case BattleField.EAST:
                    if ((getX() < battleField.getWidth() - getWidth()) &&
                            !battleField.containsImpassableArea
                            (getX() + getWidth(), getY(), speed, getHeight())) {
                        tryMove(speed+extraPace, 0);
                    }
                    break;
                case BattleField.SOUTH:
                    if ((getY() < battleField.getHeight() - getHeight())&&
                            !battleField.containsImpassableArea
                            (getX(),getY() + getHeight(), getWidth(), speed)) {
                        tryMove(0, speed+extraPace);
                    }
                    break;
                case BattleField.WEST:
                    if ((getX() > 0)&&
                            !battleField.containsImpassableArea
                            (getX() - speed,getY(), speed, getHeight())) {
                        tryMove(-speed-extraPace, 0);
                    }
                    break;
            }
            driveStartTime = tickTime;
        }
        
    }
    
    
    
    public static PlayerTank getPlayerTank(){
        return (PlayerTank)TANK_POOL[0];
    }
    
    
    public static void setLayerManager(LayerManager manager) {
        layerManager = manager;
        if (layerManager != null){
            
            for (int i = 0; i < POOL_SIZE; i++)
                layerManager.append(TANK_POOL[i]);
        }
    }
    
    
    public static void setBattleField(BattleField field) {
        battleField = field;
    }
    
    public void tick(){
        if(isVisible()){
            think();
            drive();
            shoot();
        }
    }
    
    
    public abstract void think();
    
    
    public abstract Bullet shoot();
    
    
    public abstract void initTank();
    
   
    private void tryMove(int dx, int dy) {
        move(dx, dy);
        if (overlapsTank(this))
            move(-dx, -dy);
    }
    
    
   
    protected void explode() {
        Explosion.explode(getRefPixelX(), getRefPixelY(), Explosion.BIG);
        setVisible(false);
    }
	
    
    protected static Tank getTank(int i) {
        if(i>POOL_SIZE-1){
            return null;
        }
        if(TANK_POOL[i]!=null){
            return  TANK_POOL[i];
        }else{
            return null;
        }
    }
    
   
    protected static boolean overlapsTank(Sprite sprite) {
        for (int i = 0; i < POOL_SIZE; i++) {
            if (sprite != TANK_POOL[i] && 
                    TANK_POOL[i].isVisible() &&
                    sprite.collidesWith(TANK_POOL[i], false))
                return true;
        }
        return false;
    }
    
   
    protected void changeDirection(int direction) {
        this.direction = direction;
    }
    
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -