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

📄 enemysub.java~77~

📁 j2me源代码
💻 JAVA~77~
字号:
package mysubmarine;

import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 * <p>Title: 一个简单的潜艇游戏</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: Star Group</p>
 *
 * @author not attributable
 * @version 1.0
 */
public class EnemySub extends MySprite implements Collidion {
    private SubCanvas subCanvas;
    private boolean bLive = false; //生命状态
    private byte troop = 0; //敌我识别标识
    private int levelState = 0; //速度状态

    private int xStep = 2;
    private int yStep = 0;
    private int vx = 0;
    private int nFireCount = 0;

    /** 初始化敌人图层Sprite
     *
     * @param subCanvas
     * @param image
     * @param xPos           敌人潜艇初始横坐标
     * @param yPos           敌人潜艇初始纵坐标
     * @param levelState     难度水平
     */

    public EnemySub(SubCanvas subCanvas, Image image,
                    int xPos, int yPos, int levelState) {
        super(image);
        this.subCanvas = subCanvas;

        //设定敌人潜艇出现位置
        this.x = xPos;
        this.y = yPos;
        setPosition(x, y);

        this.levelState = levelState * 3;
        this.bLive = true;
        this.troop = SubCanvas.ENEMY;

        //根据初始方向判断图形是否需要转向, 并设定速度方向
        if (this.x < SubCanvas.WORLD_WIDTH / 2) {
            this.setTransform(Sprite.TRANS_NONE);
            vx = this.xStep;
        } else {
            this.setTransform(Sprite.TRANS_MIRROR);
            vx = ( -1) * this.xStep;
        }
    }

    /**
     * 触发函数
     * 在生存状态中, 根据速度水平(循环次数)移动鱼雷
     */
    public void tick() {
        int i = 0;
        int count = 45 - 5 * levelState / 3;
        //当当前生命状态为真, 并且速度状态尚没有结束(还可以继续运行时)
        while (bLive && (i < this.levelState)) {
            movePosition();
            i++;
        }

        if (this.nFireCount >= count) {
            fire();
            nFireCount = 0;
        } else {
            this.nFireCount++;
        }
    }

    /**
     * fire
     */
    private void fire() {
        if (subCanvas.enemyTorpedoVector.size() <= 10) {
            //开火
            Image image = ResObj.createImage("/res/Torpedo2.png");
            Torpedo terpedo = new Torpedo(subCanvas, image, x + w / 2, y - h,
                                          troop, this.levelState / 3, false);
            //添加鱼雷到数组以及图层上
            subCanvas.layerManager.insert(terpedo, 0);
            subCanvas.enemyTorpedoVector.addElement(terpedo);
            image = null;
        }
    }

    /**
     * 以一个步长为单位移动图形, 如果超出边界则置生存状态为false
     * 移动之后检查碰撞事件, 如果需要消失也同样置生存状态为false
     */
    public void movePosition() {
        this.x = x + this.vx;

        //确保图形在游戏区域, 当超出游戏区时, 在反向出现
        if (x > SubCanvas.WORLD_WIDTH - w) {
            x = 0;
            this.y = SubCanvas.SHIP_HEIGHT + 20
                     + ResObj.createRandom(SubCanvas.mainHeight
                                           - SubCanvas.SHIP_HEIGHT - 20);
        }
        if (x < 0) {
            x = SubCanvas.WORLD_WIDTH - w;
            this.y = SubCanvas.SHIP_HEIGHT + 20
                     + ResObj.createRandom(SubCanvas.mainHeight
                                           - SubCanvas.SHIP_HEIGHT - 20);
        }

        if (y < SubCanvas.SHIP_HEIGHT + 20) {
            y = SubCanvas.SHIP_HEIGHT + 20;
        }

        if (y > SubCanvas.mainHeight - h) {
            y = SubCanvas.mainHeight - h;
        }

        this.setPosition(x, y);

        //处理碰撞事件
        checkCollidion();
    }

    public void checkCollidion() {
        Torpedo torpedo = null;

        for (int i = 0; i < subCanvas.torpedoVector.size(); i++) {
            torpedo = (Torpedo) subCanvas.torpedoVector.elementAt(i);

            //对鱼雷数组中的元素,当鱼雷是玩家释放的时候,才触发碰撞事件
            if (collidesWith(torpedo, false)) {
                this.bLive = false;
            }
        }
        torpedo = null;
    }

    public int getTroop() {
        return this.troop;
    }

    public void setTroop(byte troop) {
        this.troop = troop;
    }

    public int getVx() {
        return this.vx;
    }

    public boolean getLifeState() {
        return this.bLive;
    }
}

⌨️ 快捷键说明

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