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

📄 gamecanvas.java

📁 一個游戲程序,一個不可多得的源碼程序,是學習J2ME的好東東.
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        for (int j = tmpLength - 2; j >= 0; j--) { //从倒数第2列向左搜索
            for (int i = 0; i < tmpLength; i++) {
                if (gl.gameMap[i][j] == 1) { //white diamonds is not move
                    mark[i][j] = true;
                    //右方有不能动的则所有连接的同色的不能动
                }
                if (gl.gameMap[i][j] > 1 && mark[i][j + 1]) {
                    markDiamonds(i, j, gl.gameMap[i][j]);
                }
            }
        }
        //other
        checkMoveRight();
        // draw the tiles in their new positions
        for (int j = tmpLength - 1; j >= 0; j--) {
            for (int i = 0; i < tmpLength; i++) {
                if (gl.gameMap[i][j] > 1 && !mark[i][j]) {
                    gl.gameMap[i][j + 1] = gl.gameMap[i][j];
                    gl.gameMap[i][j] = 0;
                }
            }
        }
    }

    //递归检测
    private void checkMoveRight() {
        int tmpLength = gl.gameMap.length;
        for (int j = tmpLength - 2; j >= 0; j--) { //从倒数第2列向左搜索
            for (int i = 0; i < tmpLength; i++) {
                if (gl.gameMap[i][j] > 1 && !mark[i][j] && mark[i][j + 1]) {
                    markDiamonds(i, j, gl.gameMap[i][j]);
                    checkMoveRight(); //递归
                    return;
                }
            }
        }
    }

    //搜索同色的方块并做标记
    private void markDiamonds(int i, int j, int color) {
        // if this block isn't the correct colour then return non failure
        if (color != gl.gameMap[i][j]) {
            return;
        }
        // has this block already been checked
        if (mark[i][j]) {
            return;
        }
        // at this point we have found a valid block
        mark[i][j] = true;
        int tmpLength = gl.gameMap.length;
        if (i > 0) {
            markDiamonds(i - 1, j, gl.gameMap[i][j]);
        }
        if (j > 0) {
            markDiamonds(i, j - 1, gl.gameMap[i][j]);
        }
        if (i < tmpLength - 1) {
            markDiamonds(i + 1, j, gl.gameMap[i][j]);
        }
        if (j < tmpLength - 1) {
            markDiamonds(i, j + 1, gl.gameMap[i][j]);
        }
    }

    //初始化mark
    private void intMark() {
        int tmpLength = gl.gameMap.length;
        for (int i = 0; i < tmpLength; i++) {
            for (int j = 0; j < tmpLength; j++) {
                if (mark[i][j]) {
                    mark[i][j] = !mark[i][j];
                }
            }
        }
    }

    /**过小关判断**/
    private void passSubLevel() {
        int ColorCount=0;
        //检测本关需要完成的色块
        for (int k = 0; k < 4; k++) {
            if(gl.checkColor[k]>0){
                ColorCount++;
                oneColorComplete(gl.checkColor[k]);
            }
        }
        if (completeColor != ColorCount)
            return;
        //原来已经通过的就先平衡记数
        if(levelPass[subLevel])
            passCount--;
        else
            levelPass[subLevel] = true;
        stars[subLevel]=false;  //抵消上次的
        viewStar(); //显示大星星
        passCount++;
        //System.out.println("pass "+passCount);
        menuState=7;
        score[subLevel] = getsubLevelScore(); //记分
    }

    /**过大关判断**/
    private void passLevel() {
        if(passCount==20){//全过了到选关
            gameState=PASS;
            repaint();
            //将小关标记清空,并写入记录
            levelPass=new boolean[20];
            sd.writeDate(level, score, stars,levelPass);
            //增加一条新的空白记录
            stars =new boolean [20];
            score =new int [20];
            if(level<7){
                if(level==sd.getLevel()-1)
                     sd.writeDate(level+1, score, stars,levelPass);
            }
            level++;
        }
    }
    
    //计算小关分数
    private int getsubLevelScore() {
        int tmpScore = 0;
        int curWeight=gl.getWeight(level);
        if (doCount > curWeight) {
            tmpScore = (level + 1) * 2;
        } else {
            tmpScore = (curWeight - doCount + level + 1) * 2;
        }
        return tmpScore;
    }

    //某种需要完成的颜色粘连在一起则变为该色完成状态的颜色
    private void oneColorComplete(int color) {
        int count = 0;
        intMark();
        int tmpLength = gl.gameMap.length;
        //统计同色块数量
        for (int i = 0; i < tmpLength; i++) {
            for (int j = 0; j < tmpLength; j++) {
                if (gl.gameMap[i][j] == color && !mark[i][j]) {
                    markDiamonds(i, j, color); //标记
                    count++;
                    if (count > 1) { //大于1则跳出
                        return;
                    }
                }
            }
        }
        //同色方块连在一起,变色为完成状态
        if (count == 1){
            for (int i = 0; i < tmpLength; i++) {
                for (int j = 0; j < tmpLength; j++) {
                    if (gl.gameMap[i][j] == color) {
                        gl.gameMap[i][j] += 4;
                        //System.out.println("change color");
                    }
                }
            }
            completeColor++; //统计完成数
        }
    }

    /**对比特殊图形**/
    private void viewStar() {
        if (gl.starMap != null) { //有记录
            boolean goOut=false;
            int off=0;
            int posionR=0;
            int posionL=0;
            int tmpLength = gl.gameMap.length;
            int starW = gl.starMap[0].length;
            int starH = gl.starMap.length;
            int currColor = getFigureColor();
            //水平扁移
            for (int m = 0; m < starW; m++) {
                if(gl.starMap[0][m]>0){
                    off=m;
                    break;
                }
            }
            //地图中初始位置
            for (int i = 0; i < tmpLength; i++) {
                for (int j = 0; j < tmpLength; j++) {
                    if (gl.gameMap[i][j] == currColor) {
                        posionR = i; //行
                        posionL = j; //列
                        goOut=true;
                        break;
                    }
                }
                if(goOut)
                    break;
            }
            //不符合条件
            if(posionL-off <0)
                return;
            int tmpPL=posionL-off;
            for (int k = 0; k < starH; k++) {
                for (int l = 0; l < starW; l++) {
                    if (gl.starMap[k][l] > 0) { //有色块
                        if (k + posionR>=tmpLength ||
                            l + tmpPL>=tmpLength ||
                            gl.gameMap[k + posionR][l + tmpPL] != currColor) {
                            //System.out.println("not same");
                            return;
                        }
                    }
                }
            }
            stars[subLevel]= true;
            //System.out.println("have a star");
        }
    }

    //获得特殊图形颜色
    private int getFigureColor() {
        int currColor = 0;
        int tmpLength = gl.starMap[0].length;
        for (int k = 0; k < tmpLength; k++) {
            if (gl.starMap[0][k] > 0) {
                currColor = gl.starMap[0][k] + 4;
                break;
            }
        }
        return currColor;
    }
    
    /**声音**/
    //播放/停止声音
    private void soundCotrol() {
       try {
            if (soundOpen) {
                if(player != null)
                    player.close();
                InputStream is= getClass().getResourceAsStream("/music0.mid");
                player = Manager.createPlayer(is, "audio/midi");
                player.realize();
                player.prefetch();
                is.close();
                player.setLoopCount(-1);
                player.start();
                //System.out.println("sound start");
            } else {
                if(player != null){
                    player.stop();
                    player.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MediaException e) {
            e.printStackTrace();
        }
    }
    protected void hideNotify(){
        if(gameState !=GAME){
            soundOpen=false;
            soundCotrol();
        }
    }
    /** TimerTask* */ 
    /* ======刷屏====== */
    class SCFresh extends TimerTask {
        public void run() {
            if (gameState == LOGO) {
                if (timeCount == 24) {
                    timeCount = 0;
                    gameState = INDEX;
                    li.delLogoImg();
                    menuState = 9;
                }
                timeCount++;
                repaint();
                //载入图片
                switch (timeCount){
                    case 1 :
                        li.loadPagesImg0();
                        break;
                    case 5 :
                        li.loadPagesImg1();
                        break;
                    case 9 :
                        li.loadMenuImg();
                        break;
                    case 13 :
                        li.loadGameImg0();
                        break;
                    case 17 :
                        li.loadGameImg1();
                        break;
                    case 21 : //首页的菜单
                        sd = new ScoreData();
                        break;
                    case 23 : //sound
                        soundOpen = true;
                        soundCotrol();
                        break;
                }
            }

            if (gameState == INDEX) {
                if(menuState==1 || menuState==3 || menuState==5){
                    if(timeCount >= 8){
                        timeCount = 0;
                        menuState = 9;
                        repaint();
                    }
                    timeCount++;
                }
            }
            if (gameState == NEWGAME) {
                if(timeCount == 10){
                    timeCount = 0;
                    menuState = 9;
                    gotoSubLevel();
                    repaint();
                }
                timeCount++;
            }
            if (gameState == LEVEL) {
                if(level==8){//通关
                    gameState = WIN;
                    repaint();
                }
            }
            
            if (gameState == PASS) {//过关
                if (timeCount ==7) { //显示一段时间
                    timeCount = 0;
                    gameState = LEVEL;
                    repaint();
                }
                timeCount++;
            }

            if (gameState == SUBLEVEL) {
                passLevel();
            }

            if (gameState == PASSSUB) {//过小关
                if (timeCount ==7) { //显示一段时间
                    timeCount = 0;
                    gameState = SUBLEVEL;
                    repaint();
                }
                timeCount++;
            }
            
            if (gameState == GAME) {
                if(menuState==0){
                    if(timeCount==7){
                        timeCount=0;
                        menuState=8;
                        repaint();
                    }
                    timeCount++;
                }
                if(menuState==1){
                    if(timeCount==7){
                        timeCount=0;
                        menuState=8;
                        repaint();
                    }
                    timeCount++;
                }
            	if (menuState==7) {
                    if (timeCount >= 4) {
                        timeCount = 0;
                        gameState = PASSSUB;
                        menuState=9;
                        repaint();
                    }
            	    timeCount++;
                }
            }
            
            if (gameState == WIN) {
                if (timeCount == 10) {
                    gameState = SEELOG;
                    timeCount = 0;
                    repaint();
                }
                timeCount++;
            }
        }
    }
}

⌨️ 快捷键说明

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