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

📄 westgamecanvas.java

📁 j2me 横版过关游戏工程。(demo).可以参考下
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
     * @param rs
     *            RecordStore 纪录仓库对象
     * @param index
     *            int 记录索引
     * @param secondNum
     *            int 返回的2维数组的第二维的长度
     * @return byte[][] 2维数据数组
     */
    private byte[][] rsGetRecord(RecordStore rs, int index, int secondNum)
    {
        try
        {
            byte data[] = rs.getRecord(index);
            int len = data.length;
            int firstNum = len / secondNum;
            byte data2[][] = new byte[firstNum][secondNum];
            for (int i = firstNum - 1, j; i >= 0; i--)
                for (j = secondNum - 1; j >= 0; j--)
                    data2[i][j] = data[--len];
            return data2;
        }
        catch (RecordStoreNotOpenException e)
        {
        }
        catch (InvalidRecordIDException e)
        {
        }
        catch (RecordStoreException e)
        {
        }
        return null;
    }

    /**
     * 写入记录
     *
     * @param rs
     *            RecordStore 纪录仓库对象
     * @param recordId
     *            int 记录索引
     * @param data2
     *            byte[][] 2维数据数组
     */
    private void rsSetRecord(RecordStore rs, int recordId, byte[][] data2)
    {
        try
        {
            int firstNum = data2.length;
            int secondNum = data2[0].length;
            int len = firstNum * secondNum;
            byte data[] = new byte[len];
            for (int i = firstNum - 1, j; i >= 0; i--)
                for (j = secondNum - 1; j >= 0; j--)
                    data[--len] = data2[i][j];
            rs.setRecord(recordId, data, 0, data.length);
        }
        catch (RecordStoreNotOpenException e)
        {
        }
        catch (InvalidRecordIDException e)
        {
        }
        catch (RecordStoreFullException e)
        {
        }
        catch (RecordStoreException e)
        {
        }
    }


    /** rms 数据 */
    private byte   rsNameScore[][];

    /** rms 数据库名称 */
    private String rsName = "west";

    //-----------------------------------------------------------------------rms>
    /** 生成高分榜文字信息数组 */
    private void menuScoreTextHandle()
    {
        //TODO 生成高分榜信息数组
        int len = 0;
        int order = 189;
        int sum = 0;
        int i, j;
        for (i = 0; i < 5; i++)
        {
            for (j = 14; j >= 0; j--)
                sum += this.rsNameScore[i][j];
            if (sum == 0)//此条记录为空
            {
                for (j = this.txtScoreInfo.length - 1; j >= len; j--)
                    this.txtScoreInfo[j] = -7;
                break;
            }
            this.txtScoreInfo[len++] = order;//字符1的位置
            this.txtScoreInfo[len++] = -7;//空格
            for (j = 0; j < 10; j++)
                this.txtScoreInfo[len++] = ( this.rsNameScore[i][j] - 1 ) * 7;
            for (j = 3; j > 0; j--)
                this.txtScoreInfo[len++] = -7;
            for (j = 10; j < 15; j++)
                this.txtScoreInfo[len++] = ( this.rsNameScore[i][j] + 26 ) * 7;
            order += 7;
            sum &= 0;
        }
    }

    /** 对新的玩家得分在高分榜数组中排序 */
    private void menuScoreSort()
    {
        //TODO 对新的玩家得分在高分榜数组中排序
        mark = 9;
        if (score <= 0)
        {
            markNum &= 0;
            return;
        }
        byte scorePre[];
        byte scoreNow[] = this.rsNameScore[5];
        System.arraycopy(this.splitNum(score, 5), 0, scoreNow, 10, 5);
        for (int i = 0, j, k; i < 5; i++)
        {
            scorePre = this.rsNameScore[i];
            for (j = 10; j < 15; j++)
                if (scoreNow[j] > scorePre[j])
                {
                    for (k = 5; --k > i;)
                        this.rsNameScore[k] = this.rsNameScore[k - 1];
                    this.rsNameScore[k] = scoreNow;
                    i = 5;
                    break;
                }
        }
        markNum = 0x40000;
        this.menuScoreTextHandle();
    }

    /**
     * 把一个数字每位数存到数组中
     *
     * @param num
     *            int 要处理的数字
     * @param len
     *            int 数组长度
     *
     * @return 存数字的数组
     */
    private byte[] splitNum(int num, int len)
    {
        //TODO 把一个数字每位数存到数组中
        byte nums[] = new byte[len];
        for (int i = len; --i >= 0;)
        {
            nums[i] = (byte) ( num % 10 );
            num = num / 10;
        }
        return nums;

    }

    /**
     * 将字符信息数组中的索引转化成x位置
     *
     * @param idx
     *            byte[] 字符索引信息数组
     * @param w
     *            int 字符宽度
     * @param haveNum
     *            boolean 数组中是否含有分段数
     * @return int[] 字符x位置信息数组
     */
    private int[] menuTextIdxToX(byte idx[], int w, boolean haveNum)
    {
        //TODO 将字符信息数组中的索引转化成x位置
        int len = idx.length;
        int temp[] = new int[len];
        if (haveNum)
        {
            for (int i = 0, j; i < len;)
            {
                j = idx[i];
                temp[i] = j;
                j += i;
                for (i++; i <= j; i++)
                    temp[i] = ( idx[i] - 1 ) * w;
            }
        }
        else
        {
            for (int i = len - 1; i >= 0; i--)
            {
                if (idx[i] == -2)
                    temp[i] = idx[i];
                else
                    temp[i] = ( idx[i] - 1 ) * w;
            }
        }
        return temp;
    }

    /**
     * 操作newgame界面的字符显示
     *
     * @param dx
     *            int 按左取-1,按右取1,按上下时取0,代表箭头移动的位置个数
     * @param dy
     *            int 按上取-1,按下取1,按左右时取0,代表字符变化的跨度
     */
    private void menuChangeName(int dx, int dy)
    {
        if (dy == 0)
        {//处理初始化或箭头移动
            this.txtNameIdx[this.menuNameUpIdx] &= 0;
            this.txtNameIdx[this.menuNameUpIdx + MENU_NAME_UP_DOWN] &= 0;
            if (dx == 0)
            {//初始化
                System.arraycopy(this.rsNameScore[5], 0, this.txtNameIdx,
                        MENU_NEW_NAME_IDX, 10);
                this.menuNameUpIdx = 0;
            }
            else
            {//箭头移动
                this.menuNameUpIdx += dx;
                if (this.menuNameUpIdx < 0)
                    this.menuNameUpIdx = 9;
                else if (menuNameUpIdx > 9)
                    this.menuNameUpIdx = 0;
            }
            this.txtNameIdx[this.menuNameUpIdx] = MENU_UP_SOURCE_IDX;
            this.txtNameIdx[this.menuNameUpIdx + MENU_NAME_UP_DOWN] = MENU_DOWN_SOURCE_IDX;
        }
        else
        {//字符变化
            int idx = this.menuNameUpIdx;
            this.rsNameScore[5][idx] += dy;
            if (this.rsNameScore[5][idx] < 0)
                this.rsNameScore[5][idx] = 26;
            else if (this.rsNameScore[5][idx] > 26)
                this.rsNameScore[5][idx] = 0;
            this.txtNameIdx[this.menuNameUpIdx + 10] = this.rsNameScore[5][idx];
        }
        //将索引号转化成在图片中的位置
        this.txtNameInfo = this.menuTextIdxToX(this.txtNameIdx, 7, false);

    }

    /**
     * 二级菜单的文本内容显示
     *
     * @param g
     *            Graphics Canvas 画笔
     * @param info
     *            int 文字信息数组
     * @param leftX
     *            int 左边x
     * @param rightX
     *            int 右边x
     * @param txtY
     *            int 上边x
     */
    private void menuDrawSubText(Graphics g, int info[], int leftX, int rightX,
            int txtY)
    {
        //TODO 二级菜单的文本内容显示
        if (null == info)
            return;
        //字符的xy位置
        int txtX;
        int len = info.length;
        //字符索引
        int idx = 0;
        //文字内容显示到结尾的标志
        boolean isTextEnd = false;
        //每一行的循环
        for (; txtY < 208; txtY += MENU_SUB_TEXT_H)
        {
            //每一行中每列的循环
            for (txtX = leftX; txtX < rightX; txtX += MENU_SUB_TEXT_W)
            {
                //字符索引达到最大值,文字内容显示到结尾
                if (idx >= len)
                {
                    isTextEnd = true;
                    break;
                }
                g.setClip(txtX, txtY, MENU_SUB_TEXT_W, MENU_SUB_TEXT_H);
                g.drawImage(this.imgMenu[26], txtX - info[idx++], txtY, 0);
            }
            //文字内容显示到结尾,结束行循环,所有循环结束
            if (isTextEnd)
                break;
        }
    }


    private DataInputStream dataOpen(String name)
    {
        return new DataInputStream(getClass().getResourceAsStream(name));
    }

    /**
     * 关闭数据流
     *
     * @param dis
     *            DataInputStream 数据流
     */
    private void dataClose(DataInputStream dis)
    {
        try
        {
            dis.close();
            dis = null;
        }
        catch (IOException e)
        {
        }
    }

    private Image[] dataGetImg(int idx)
    {
        //可以提出到外面的变量
        DataInputStream dis = this.dataOpen("imgData");
        Image img[] = null;
        byte[] dataImgIdx;//图片分组的信息,将第一个字母相同的图片分在一组,记录了每组图片的个数
        byte[][] dataChunkName;//包括图片每个数据块的名字,和首尾数据块的crc码信息
        //打开图片数据
        try
        {
            int i,j,imgNum, startIdx = 0;
            //读取图片分组信息----------------------------
            i = dis.readByte();
            dataImgIdx = new byte[i];

            if (idx >= i){
                System.out.println("not get img!!!!!");
                return null;
            }else{
                System.out.println(" get img!!!!!");
            }
            dis.read(dataImgIdx);
            for (i = 0; i < idx;)
                startIdx += dataImgIdx[i++];
            imgNum = dataImgIdx[idx];
            img = new Image[imgNum];
            //读取图片数据块名字--------------------------
            j = dis.readByte() / 4;
            dataChunkName = new byte[j][4];
            for (i = 0; i < j; i++)
                dis.read(dataChunkName[i]);
            //跳过要读的首张图片以前的图片----------------
            for (i = 0; i < startIdx; i++)
                dis.skip(dis.readShort() + 1);
            //读取图片数组图片---------------------------
            byte byteArray[] = null;
            byte byteTemp[] = null;
            short d;

            for (i = 0; i < imgNum;)
            {
                //取得图片的压

⌨️ 快捷键说明

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