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

📄 gamecanvas.java~656~

📁 连连看J2ME
💻 JAVA~656~
📖 第 1 页 / 共 2 页
字号:
                            Vline = false;
                            break;
                        }
                    }
                }
                if (!Hline || !Vline) {
                    direction = 1;
                }
                if (Hline && Vline) {
                    map[y1][x1] = map[y2][x2] = -1;
                    sel[0] = sel[1] = null;
                    Sys();
                    repaint();
                    isEnd = true;
                    return;
                }

                break;
            case 1:
                for (int i = endY - 1; i >= y; i--) {
                    if (map[i][temp] != -1) {
                        Hline = false;
                        break;
                    }
                }

                if (Hline) {
                    for (int i = x + 1; i < endX; i++) {
                        if (map[y][i] != -1) {
                            Vline = false;
                            break;
                        }
                    }
                }
                if (Hline && Vline) {
                    map[y1][x1] = map[y2][x2] = -1;
                    sel[0] = sel[1] = null;
                    Sys();
                    repaint();
                    isEnd = true;
                    return;
                }
                if (!Hline || !Vline) {
                    sel[0] = sel[1] = null;
                    return;
                }

                break;
            }
            repaint();
        }

    }

    void  X3_b(int x1, int y1, int x2, int y2) {//寻路2的补充
        int _x, _endx, _y, _endy;
        int _dir; //路径
        //2种情况
        /**1.
         * 100
         * 001
         */
        if (x1 < x2 && y1 > y2 || x1 > x2 && y1 < y2) {
            _dir = 1;
            /**
             * 100
             * 001
             */
        } else {
            _dir = 2;
            /**
             * 001
             * 100
             */
        }
        /**2.
         * 001
         * 100
         */


    }

    void XunLu_1(int x1, int y1, int x2, int y2, int number) { //寻路方法1,所选数字在同一行且中间没有障碍
        boolean clear = true;
        int x = 0, end = 0;
        if (x1 < x2) {
            x = x1;
            end = x2;
        } else if (x1 > x2) {
            x = x2;
            end = x1;
        }
        if (x1 == x2 && y1 == y2) {
            sel[0] = sel[1] = null;
            return;
        }

        if (y1 == y2 && y1 == 0) { //在第一行
            map[y1][x1] = map[y2][x2] = -1;
            sel[0] = sel[1] = null;
            Sys();
            repaint();
            return;
        }
        if (y1 == y2 && y1 == (ROW - 1)) { //在最后一行
            map[y1][x1] = map[y2][x2] = -1;
            sel[0] = sel[1] = null;
            Sys();
            repaint();
            return;
        }

        if (sel[0] == null || sel[0] == "" && sel[1] == null || sel[1] == "") {
            return;
        }
        if (y1 != y2) { //不在同一行
            return;
        }
        for (int i = x + 1; i < end; i++) {
            if (map[y1][i] != -1) {
                clear = false; //如果中间有一个不是-1
                break;
            }
        }
        if (x1 == x2 && y1 == y2) {
            clear = false;
        }

        if (clear) {
            map[y1][x1] = map[y2][x2] = -1;
            Sys();
            sel[0] = sel[1] = null;
        }
        if (!clear) {
            sel[0] = sel[1] = null;
            repaint();
        }
        if (!clear) { //如果同一行中间有障碍调用第二种方法
            XunLu_2(x1, y1, x2, y2, number);
            return;
        }
        repaint();
    }

    void Sys() { //加分,播放消除音乐
        socre += 10;
        if (time <= 180) {
            time += 1;
        }
        number++;
        pbg.playClear();
    }

    void select() {
        if (map[(Sy - 1) % ROW][(Sx - 1) % COL] == -1) {
            System.out.println("出界");
            return;
        }
        String _x = "" + (Sx - 1);
        String _y = "" + (Sy - 1);
        String _num = "" + map[(Sy - 1) % ROW][(Sx - 1) % COL];
        //实现选择功能,存入sel[]数组,
        if (map[(Sy - 1) % ROW][(Sx - 1) % COL] < 10) {
            _num = "0" + map[(Sy - 1) % ROW][(Sx - 1) % COL];
        }
        if ((Sx - 1) < 10) {
            _x = "0" + (Sx - 1);
        }
        if ((Sy - 1) < 10) {
            _y = "0" + (Sy - 1);
        }

        if (sel[0] == null) {
            sel[0] = _num + _x + _y;
        } else if (sel[0] != null && sel[1] == null) {
            if (Ch_num((_num + _x + _y), 0, 1) != Ch_num(sel[0], 0, 1)) {
                sel[0] = _num + _x + _y;
                sel[1] = null;
            } else {
                sel[1] = _num + _x + _y;
            }
        }
//        System.out.println("x:" + (Sx - 1) + "\ny:" + (Sy - 1));
//        sel[0] = _num + _x + _y;
//        sel[1] = _num + _x + _y;
//        System.out.println("sel[0]:" + sel[0] + "\nsel[1]:" + sel[1]);
    }

    public void commandAction(Command command, Displayable displayable) {
        /** @todo Add command handling code */
        if (command == Return) {
            gamestate = GAME_MENU;
            display.setCurrent(this);
        }
        if (command == exit) {
            System.out.println("exit");
            gamestate = GAME_MENU;
            pbg.stop();
            System.out.println("pbg.stop()");
            repaint();

        }
        if (command == newGame) {
            newGame();
        }
    }


    protected void paint(Graphics g) {
        switch (gamestate) {
        case GAME_START:
            g.drawImage(logo, 0, 0, 20);
            break;
        case GAME_MENU:
            g.drawImage(logo, 0, 0, 20);
            g.setColor(0xffffff);
            g.setFont(font);
            g.drawString("新局", 20, height - 100, 20);
            g.drawString("难度", 20, height - 80, 20);
            g.drawString("关于", 20, height - 60, 20);
            g.drawString("退出", 20, height - 40, 20);
            g.setColor(0xff0023);
            g.drawRect(15, Py + (y * 0x0014), 35, 20);
            g.setFont(font);
            g.setColor(0xffffff);
            g.drawString(s, 45, height - 20, 20);
            break;
        case GAME_INIT:
            break;
        case GAME_PLAY:
            if (number == 60) {
                g.drawString("进入下一关", width / 2, height / 2, 20);
                number = 0;
                newGame();
                ga.setValue(ga.getValue() + 1); //下一关
            }
            if (time <= 0) {
                gamestate = GAME_END;
            }
            if (!Start) {
                timer.schedule(task, 1000);
                Start = true;
            }
            addCommand(newGame);
            addCommand(exit);
            this.setCommandListener(this);
            g.setColor(color);
            g.fillRect(0, 0, width, height);
            g.setColor(frame);
            g.drawRect(5, 5, width - 10, height - 10);
            drawMap(g);
            g.setColor(0xff0000);
            g.drawRect(Sx * 20, Sy * 20, 20, 20);

            //画选择框
            g.setColor(0x001234);
            if (sel[0] != null) {
                g.drawRect(Ch_num(sel[0], 2, 3) * 20 + 20,
                           Ch_num(sel[0], 4, 5) * 20 + 20, 20,
                           20);
            }
            if (sel[1] != null) {
                g.drawRect(Ch_num(sel[1], 2, 3) * 20 + 20,
                           Ch_num(sel[1], 4, 5) * 20 + 20, 20,
                           20);
            }
            if (sel[0] != null && sel[1] != null &&
                Ch_num(sel[0], 4, 5) == Ch_num(sel[1], 4, 5)) {
                //寻路1
                XunLu_1(Ch_num(sel[0], 2, 3), Ch_num(sel[0], 4, 5),
                        Ch_num(sel[1], 2, 3), Ch_num(sel[1], 4, 5),
                        Ch_num(sel[0], 0, 1));
            } else if (sel[0] != null && sel[1] != null) {
                XunLu_3(Ch_num(sel[0], 2, 3), Ch_num(sel[0], 4, 5),
                        Ch_num(sel[1], 2, 3), Ch_num(sel[1], 4, 5),
                        Ch_num(sel[0], 0, 1));
                /**
                 * 1000000
                 * 0000001
                 * 0000000
                 */
            }
            g.drawString("积分:" + socre, 30, 5, 20);
            g.drawString("级别:" + ga.getValue(), 150, 5, 20);
            g.setColor(0xC8C5C8);
            g.drawRect(20, height - 20, 180, 10);
            g.setColor(Tcolor);
            g.fillRect(20, height - 20, time, 10);
            break;
        case GAME_END:
            g.setColor(0x000000);
            g.fillRect(0, 0, width, height);
            g.setColor(0xffffff);
            g.drawString("GAME OVER", width / 2, height / 2, 20);
            break;

        }
    }

}


class playMusic {
    playMusic pm;
    Player p, pp;
    GameCanvas my;
    boolean run = true;
    public playMusic(GameCanvas my) {
        this.my = my;
        try {
            InputStream is = getClass().getResourceAsStream(
                    "/sound/InfTrade.wav");
            p = Manager.createPlayer(is, "audio/x-wav");
            //*************
             InputStream in = getClass().getResourceAsStream(
                     "/sound/CHIMES.WAV");
            pp = Manager.createPlayer(in, "audio/x-wav");

            pp.setLoopCount(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void playClear() {
        try {
            p.realize();
            p.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void playfile() {
        try {
            pp.realize();
            pp.start();
        } catch (Exception e) {
            System.out.println("music play fail!!!" + e.toString());
        }
    }

    public void run() {
        Thread t = new Thread() {
            public void run() {
                pm = new playMusic(my);
                if (pm != null) {
                    pm.playfile();
                } while (run) {
//                    System.out.println("running");
                    my.repaint();
                    try {
                        Thread.sleep(100);
                    } catch (Exception e) {}
                }
            }
        };
        t.start();
    }

    public void stop() {
        run = false;
        try {
            pp.close();
            pp = null;

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}


class timerTask extends TimerTask {
    Random r = new Random(); //产生随机赠送的时间,
    int s_time;
    GameCanvas my;
    int t = 0;
    public timerTask(GameCanvas my) {
        this.my = my;
    }

    int r(int x, int timer) { //
        int t = r.nextInt(100);
        System.out.println(" 号码:" + timer % 5);

        if (Integer.toString(timer % 5).equals("0") ||
            Integer.toString(timer % 5).equals("5")) {
            System.out.println("中奖号码:" + timer % 5);
            return 1;
        }
        return 0;
    }

    public void run() {
        int temp = 0;
        while (true) {
            s_time = r.nextInt(100000); //10万分之一的机会
            switch (my.ga.getValue()) {
            case 1:
            case 2:
                temp = r(2, s_time);
                if (my.time < 180) {
                    my.time += temp;
                }
                if (t > 1) {
                    my.time -= 2;
                    t = 0;
                    my.repaint();
                }
                break;
            case 3:
            case 4:
                temp = r(4, s_time);
                if (my.time < 180) {
                    my.time += temp;
                }

                if (t > 2) {
                    my.time -= 2;
                    t = 0;
                    my.repaint();
                }

                break;
            case 5:
            case 6:
                temp = r(6, s_time);
                if (my.time < 180) {
                    my.time += temp;
                }

                if (t > 2) {
                    my.time -= 3;
                    t = 0;
                    my.repaint();
                }

                break;
            case 7:
            case 8:
                temp = r(8, s_time);
                if (my.time < 180) {
                    my.time += temp;
                }

                if (t > 1) {
                    my.time -= 2;
                    t = 0;
                    my.repaint();
                }

                break;
            case 9:
            case 10:
                temp = r(10, s_time);
                if (my.time < 180) {
                    my.time += temp;
                }

                if (t > 1) {
                    my.time -= 3;
                    t = 0;
                    my.repaint();
                }

                break;
            }

            try {
                Thread.sleep(1000);
            } catch (Exception e) {}
            t++;
        }
    }
}


class test {

}

⌨️ 快捷键说明

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