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

📄 gamelogic.java

📁 一個游戲程序,一個不可多得的源碼程序,是學習J2ME的好東東.
💻 JAVA
字号:
import java.io.IOException;
import java.io.InputStream;

public class GameLogic{
    /**初始化**/
    private int subLevel; //小关1,2
    protected int[][] gameMap;
    protected int[][] starMap;
    protected int[] checkColor;
    private int weight;
    private int starScore;

    protected GameLogic(){
    }
    
    protected int getWeight(int level){
        switch(level){
            case 0:
                weight=20;
                break;
            case 1:
                weight=40;
                break;
            case 2:
                weight=60;
                break;
            case 3:
                weight=80;
                break;
            case 4:
                weight=100;
                break;
            
            case 5:
                weight=120;
                break;
            
            case 6:
                weight=140;
                break;
            
            case 7:
                weight=160;
                break;
            
        }
        return weight;
    }

    protected int getStarScore(int level){
        switch(level){
            case 0:
                starScore=10;
                break;
            
            case 1:
                starScore=20;
                break;
            
            case 2:
                starScore=30;
                break;
            
            case 3:
                starScore=40;
                break;
            
            case 4:
                starScore=50;
                break;
            
            case 5:
                starScore=60;
                break;
            
            case 6:
                starScore=70;
                break;
            
            case 7:
                starScore=80;
                break;
            
        }
        return starScore;
    }

     /**==============================**
     /             关卡设置
     / 白1,0空白,红,绿,蓝,黄分别为2,3,4,5
     /**==============================**/
     //游戏地图,包括大关、小关
     protected void readMap(int level, int sublevel) {
         switch (level) {
         case 0:
             readResFiles(0,sublevel);
             break;
         case 1:
             readResFiles(1,sublevel);
             break;
         case 2:
             readResFiles(2,sublevel);
             break;
         case 3:
             readResFiles(3,sublevel);
             break;
         case 4:
             readResFiles(4,sublevel);
             break;
         case 5:
             readResFiles(5,sublevel);
             break;
         case 6:
             readResFiles(6,sublevel);
             break;
         case 7:
             readResFiles(7,sublevel);
             break;
         }
     }
     
     //读取对应关卡文件数据
     private void readResFiles(int level ,int sublevel){
         try {
             InputStream is =
                 getClass().getResourceAsStream(level + ".map");
             int ch;
             while((ch=is.read()) != -1){
                 if (ch == 62) { //'>'
                     //transLevel(is)取得小关数
                     //取得该关数据
                     if(transLevel(is) == sublevel){ //小关数
                         //取得地图数据
                         mapData(is);
                         skipEnter(is);
                         //特殊图形
                         signData(is);
                         skipEnter(is);
                         //任务色
                         levelColor(is);
                         is.close();
                         return;
                     }else{
                         is.skip(18*16+8);  //至少跳过一张地图
                     }
                 }
             }
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
     
     //小关数
     private int transLevel(InputStream is){
         int tmp=0;
         int ch;
           while(true){
             try {
                if((ch=is.read()) == 13){
                     is.read();   //跳过换行
                     break;
                 }
                tmp+=ch;
            } catch (IOException e) {
                e.printStackTrace();
            }
         }
         if(tmp<58){  //小于10(48,49……57)
             tmp=tmp-48;
         }else{  //10以上(97,98,99……)
             tmp=tmp-87;
         }
         return tmp;
     } 

     //地图数据
     private void mapData(InputStream is){
         int ch;
         gameMap=new int[16][16];
         for(int i=0;i<16;i++){
             for(int j=0;j<16;j++){
                 try {
                    ch=is.read();
                     if(ch == 13){ //(13)回车
                         is.read(); //(10)换行跳过
                         gameMap[i][j]=is.read()-48;
                     }else{
                         gameMap[i][j]=ch-48;
                     }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                 /*if(j==15)
                     System.out.println(gameMap[i][j]);
                 else
                     System.out.print(gameMap[i][j]);*/
             }
         }
     }
     
     //特殊图形
     private void signData(InputStream is){
         int ch;
         try {
             if ((ch = is.read()) == 124) { //|
                 ch = is.read();
                 if (ch == 48) { //0为空
                     starMap = null;
                 } else {
                     int h = ch - 48; //行
                     int w = is.read() - 48; //列
                     starMap = new int[h][w];
                     for (int i = 0; i < h; i++) {
                         for (int j = 0; j < w; j++) {
                             ch = is.read();
                             if (ch == 13) { //(13)回车
                                 is.read(); //(10)换行跳过
                                 starMap[i][j] = is.read() - 48;
                             } else {
                                 starMap[i][j] = ch - 48;
                             }
                             /*if (j == w - 1)
                                 System.out.println(starMap[i][j]);
                             else
                                 System.out.print(starMap[i][j]);*/
                         }
                     }
                 }
             }
        } catch (IOException e) {
            e.printStackTrace();
        }
     }
     
     //任务色
     private void levelColor(InputStream is){
         int ch;
         checkColor=new int[4];
         try {
            if((ch=is.read()) == 45){  //-
                 while((ch=is.read()) !=-1){
                     if(ch==13)
                         return;
                     ch=ch-48;
                     //0-3依次为2-5颜色
                     checkColor[ch-2]=ch;
                 }
                 /*for (int i = 0; i < 4; i++) {
                     System.out.print(checkColor[i]);
                 }*/
             }
        } catch (IOException e) {
            e.printStackTrace();
        }
     }
     
     //跳过回车换行
     private void skipEnter(InputStream is){
         try {
            is.read();
            is.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
     }
    //清空
    protected void clearArry(){
        gameMap=null;
        starMap=null;
        checkColor=null;
    }
}

⌨️ 快捷键说明

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