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

📄 submarine.java

📁 军舰打潜艇手机版很好玩
💻 JAVA
字号:
package boat;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * 潜水艇类
 */
public class Submarine {
    /**潜水艇图片*/
    private Image[] submarineImage;
    /**屏幕宽度*/
    private int width;
    /**屏幕高度*/
    private int height;
    /**图片宽度*/
    private int imageWidth;
    /***/
    private int imageHeight;
    /**x坐标*/
    private int x;
    /**y坐标*/
    private int y;
    /**移动方向,0代表向右,1代表向左*/
    private int direction;
    /**移动单位*/
    private int dx = 2;
    /**是否爆炸*/
    private boolean isBoom = false;
    /**爆炸图片数组*/
    private Image[] boom;
    /**爆炸图片序号*/
    private int boomIndex = -1;
    /**潜艇编号*/
    private int num;

    /**
     * 构造方法
     * @param submarineImage 潜水艇图片数组
     * @param width 屏幕宽度
     * @param num 潜水艇编号
     * @param boom 爆炸图片数组
     */
    public Submarine(Image[] submarineImage, int width, int height, int num,
                     Image[] boom) {
        this.submarineImage = submarineImage;
        this.width = width;
        this.height = height;
        this.num = num;
        this.boom = boom;
        //获得图片宽度和高度
        this.imageWidth = submarineImage[0].getWidth();
        this.imageHeight = submarineImage[0].getHeight();
        //产生潜水艇
        generate();
    }

    /**
     * 绘制潜水艇
     * @param g 画笔
     */
    public void paint(Graphics g) {
        if (!isBoom) {
            g.drawImage(submarineImage[direction], x, y,
                        Graphics.TOP | Graphics.LEFT);
        } else { //爆炸效果
            if (boomIndex == -1) {
                return;
            }
            g.drawImage(boom[boomIndex], x, y,
                        Graphics.TOP | Graphics.LEFT);
        }
    }

    /**
     * 爆炸处理
     */
    public void boom() {
        //如果爆炸完毕
        if (boomIndex == 2) {
            //修改标志
            isBoom = false;
            boomIndex = -1;
            //重新生成潜艇
            generate();
        }
        if (boomIndex < 2) {
            boomIndex++;
        }
    }

    /**
     * 移动方法
     */
    public void move() {
        //没有爆炸
        if (!isBoom) {
            if (direction == 0) { //向右移动
                //边界检测
                if (x < width + dx) {
                    x += dx;
                } else {
                    x = width + dx;
                    //改变方向
                    direction = 1;
                }
            }

            if (direction == 1) { //向右移动
                //边界检测
                if (x + imageWidth > dx) {
                    x -= dx;
                } else {
                    x = dx - imageWidth;
                    //改变方向
                    direction = 0;
                }
            }
        }
    }

    /**
     * 生成潜水艇坐标
     */
    private void generate() {
        if (num == 0) {
            x = -imageWidth;
            y = height - 30;
            direction = 0;
        }

        if (num == 1) {
            x = width;
            y = height - 60;
            direction = 1;
        }
    }

    /**
     * 判别是否爆炸
     * @return boolean true代表爆炸,false代表没有爆炸
     */
    public boolean isIsBoom() {
        return isBoom;
    }

    /**
     * 获得图片高度
     * @return int 图片高度
     */
    public int getImageHeight() {
        return imageHeight;
    }

    /**
     * 获得图片宽度
     * @return int 图片宽度
     */
    public int getImageWidth() {
        return imageWidth;
    }

    /**
     * 获得x坐标
     * @return int x坐标
     */
    public int getX() {
        return x;
    }

    /**
     * 获得y坐标
     * @return int y坐标
     */
    public int getY() {
        return y;
    }

    /**
     * 设置爆炸效果
     * @param isBoom true代表爆炸
     */
    public void setIsBoom(boolean isBoom) {
        this.isBoom = isBoom;
    }
}

⌨️ 快捷键说明

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