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

📄 ship.java

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

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


public class Ship extends MySprite implements Collidion {
    private boolean bLive = false; //生命状态
    private byte troop = 0; //敌我识别标志

    private int levelState = 0; //船只以及它所发射的鱼雷的速度状态
    private boolean flagRight = true; //当前图形方向.  true为右, false为左
    boolean bHurt = false;
    private int hpLife; //玩家生命值HP
    private byte xStep = 15; //图像平移的步进像素值

    /**
     * 构造函数
     */
    public Ship(SubCanvas subCanvas, Image image,
                int xPos, int yPos, LayerManager layerManager) {
        super(image);
        this.subCanvas = subCanvas;
        this.setPosition(xPos, yPos);
        this.layManager = layerManager;
        this.bLive = true;

        //设定敌我识别
        this.troop = SubCanvas.PLAYER;
        this.levelState = 3;
    }

    /**
     * 改写父类的movePosition函数
     * 响应键盘消息,完成对船只的移动控制
     */
    public void movePosition(int keyState) {
        //判断走向, 执行移动
        x = getX();
        y = getY();
        w = getWidth();
        h = getHeight();

        if (subCanvas.nState == subCanvas.GAME_RUN) {
            if ((keyState & GameCanvas.LEFT_PRESSED) != 0) { //左移
                if (flagRight) {
                    setTransform(Sprite.TRANS_MIRROR);
                    this.flagRight = false;
                } else {
                    x = x - xStep;
                }
            } else if ((keyState & GameCanvas.RIGHT_PRESSED) != 0) { //右移
                if (!flagRight) {
                    setTransform(Sprite.TRANS_NONE);
                    this.flagRight = true;
                } else {
                    x = x + xStep;
                }
            }
        }
        //确保图形在游戏区域
        if (x > SubCanvas.WORLD_WIDTH - w) {
            x = SubCanvas.WORLD_WIDTH - w;
        }
        if (x < 0) {
            x = 0;
        }
        this.setPosition(x, y);

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

        //当受到伤害时
        if (bHurt) {
            this.hurt();
        }

        //重新设置图层显示区域
        subCanvas.adjustViewWindow(x, getWidth());
    }

    /**
     * 船只“开火”命令
     */
    public void fire() {
        if (subCanvas.torpedoVector.size() <= 10) {
            //开火
            Image image = ResObj.createImage("/res/Torpedo.png");
            Torpedo terpedo = new Torpedo(subCanvas, image,
                                          x + w / 5, y + h,
                                          this.troop, this.levelState,
                                          false);
            //添加鱼雷到数组以及图层上
            this.layManager.insert(terpedo, 0);
            subCanvas.torpedoVector.addElement(terpedo);
            image = null;
        }
    }

    /**
     * 玩家船只受到损伤,减少玩家生命值
     */
    private void hurt() {
        this.hpLife--;
        if (hpLife <= 0) {
            bLive = false;
        }
    }

    /**
     * 碰撞检测,只有敌潜艇发射的鱼雷才能伤害到玩家船只
     */
    public void checkCollidion() {
        Sprite collideable = null;
        this.bHurt = false;
        for (int j = 0; j < subCanvas.enemyTorpedoVector.size(); j++) {
            collideable = (Sprite) subCanvas.enemyTorpedoVector.elementAt(j);
            if (collidesWith(collideable, true)) {
                this.bHurt = true;
            }
        }
    }

    /**
     * 获取玩家的生命值状态
     */
    public boolean getLifeState() {
        return bLive;
    }

    /**
     * 获取船只的敌我属性
     */
    public int getTroopState() {
        return troop;
    }

    /**
     * 获取玩家生命值
     */
    public int getHpLife() {
        return hpLife;
    }

    /**
     * 设置玩家生命值
     */
    public void setHpLife(int hpLife) {
        this.hpLife = hpLife;
    }
}

⌨️ 快捷键说明

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