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

📄 boat.java

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

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

/**
 * 军舰类
 */
public class Boat {
    /**
     * 军舰数组,下标0代表向右,1代表向左
     */
    Image[] boatImage;
    /**军舰方向,0代表向右,1代表向左*/
    int direction = 0;
    /**屏幕宽度*/
    int width;

    /**x坐标*/
    int x;
    /**y坐标*/
    public final static int Y = 60;

    /**向左移动*/
    public final static int BOAT_MOVE_LEFT = 1;
    /**向右移动*/
    public final static int BOAT_MOVE_RIGHT = 0;
    /**移动单位*/
    private int dx = 2;
    /**图片宽度*/
    private int boatImageWidth;
    /**图片高度*/
    private int boatImageHeight;
    /**
     * 构造方法
     * @param boatImage 军舰图片
     * @param width 屏幕宽度
     */
    public Boat(Image[] boatImage, int width) {
        this.boatImage = boatImage;
        this.width = width;
        //初始化图片宽度和高度
        this.boatImageWidth = boatImage[0].getWidth();
        this.boatImageHeight = boatImage[0].getHeight();
    }

    /**
     * 绘制军舰
     * @param g Graphics 画笔对象
     */
    public void paint(Graphics g) {
        g.drawImage(boatImage[direction], x, Y, Graphics.TOP | Graphics.LEFT);
    }

    /**
     * 移动军舰
     * @param derection 移动方向
     */
    public void move(int direction) {
        //设置方向
        this.direction = direction;
        //向左移动
        if (this.direction == BOAT_MOVE_LEFT) {
            //边界检测
            if (x > dx) {
                x -= dx;
            } else {
                x = 0;
            }
        }
        //向右移动
        if (this.direction == BOAT_MOVE_RIGHT) {
            //边界检测
            if (x + boatImageWidth < width - dx) {
                x += dx;
            } else {
                x = width - boatImageWidth;
            }
        }

    }
    /**
     * 获得军舰图片宽度
     * @return int 图片宽度
     */
    public int getBoatImageWidth() {
        return boatImageWidth;
    }
    /**
     * 获得军舰的x坐标
     * @return int 军舰的x坐标
     */
    public int getX() {
        return x;
    }
    /**
     * 获得军舰图片高度
     * @return int 军舰图片高度
     */
    public int getBoatImageHeight() {
        return boatImageHeight;
    }
}

⌨️ 快捷键说明

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