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

📄 main.java

📁 这是一个RPG的小游戏
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        if (direction == 2 && startY > endY) 
            return;
        g.drawLine(startX, startY, endX, endY);
    }
    
    public static void showMenu() {
        if (subMenuVisible)
            return;
        subMenuVisible = true;
        subMenuIndex = 0;
        switch (missionStatus) {
        case MISSION_FAIL:
            subMenuItems = new Image[2];
            subMenuItems[0] = word_retry;
            subMenuItems[1] = word_mainMenu;
            break;
        case MISSION_COMPLETED:
            if (missionNum < missionCount - 1) {
                subMenuItems = new Image[2];
                subMenuItems[0] = word_nextMission;
                subMenuItems[1] = word_mainMenu;
            } else {
                subMenuItems = new Image[1];
                subMenuItems[0] = word_mainMenu; 
            }
            break;
        case MISSION_RUNNING:
            subMenuItems = new Image[3];
            subMenuItems[0] = word_continue;
            subMenuItems[1] = word_retry;
            subMenuItems[2] = word_mainMenu;
            break;
        }
    }
    
    public void loadMission(int no) throws Exception {
        missionNum = no;
        InputStream input = getInputStream();
        input.read();
        
        //略过前面的字节,定位到指定的任务的存储位置上。
        int count = 0;
        for (int i = 0; i < missionNum; i++) {
            //略过任务描述
            count = input.read();
            input.skip(count + 122);
           //略过工具描述
            count = input.read();
            input.skip(count);
        }
        
        //读取任务描述
        count = input.read();
        byte[] descBytes = new byte[count];
        input.read(descBytes);
        DataInputStream di = new DataInputStream(new ByteArrayInputStream(descBytes));
        description = di.readUTF();
        di.close();
        
        input.read(missionData);
        
        int pos = input.read();
        initRow = pos & 0x0F;
        initColumn = pos >> 4;
        floor = input.read();
        // 读取工具描述
        count = input.read();
        initTools = new byte[count];
        input.read(initTools);
        // 关闭输入流
        input.close();

        reload();
        
        descriptionX = 0;
        int w = font.stringWidth(description);
        int h = font.getHeight();
        image_description = Image.createImage(w, h);
        Graphics g = image_description.getGraphics();
        g.setFont(font);
        g.setColor(0x0099FF);
        g.fillRect(0, 0, w, h);
        g.setColor(0xFFFFFF);
        g.drawString(description, 0, 0, 20);
        image_description = resizeImage(image_description, w * 14/ h, 14);
        
        System.gc();
        
        playSound(players[Math.abs(Stuff.random.nextInt())%playersCount], 1);
    }

    /**
     * 获取任务文件的输入流
     * @return 任务文件的输入流
     */
    public InputStream getInputStream() {
        return getClass().getResourceAsStream("/mission.dat");
    }
    
    /**
     * 重新加载游戏
     */
    public void reload() {
        setStatus(MISSION_RUNNING);
        startTime = System.currentTimeMillis();
        // 进行必要的清理
        dweep.reset();
        dweep.setLocation(initRow, initColumn);
        dweep.setPath(initRow, initColumn);
        selectedToolIndex = -1;

        for (int i = 0; i < 12; i++) {
            for (int j = 0; j < 10; j++) {
                byte value = missionData[i * 10 + j];
                if (value < STUFF_BOMB) {
                    base[i][j] = value;
                    stuffs[i][j] = null;
                } else {
                    stuffs[i][j] = new Stuff(value, i, j);
                    base[i][j] = 0;
                }
            }
        }

        for (int k = 0; k < 9; k++)
            tools[k] = (k < initTools.length) ? initTools[k] : 0;

        System.gc();
    }

    /**
     * 设置任务的状态
     * 
     * @param status
     *            int
     */
    public static void setStatus(int state) {
        if (state == missionStatus)
            return;
        missionStatus = state;
        if (missionStatus == MISSION_COMPLETED) {
            playSound(player_win, 1);
            
            int i = missionNum / 8;
            int m = missionNum % 8;
            record[i] |= (0x01 << m);
            showMenu();
        } else if (missionStatus == MISSION_FAIL) {
            showMenu();
        }
    }
    
    public static void playSound(Player player, int count) {
        if (MainMIDlet.isSoundOpen) {
            try {
                nowPlayer.deallocate();
                nowPlayer = player;
                nowPlayer.setLoopCount(count);
                nowPlayer.start();
            } catch (MediaException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static boolean canCross(int row, int column) {
        return stuffs[row][column] == null;
    }
    
    /**
     * 保存任务完成的进度
     */
    public static void save() {
        try {
            RecordStore recordStore = RecordStore.openRecordStore("JDweep",
                    false);
            recordStore.setRecord(1, record, 0, record.length);
            recordStore.closeRecordStore();
        } catch (Exception e) {
        }

    }
      
    public static void removeSelectedTool() {
        tools[selectedToolIndex] = 0;
        selectedToolIndex = -1;
    }
    
    /**
     * 添加一个工具
     * @param type 工具的类型
     * @return 是否添加成功.假如工具包已满返回false,否则将工具加入到工具包返回true
     */
    public static boolean addTool(int type) {
        for (int i = 0; i < tools.length; i++) {
            if (tools[i] <= 0) {
                tools[i] = type;
                return true;
            }
        }
        return false;
    }
    
   
    /**
     * 返回指定位置的物体
     * @param row int
     * @param column int
     * @return Stuff
     */
    public static Stuff getStuff(int row, int column) {
        if (row < 0 || row >= 12 || column < 0 || column >= 10)
            return null;

        return stuffs[row][column];
    }
    
    
    private static final int FP_SHIFT = 13;

    private Image resizeImage(Image src, int destW, int destH) {
        int srcW = src.getWidth();
        int srcH = src.getHeight();

        // create pixel arrays
        int[] destPixels = new int[destW * destH];
        // pixels
        int[] srcPixels = new int[srcW * srcH];
        
        src.getRGB(srcPixels, 0, srcW, 0, 0, srcW, srcH);
        // precalculate src/dest ratios
        int ratioW = (srcW << FP_SHIFT) / destW;
        int ratioH = (srcH << FP_SHIFT) / destH;

        int[] tmpPixels = new int[destW * srcH]; // temporary buffer for the
        // horizontal resampling
        // step

        // variables to perform additive blending
        int argb; // color extracted from source
        int a, r, g, b; // separate channels of the color
        int count; // number of pixels sampled for calculating the average

        // the resampling will be separated into 2 steps for simplicity
        // the first step will keep the same height and just stretch the
        // picture horizontally
        // the second step will take the intermediate result and stretch it
        // vertically

        // horizontal resampling
        for (int y = 0; y < srcH; ++y) {
            for (int destX = 0; destX < destW; ++destX) {
                count = 0;
                a = 0;
                r = 0;
                b = 0;
                g = 0; // initialize color blending vars
                int srcX = (destX * ratioW) >> FP_SHIFT; // calculate
                // beginning of
                // sample
                int srcX2 = ((destX + 1) * ratioW) >> FP_SHIFT; // calculate
                // end of
                // sample

                // now loop from srcX to srcX2 and add up the values for
                // each channel
                do {
                    argb = srcPixels[srcX + y * srcW];
                    a += ((argb & 0xff000000) >> 24); // alpha channel
                    r += ((argb & 0x00ff0000) >> 16); // red channel
                    g += ((argb & 0x0000ff00) >> 8); // green channel
                    b += (argb & 0x000000ff); // blue channel
                    ++count; // count the pixel
                    ++srcX; // move on to the next pixel
                } while (srcX <= srcX2 && srcX + y * srcW < srcPixels.length);

                // average out the channel values
                a /= count;
                r /= count;
                g /= count;
                b /= count;

                // recreate color from the averaged channels and place it
                // into the temporary buffer
                tmpPixels[destX + y * destW] = ((a << 24) | (r << 16)
                        | (g << 8) | b);
            }
        }

        // vertical resampling of the temporary buffer (which has been
        // horizontally resampled)
        for (int x = 0; x < destW; ++x) {
            for (int destY = 0; destY < destH; ++destY) {
                count = 0;
                a = 0;
                r = 0;
                b = 0;
                g = 0; // initialize color blending vars
                int srcY = (destY * ratioH) >> FP_SHIFT; // calculate
                // beginning of
                // sample
                int srcY2 = ((destY + 1) * ratioH) >> FP_SHIFT; // calculate
                // end of
                // sample

                // now loop from srcY to srcY2 and add up the values for
                // each channel
                do {
                    argb = tmpPixels[x + srcY * destW];
                    a += ((argb & 0xff000000) >> 24); // alpha channel
                    r += ((argb & 0x00ff0000) >> 16); // red channel
                    g += ((argb & 0x0000ff00) >> 8); // green channel
                    b += (argb & 0x000000ff); // blue channel
                    ++count; // count the pixel
                    ++srcY; // move on to the next pixel
                } while (srcY <= srcY2 && x + srcY * destW < tmpPixels.length);

                // average out the channel values
                a /= count;
                a = (a > 255) ? 255 : a;
                r /= count;
                r = (r > 255) ? 255 : r;
                g /= count;
                g = (g > 255) ? 255 : g;
                b /= count;
                b = (b > 255) ? 255 : b;

                // recreate color from the averaged channels and place it
                // into the destination buffer
                destPixels[x + destY * destW] = ((a << 24) | (r << 16)
                        | (g << 8) | b);
            }
        }

        return Image.createRGBImage(destPixels, destW, destH, true);
    }
}

⌨️ 快捷键说明

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