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

📄 mysprite.java~34~

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

import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.LayerManager;
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 MySprite extends Sprite {

    public SubCanvas subCanvas;
    public LayerManager layManager;

    public int x = getX(); //屏幕原点的x坐标
    public int y = getY(); //屏幕原点的y坐标
    public int w = getWidth(); //屏幕宽度
    public int h = getHeight(); //屏幕高度

    private final int frameChangeTick; //可以做帧画面变化的次数

    //每个帧画面在一下区域变化
    private int xBound;
    private int yBound;
    private int wBound;
    private int hBound;

    private int vx;
    private int vy;

    private int ticksSinceLastChangeX = 0;
    private int ticksSinceLastChangeY = 0;


    public MySprite(Image image) {
        super(image);
        this.frameChangeTick = 0;
    }

    /**
     * @param image              传入图像
     * @param frameWidth         目标帧单元宽度
     * @param frameHeight        目标帧单元高度
     * @param frameChangeTick    帧图片数量
     *
     * @param x                  图层横坐标      初始化类图层横坐标加上横向位移
     * @param y                  图层纵坐标      初始化类图层纵坐标加上纵向位移
     * @param vx                 图层移动x轴速度
     * @param vy                 图层移动y轴速度
     * @param xBound             图层运动区域 -- 起点x坐标
     * @param yBound             图层运动区域 -- 起点y坐标
     * @param wBound             图层运动区域 -- 终点x坐标
     * @param hBound             图层运动区域 -- 终点y坐标
     * @param layerManager       图层管理调用器
     */
    public MySprite(Image image, int frameWidth, int frameHeight,
                    int frameChangeTick, int x, int y,
                    int vx, int vy, int xBound, int yBound, int wBound,
                    int hBound, LayerManager layerManager) {

        super(image, frameWidth, frameHeight);

        this.frameChangeTick = frameChangeTick;

        //图层定位及定向
        this.setPosition(x, y);
        if (vx < 0) {
            this.setTransform(Sprite.TRANS_MIRROR);
        } else {
            this.setTransform(Sprite.TRANS_NONE);
        }

        //设置速度和运动边界
        setSpeed(vx, vy);
        setBound(xBound, yBound, wBound, hBound);

        this.layManager = layerManager;
    }

    /**
     * setBound:设定边界值
     */
    public void setBound(int xBound, int yBound, int wBound, int hBound) {
        this.xBound = xBound;
        this.yBound = yBound;
        this.wBound = wBound;
        this.hBound = hBound;
    }

    /**
     * setSpeed: 设定物体的移动速度
     * @param vx 物体x方向的移动步长
     * @param vy 物体y方向的移动步长
     */
    public void setSpeed(int vx, int vy) {
        this.vx = vx;
        this.vy = vy;
    }

    /** 根据传入的参数设定时间, 触发Sprite的内部帧转换(动画效果)
     *  本方法每运行一次, 画面帧改变一次, 同时位置也改变一次
     * @param tickCount 触发频率
     */
    public void tick(int tickCount) {
        if ((tickCount % this.frameChangeTick) == 0) {
            this.nextFrame();
        }

        //画面移动
        this.movePosition();
    }

    /**
     * 没有传入参数的movePosition, 表明没有从外界接受移动指令
     * 而是自身自动移动
     */
    public void movePosition() {
        this.ticksSinceLastChangeX++;
        this.ticksSinceLastChangeY++;

        //获取当前位置
        int x = getX();
        int y = getY();

        //有的时候改变一下速度vx 及 vy
        if ((this.ticksSinceLastChangeX > 20) &&
            ResObj.createRandom(20) == 0) {
            //vx置为-1 或者 1
            this.vx = ResObj.createRandom(2) * 2 - 1;
            this.ticksSinceLastChangeX = 0;
        }
        if ((this.ticksSinceLastChangeY > 20) &&
            ResObj.createRandom(20) == 0) {
            //vx置为-1, 0 或者 1
            this.vy = ResObj.createRandom(3) - 1;
            this.ticksSinceLastChangeY = 0;
        }

        //当触壁时, 速度反向, 速度变化累加器置为0
        if (((vx < 0) && (x + vx < xBound)) ||
            (vx > 0) && (x + getWidth() > (xBound + wBound))) {
            vx = -vx;
            this.ticksSinceLastChangeX = 0;
        }
        if (((vy < 0) && (y + vy < yBound)) ||
            (vy > 0) && (y + getHeight() > (yBound + hBound))) {
            //此时y轴速度既可能反向, 也可能停止
            if (ResObj.createRandom(2) == 0) {
                vy = -vy;
            } else {
                vy = 0;
            }
            this.ticksSinceLastChangeY = 0;
        }

        //当x轴速度反向时, 图形反向
        if (vx < 0) {
            this.setTransform(Sprite.TRANS_MIRROR);
        } else {
            this.setTransform(Sprite.TRANS_NONE);
        }

        //重置图形位置
        x = x + vx;
        y = y + vy;

        //由于是类图层运动先重新定位, 有可能造成在类图层定位后
        //某个Sprite的坐标被抛出了setBound()重定义后的, 自身运动区域,就需要做轻微调整
        if (x < xBound) {
            x = xBound;
        }
        if (x > xBound + wBound) {
            x = xBound + wBound - 1;
        }
        if (y < yBound) {
            y = yBound;
        }
        if (y > yBound + hBound) {
            y = yBound + hBound - 1;
        }

        this.setPosition(x, y);
    }

    /**
     * 碰撞检测接口
     */
    public void checkCollidion() {

    }

}

⌨️ 快捷键说明

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