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

📄 enemysub.java

📁 利用Java 开发的一个手机版的潜艇游戏。游戏实现的重点就是屏幕的绘制滚动技术和碰撞检测技术
💻 JAVA
字号:
package mysubmarine;

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


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;  //物体x轴方向移动的步进长度
    private int yStep = 0;  //物体y轴方向移动的步进长度
    private int xSpeed = 0;  //物体X轴方向移动的速度
    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);
            xSpeed = this.xStep;
        } else {
            this.setTransform(Sprite.TRANS_MIRROR);
            xSpeed = ( -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++;
        }
    }

    /**
     * 开火命令,在触发函数中自动调用
     */
    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;
        }
    }

    /**
     * 潜艇位置移动
     * 移动之后检查碰撞事件
     */
    public void movePosition() {
        this.x = x + this.xSpeed;

        //确保图形在游戏区域, 当超出游戏区时, 在反向出现
        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 getXSpeed() {
        return this.xSpeed;
    }

    /**
     * 获取潜艇的生命状态
     */
    public boolean getLifeState() {
        return this.bLive;
    }
}

⌨️ 快捷键说明

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