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

📄 gamecanvas.java~656~

📁 连连看J2ME
💻 JAVA~656~
📖 第 1 页 / 共 2 页
字号:
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.media.*;
import java.util.TimerTask;
import java.util.Timer;
import java.util.Random;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class GameCanvas extends Canvas implements CommandListener {
    int number; //消除数
    Command newGame = new Command("新一局", Command.OK, 1);
//    boolean newGame = true;
    Random rnd = new Random();
    timerTask task = new timerTask(this);
    Timer timer = new Timer();
    boolean Start = false;
    Command exit;
    int time = 180; //
    playMusic pbg = new playMusic(this);
    protected static final int GAME_START = 0;
    protected static final int GAME_INIT = 1;
    protected static final int GAME_PLAY = 2;
    protected static final int GAME_END = 3;
    protected static final int GAME_MENU = 4;
    int socre = 0;
    int Sx = 1, Sy = 1; //选择框坐标
    int sx;
    Form form;
    Form form1;
    Font font;
    int gamestate;
    int width, height;
    Image logo;
    Thread thread = null;
    int Py, y = 0; //菜单指针
    String s = "<< please choice item >>";
    Font f;
    Display display;
    Command Return;
    Gauge ga;
    int ROW = 12, COL = 10;
    Image mapArray[] = new Image[10];
    int color = 0xFAE6A9;
    int frame = 0xCCB46C;
    int Tcolor = 0xC18E00;
    protected int[][] map = { {0, 1, 3, 2, 4, 6, 5, 9, 7, 8}, {7, 9, 8,
                            1, 5, 0, 4, 2, 3, 6}, {8, 9, 6, 7, 4, 5, 1, 2, 3,
                            0}, {2, 3, 1, 0, 6, 4, 5, 7, 8, 9}, {3, 2, 1, 0, 9,
                            8, 7, 6, 5,
                            4}, {9, 6, 3, 2, 5, 8, 7, 4, 1, 0}, {7, 4, 1, 2, 0,
                            5, 8, 3, 6, 9}, {8, 5, 2, 0, 3, 6, 9, 1, 4, 7}, {4,
                            5, 7, 8, 9, 6, 3, 2, 1, 0}, {1, 2, 4, 3, 0, 6, 7, 5,
                            8, 9}, {9, 0, 7, 5, 4, 6, 3, 2, 1, 8}, {0, 1, 2, 3,
                            8, 7, 6, 5, 4, 9}
    };
    int[][] _temp = new int[ROW][COL];

    String sel[] = new String[2]; //选择的2个数字
    public GameCanvas() {
        exit = new Command("退出", Command.EXIT, 1);
        ga = new Gauge("难度", true, 10, 1);
        form1 = new Form("难易度设定");
        form1.append("\n\n\n");
        form1.append(ga);
        form1.append("设定游戏难易程度,进度条越靠右难度越大");
        form = new Form("关于连连看");
        Return = new Command("返回", Command.OK, 1);
        form1.addCommand(Return);
        form.append("版本:水晶连连看V1.0\n");
        form.append("程序:魏超\n");
        form.append("美工:张文强\n");
        form.append("测试:山菊花\n");
        form.append("E-mail:superwei_800@163.com\n");
        form.append("QQ:414197507\n");
        form.append("开始日期:2005.11\n");
        form.append("完成日期:2005.");
        form.addCommand(Return);
        form.setCommandListener(this);
        form1.setCommandListener(this);
        font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
        f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        display = Display.getDisplay(Main.instance);
        display.setCurrent(this);
        gamestate = GAME_MENU;
        width = this.getWidth();
        height = this.getHeight();
        Py = height - 0x0064;
        try {
            logo = Image.createImage("/pic/logo.png");
            for (int i = 0; i < 10; i++) {
                mapArray[i] = Image.createImage("/pic/" + i + ".png");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // pbg.run(); //bgsound play
        //  pbg.playfile();
        SaveMap(map, _temp); //保存初始地图

    }

    void SaveMap(int[][] map1, int[][] map2) { //保存原始地图
        for (int i = 0; i < map1.length; i++) {
            for (int j = 0; j < map1[i].length; j++) {
                map2[i][j] = map1[i][j];
            }
        }
    }

    void drawMap(Graphics g1) {
        int x, y;
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
                x = j * 0x0014 + 0x0014;
                y = i * 0x0014 + 0x0014;
                switch (map[i][j]) {
                case 0:
                    g1.drawImage(mapArray[0], x, y, 20);
                    break;
                case 1:
                    g1.drawImage(mapArray[1], x, y, 20);
                    break;
                case 2:
                    g1.drawImage(mapArray[2], x, y, 20);
                    break;
                case 3:
                    g1.drawImage(mapArray[3], x, y, 20);
                    break;
                case 4:
                    g1.drawImage(mapArray[4], x, y, 20);

                    break;
                case 5:
                    g1.drawImage(mapArray[5], x, y, 20);

                    break;
                case 6:
                    g1.drawImage(mapArray[6], x, y, 20);

                    break;
                case 7:
                    g1.drawImage(mapArray[7], x, y, 20);

                    break;
                case 8:
                    g1.drawImage(mapArray[8], x, y, 20);

                    break;
                case 9:
                    g1.drawImage(mapArray[9], x, y, 20);

                    break;
                }
            }
        }
    }

    void newGame() { //start new game
        SaveMap(_temp, map);
        map = RandMap(map);
        sel[0] = sel[1] = null;
        repaint();
        socre = 0;
        Sx = 1;
        Sy = 1;
        time = 180;
        gamestate = GAME_PLAY;
    }

    void Jb() {
        if (y == 1) {
            display.setCurrent(form1);
        }
    }

    void command() {
        if (y == 0) {
            gamestate = GAME_PLAY;
            newGame();
            //new
        } else if (y == 1) {
            Jb();
//难度
        } else if (y == 2) {
            display.setCurrent(form);
//关于
        } else if (y == 3) {
            exit();
//退出
        }
    }

    int[][] RandMap(int[][] _map) { //随机排列数组
        int temp1;
        int temp;
        for (int i = 0; i < _map.length; i++) {
            for (int j = 0; j < _map[i].length; j++) {
                temp1 = rnd.nextInt(10);
                temp = _map[i][j];
                _map[i][j] = _map[i][temp1];
                _map[i][temp1] = temp;
            }
        }
        return _map;
    }

    void exit() {
        if (y == 3) {
            Main.quitApp();
        }
    }

    void menu() {
        if (y == 0) {
            s = "开始新的一局游戏";
        } else if (y == 1) {
            s = "设定当前游戏的难度";
        } else if (y == 2) {
            s = "关于本游戏";
        } else if (y == 3) {
            s = "退出本游戏";
        }
    }

    public void keyPressed(int keycode) {
        if (gamestate == GAME_MENU) {
            switch (this.getGameAction(keycode)) {
            case UP:
                if (y > 0) {
                    y--;
                }
                menu();
                System.out.println(y);
                break;
            case DOWN:
                if (y < 3) {
                    y++;
                }
                menu();
                System.out.println(y);
                break;
            case FIRE:
                command();
                break;
            }
        } else if (gamestate == GAME_PLAY) {
            switch (this.getGameAction(keycode)) {
            case UP:
                if (Sy > 1) {
                    Sy--;
                }
                break;
            case DOWN:
                if (Sy < 12) {
                    Sy++;
                }
                break;
            case LEFT:
                if (Sx > 1) {
                    Sx--;
                }
                break;
            case RIGHT:
                if (Sx < 10) {
                    Sx++;
                }
                break;
            case FIRE:
                Clear();
                select();
                break;
            }
        }
        repaint();
    }

    /**
     * 011102
     * 前面2位:代表数字
     * 3,4位:X位置
     * 5,6位:Y位置
     * @param str String
     * @param first int
     * @param last int
     * @return int
     */
    int Ch_num(String str, int first, int last) { //返回一个指定字符串中任意位置的数值型字符之数值
        String temp = "";
        int number = 0;
        temp = temp + str.charAt(first) + str.charAt(last);
        number = Integer.parseInt(temp);
//        System.out.println("temp" + number);
        return number; //返回first +last位置的数值
    }

    void Clear() { //清除选择数组的数值
        if (sel[0] != null && sel[1] != null) {
            sel[0] = sel[1] = null;
        }
    }

    void XunLu_2(int x1, int y1, int x2, int y2, int number) {

    }

    void XunLu_3(int x1, int y1, int x2, int y2, int number) { //所选数字不在同一行
//        //*****************START***
//         //         改进后程序
//         int startX, endX, startY, endY; //存储x,y的起始,结束值
//        boolean clear = true; //默认可以消除
//        //起判点始终选择在最左边或最上边
//        if (x1 > x2) {
//            startX = x2;
//            endX = x1;
//        } else {
//            startX = x1;
//            endX = x2;
//        }
//        if (y1 > y2) {
//            startY = y2;
//            endY = y1;
//        } else {
//            startY = y1;
//            endY = y2;
//        }
//        /**
//         * 01
//         * 10
//         * 10002
//         * 00236
//         * 20001
//         * 2个1为判断点
//         */
//
//        //*1.从左至右线路
//         for (int i = startX + 1; i <= endX; i++) {
//             if (i != endX) { //表示不在同一列
//                 boolean _lie = true;
//                 for (int j = startY; j <= endY; j++) {
//                     if (map[j][i] != -1) {
//                         _lie = false;
//                         break;
//                     }
//                 }
//                 if (_lie) { //列可以联通
//                     boolean _han=true;//行
//                     if (i + 1 == endX) { //表示第2个点靠左边一个点成立表示两点可以消除
//                         clear=true;
//                         break;
//                     } else {
//                         for (int k = i + 1; k < endX; k++) {
//                             if (map[endY][k] != -1) {
//                                 clear = false;
//                                 _han=false;
//                                 break;
//                             }
//                         }
//                         //*********消除*******
//                          if(_han){
//                              map[y1][x1] = map[y2][x2] = -1;
//                              sel[0] = sel[1] = null;
//                              Sys();
//                              repaint();
//                              return;
//                          }
//                 //**********************************************
//                     }
//
//                 }
//             } else { //如果两个点在同一列
//
//             }
//
//         }
//        //消除处理
//        if(clear){
//            map[y1][x1] = map[y2][x2] = -1;
//            sel[0] = sel[1] = null;
//            Sys();
//            repaint();
//            return;
//        }
            //*2.从右至左线路
//**********************改进后END********************************************

              //*****************************改进前
               if (x1 == x2 && y1 == y2) {
                   return;
               }
        if (x1 == x2 && x1 == 0 && y1 != y2) {

            map[y1][x1] = map[y2][x2] = -1;
            sel[0] = sel[1] = null;
            Sys();
            repaint();
            return;
        }
        if (x1 == x2 && x1 == (COL - 1) && y1 != y2) {

            map[y1][x1] = map[y2][x2] = -1;
            sel[0] = sel[1] = null;
            Sys();
            repaint();
            return;
        }
        boolean isEnd = false;
        boolean row_clear = true; //行
        boolean col_clear = true; //列
        int x = 0, endX = 0;
        int y = 0, endY = 0;
        int tempX = 0;
        int temp = 0;
        int direction = 0; //默认向下寻找
        if (x1 > x2) {
            x = x2;
            endX = x1;
        } else if (x1 < x2) {
            x = x1;
            endX = x2;
        } //x
        if (y1 > y2) {
            y = y2;
            endY = y1;
            tempX = x2;
            temp = x1;
        } else if (y1 < y2) {
            y = y1;
            tempX = x1;
            endY = y2;
            temp = x2;
        } //y
        //定义循环起点
        if (x1 == x2 && y + 1 == endY && y1 != y2) { //同一列

            map[y1][x1] = map[y2][x2] = -1;
            sel[0] = sel[1] = null;
            Sys();
            repaint();
            return;
        } else if (x1 == x2 && y1 != y2) {

            for (int i = y + 1; i < endY; i++) {
                if (map[i][x1] != -1) {
                    col_clear = false;
                    break;
                }
            }
            if (col_clear) {
                map[y1][x1] = map[y2][x2] = -1;
                sel[0] = sel[1] = null;
                Sys();
                repaint();
                return;
            }
        }
        while (!isEnd) {
//            System.out.println("temp***:" + temp);
//            System.out.println("endY***:" + endY);
            boolean Vline = true; //行
            boolean Hline = true; //表示当前列搜索成功中间没有阻挡
            switch (direction) {
            case 0:
                for (int i = y + 1; i <= endY; i++) {
                    if (map[i][tempX] != -1) {
                        direction = 1;
                        Hline = false;
                        break;
                    }
                }
                if (Hline) {
                    for (int i = x + 1; i < endX; i++) {
                        if (map[endY][i] != -1) {
                            direction = 1;

⌨️ 快捷键说明

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