• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Khan's Notebook GCC/GNU/Linux Delphi/Window Java/Anywhere

            路漫漫,長修遠,我們不能沒有錢
            隨筆 - 173, 文章 - 0, 評論 - 257, 引用 - 0
            數據加載中……

            掃雷游戲的設計(Kjava 原創)


            首先我要裝得像高手一樣,來假裝把系統稍微分析一下。

            一般,按照java得開發模式,這種程序一般是分為三個模塊來開發。

            如下三個:

            一個程序運作的主文件,也就是一個midlet的繼承;

            一 個界面的表示類,也就是一個canvas的繼承,界面上應該有些菜單,如new、exit 什么的,那就應該要 implements一個 commandListener消息監聽類(大家可以把java的消息監聽理解為一個線程,一直像倭寇那樣對看得順眼的東西虎視耽耽,當然這里指的是他所 能觸及到的消息,當收到消息的時候,會調用一個抽象函數public void commandAction(Command c, Displayable d),而這個抽象函數使得我們可以通過對他的實現來處理收到的消息,即消息響應)

            最后一個當然就是與界面無關的邏輯單元了,在這里我們定義整個游戲的邏輯,做到邏輯與界面分開。這是我學java的最大收獲,呵呵。


            來自:低調一賤男, 時間:2003-10-25 23:28:00, ID:2252869 | 編輯

            首先正式開始第一講 <掃雷游戲的邏輯>

            我的設想是,掃雷的地圖一般是一個矩形,因為,圓形屏幕的手機看起來蠻變態的,沒有必要遷就他,所以,我用一個a*b的二維數組就完全可以表示整個地圖。

            有了地圖以后地圖里面的類容自然就有一部分是表示地雷啦,既然這樣,那不如就這樣<廢話來的,小朋友不要學>

            /**
            * 20 標志該位置為地雷
            * <=10的數字表示未翻開的方塊及周圍的地雷數目
            * >=10的數字表示已翻開的方塊及周圍的地雷數目
            **/

            表示方法就出來了,邏輯也明朗起來了。

            我要將某個塊翻開,只要將他加上10就可以了。

            Java編程第一步,當然是先要class啊
              1 
              2 package games;
              3 
              4 
              5 import java.util.Random;
              6 import java.lang.Math;
              7 
              8 class gamelogic {
              9 
             10   /** 表示一個10*10的棋盤 */
             11   private int[][] pan = new int;
             12   private Random random;// 一個隨機變量,主要作用是用來指定哪些位置為地雷
             13   private int BombNum = 0// 統計地雷總數
             14 
             15 
             16 
             17   /** 游戲是否結束 */
             18   private boolean GameOver;
             19 
             20 
             21 
             22 /*
             23  * 接下來就是要初始化地圖了,地圖首先要扔一個雷在上面啊,不然怎么叫掃雷呢,扔完了地雷以后接下來當然是遍歷一次地圖(我們還是很仁慈d,我們得告訴掃雷d
             24  * 同志,某某位置,有多少雷,比如這樣:"01、01、12點中方向有地雷,14點鐘方向有幺雞,2點鐘方向有東風之類的啊")。
             25  */
             26 
             27 /** 初始化數組,生成地圖 */
             28 public void InitArray() {
             29   for (int i = 0; i < 8; i++) {
             30     for (int j = 0; j < 8; j++) {
             31       pan[i][j] = 0;
             32     }
             33   }
             34 
             35   RandomArray();
             36   CountBomb();
             37   BombNum = Bomb();
             38 }
             39 
             40 
             41 /**
             42  * 統計地雷總數
             43  * 
             44  * @return int 返回地雷總數
             45  */
             46 private int Bomb() {
             47   int count = 0;
             48   for (int i = 0; i < 8; i++) {
             49     for (int j = 0; j < 8; j++) {
             50       if (pan[i][j] == 20) {
             51         count += 1;
             52       }
             53     }
             54   }
             55 
             56   return count;
             57 }
             58 
             59 
             60 /** 隨機決定地雷的位置 */
             61 private void RandomArray() {
             62   int i, j, k;
             63 
             64   // 先扔15個左右的地雷吧,注意,這里不一定有15個哦,因為隨機值可能重復,我不管啦
             65   for (int r = 0; r < 15; r++) {
             66     k = java.lang.Math.abs(random.nextInt()) % 64// random.nextInt(100);
             67     i = k / 8;
             68     j = k % 8;
             69     this.pan[i][j] = 20// 指定該位置為地雷
             70   }
             71 
             72 }
             73 
             74 
             75 /** 統計棋盤上的數據 */
             76 private void CountBomb() {
             77   for (int i = 0; i < 8; i++) {
             78     for (int j = 0; j < 8; j++) {
             79       int count = 0;
             80 
             81       // 當需要檢測的單元格本身無地雷的情況下,統計周圍的地雷個數
             82       if (pan[i][j] != 20) {
             83         if ( (i - 1 >= 0&& (j - 1 >= 0)) {
             84           if (pan[i - 1][j - 1== 20) {
             85             count += 1// 檢測左上方空格是否是地雷
             86           }
             87         }
             88 
             89         if ( (i - 1 >= 0)) {
             90           if (pan[i - 1][j] == 20) {
             91             count += 1// 檢測上方空格是否為地雷
             92           }
             93         }
             94 
             95         if ( (i - 1 >= 0&& (j + 1 <= 7)) {
             96           if (pan[i - 1][j + 1== 20) {
             97             count += 1// 檢測右上方是否為地雷
             98           }
             99         }
            100 
            101         if ( (j - 1 >= 0)) {
            102           if (pan[i][j - 1== 20) {
            103             count += 1// 檢測左邊是否為地雷
            104           }
            105         }
            106 
            107         if ( (i >= 0&& (j + 1 <= 7)) {
            108           if (pan[i][j + 1== 20) {
            109             count += 1// 右邊
            110           }
            111         }
            112 
            113         if ( (j - 1 >= 0&& (i + 1 <= 7)) {
            114           if (pan[i + 1][j - 1== 20) {
            115             count += 1// 左下
            116           }
            117         }
            118 
            119         if ( (i + 1 <= 7)) {
            120           if (pan[i + 1][j] == 20) {
            121             count += 1// 下
            122           }
            123         }
            124 
            125         if ( (j + 1 <= 7&& (i + 1 <= 7)) {
            126           if (pan[i + 1][j + 1== 20) {
            127             count += 1// 右下
            128           }
            129         }
            130 
            131         pan[i][j] = count;
            132       }
            133     }
            134   }
            135 }
            136 
            137 
            138 
            139 /**
            140  * 檢測已經被揭開的位置總和
            141  * 
            142  * @return 返回被揭開的數量
            143  */
            144 private int countOpen() {
            145   int count = 0;
            146   for (int i = 0; i < 8; i++) {
            147     for (int j = 0; j < 8; j++) {
            148       if (pan[i][j] < 20 && pan[i][j] > 9) {
            149         count += 1;
            150       }
            151     }
            152   }
            153 
            154   return count;
            155 }
            156 
            157 
            158 
            159 /**
            160  * 檢測是否勝利
            161  * 
            162  * @return 是否勝利boolean值
            163  */
            164 public boolean isWin() {
            165   // System.out.println(BombNum +""+ countOpen());
            166   if ( (BombNum + countOpen()) == 64) {
            167     this.GameOver = true;
            168     return true;
            169   }
            170   else {
            171     return false;
            172   }
            173 }
            174 
            175 
            176 /**
            177  * 選中棋盤上的位置,并翻開
            178  * 
            179  * @param matrix
            180  *            位置
            181  */
            182 public void openpan(int matrix) {
            183   switch (getBomb(matrix)) {
            184     case 20// 當選中的位置為地雷,游戲結束
            185       setGameOver();
            186       break;
            187     case 0:
            188       isNull(matrix); // 當選中的位置為空,則翻開周圍的地圖
            189       break;
            190     default:
            191       this.isNotNull(matrix); // 否則,翻開當前位置,并作上翻開的標記
            192   }
            193 }
            194 
            195 
            196 
            197 /**
            198  * 當選中的位置為空,則翻開周圍的地圖
            199  * 
            200  * @param matrix
            201  *            位置
            202  */
            203 private void isNull(int matrix) {
            204   int i, j;
            205   i = matrix / 8;
            206   j = matrix % 8;
            207 
            208   if (pan[i][j] < 9) {
            209     pan[i][j] += 10;
            210   }
            211 
            212   if ( (i - 1 >= 0&& (j - 1 >= 0)) { // 檢測左上方空格是否是空
            213     if (pan[i - 1][j - 1== 0) {
            214       isNull( (i - 1* 8 + (j - 1));
            215     }
            216     if (pan[i - 1][j - 1< 9) {
            217       pan[i - 1][j - 1+= 10;
            218     }
            219   }
            220 
            221   if ( (i - 1 >= 0)) { // 檢測上方空格是否為空
            222     if (pan[i - 1][j] == 0) {
            223       isNull( (i - 1* 8 + j);
            224     }
            225     if (pan[i - 1][j] < 9) {
            226       pan[i - 1][j] += 10;
            227     }
            228   }
            229 
            230   if ( (i - 1 >= 0&& (j + 1 <= 7)) { // 檢測右上方是否為空
            231     if (pan[i - 1][j + 1== 0) {
            232       isNull( (i - 1* 8 + (j + 1));
            233     }
            234     if (pan[i - 1][j + 1< 9) {
            235       pan[i - 1][j + 1+= 10;
            236     }
            237   }
            238 
            239   if ( (j - 1 >= 0)) { // 檢測左邊是否為空
            240     if (pan[i][j - 1== 0) {
            241       isNull(i * 8 + (j - 1));
            242     }
            243     if (pan[i][j - 1< 9) {
            244       pan[i][j - 1+= 10;
            245     }
            246   }
            247 
            248   if ( (i >= 0&& (j + 1 <= 7)) { // 右邊
            249     if (pan[i][j + 1== 0) {
            250       isNull(i * 8 + (j + 1));
            251     }
            252     if (pan[i][j + 1< 9) {
            253       pan[i][j + 1+= 10;
            254     }
            255   }
            256 
            257   if ( (j - 1 >= 0&& (i + 1 <= 7)) { // 左下
            258     if (pan[i + 1][j - 1== 0) {
            259       isNull( (i + 1* 8 + (j - 1));
            260     }
            261     if (pan[i + 1][j - 1< 9) {
            262       pan[i + 1][j - 1+= 10;
            263     }
            264   }
            265 
            266   if ( (i + 1 <= 7)) { // 下
            267     if (pan[i + 1][j] == 0) {
            268       isNull( (i + 1* 8 + j);
            269     }
            270     if (pan[i + 1][j] < 9) {
            271       pan[i + 1][j] += 10;
            272     }
            273   }
            274 
            275   if ( (j + 1 <= 7&& (i + 1 <= 7)) { // 右下
            276     if (pan[i + 1][j + 1== 0) {
            277       isNull( (i + 1* 8 + (j + 1));
            278     }
            279     if (pan[i + 1][j + 1< 9) {
            280       pan[i + 1][j + 1+= 10;
            281     }
            282   }
            283 }
            284 
            285 
            286 
            287 /**
            288  * 選中棋盤上的位置,并翻開當前位置
            289  * 
            290  * @param matrix
            291  *            位置
            292  */
            293 private void isNotNull(int matrix) {
            294   int i, j;
            295   i = matrix / 8;
            296   j = matrix % 8;
            297   pan[i][j] += 10;
            298 }
            299 
            300 
            301 /**
            302  * 取得指定位置的數據
            303  * 
            304  * @param matrix
            305  *            位置
            306  * @return int 數據
            307  */
            308 public int getBomb(int matrix) {
            309   int i, j;
            310   i = matrix / 8;
            311   j = matrix % 8;
            312   return this.pan[i][j];
            313 }
            314 
            315 
            316 /**
            317  * 檢測游戲是否結束
            318  * 
            319  * @return boolean 游戲是否結束的狀態
            320  */
            321 public boolean isGameOver() {
            322   return GameOver;
            323 }
            324 
            325 
            326 /** 設置游戲結束 */
            327 private void setGameOver() {
            328   GameOver = true;
            329 }
            330 
            331 
            332 /** 開新局 */
            333 
            334 public void setNewGame() {
            335   this.GameOver = false;
            336 }
            337 
            338 
            339 /**
            340  * 指定位置是否被揭開
            341  * 
            342  * @param matrix
            343  *            位置
            344  * @return boolean 返回是否可以被揭開
            345  */
            346 public boolean isFree(int matrix) {
            347   int i, j;
            348   i = matrix / 8;
            349   j = matrix % 8;
            350   return pan[i][j] < 8 || pan[i][j] == 20;
            351 }
            352 
            353 }
            354 


            來自:低調一賤男, 時間:2003-10-25 23:46:00, ID:2252881 | 編輯

            public void openpan(int matrix) 是整個程序的核心所在,他描述了所有的動作,
            1、有可能你踩到屎,踩到屎當然就gameover啦
            2、有可能踩到錢,有一大片空地給你玩
            3、命好,沒有炸死你,繼續探索



            來自:低調一賤男, 時間:2003-10-25 23:47:00, ID:2252882 | 編輯

            呵呵,第一講先到這里,下次講界面的實現


            來自:低調一賤男, 時間:2003-10-27 17:38:00, ID:2255425 | 編輯

            今天考試,所以現在才開始第二講,見諒,不過好像感興趣的人不多,所以,我講完這個例子就停了,如轉載請注明 作者


            來自:yanyandt2, 時間:2003-10-27 17:45:00, ID:2255447

            請問你這個東西我在PC上能實現嗎?
            我最近也想學java,可是我不知道能用java來做什么。
            jbuilder太大,我的機器跑不動,
            做網頁又不喜歡,
            還希望樓主能給迷茫的我指導一下,謝謝!!


            來自:低調一賤男, 時間:2003-10-27 17:57:00, ID:2255484 | 編輯

            用jbuilder配置j2me比較方便,幾乎不用配置,用記事本也可以寫,用命令行運行,但是要裝j2se,j2ee,然后配置一堆環境變量,然后還要用命令行來編譯,審核,運行,一個基本上會死人,這也是我不用的原因,呵呵,具體步驟你到 http://www.cnjm.net/ 上去看看


            來自:低調一賤男, 時間:2003-10-28 14:19:00, ID:2257384 | 編輯

            呵呵,無人問津就只貼代碼算了,搞到我都沒有動力
              1 //gamecCanvans.java

              2 package games;
              3 import java.lang.System.*;
              4 import java.util.Random;
              5 import java.util.Vector;
              6 import javax.microedition.midlet.*;
              7 import javax.microedition.lcdui.*;
              8 
              9 
             10 
             11 /**
             12 * 標題: Canvas測試
             13 * 描述: 游戲的界面
             14 * 版權: 2003
             15 * 公司: none
                * 
            @author 劉昆
             16 @version 1.0*/
             17 
             18 
             19 /**游戲動作類*/
             20 class gameCanvas extends Canvas
             21 
             22 implements CommandListener {
             23 
             24   /***/
             25   private static final int BLACK = 0x00000000;
             26 
             27   /***/
             28   private static final int WHITE = 0x00FFFFFF;
             29 
             30   /***/
             31   private static final int RED = 0x00FF0000;
             32 
             33   /***/
             34   private static final int BLUE = 0x000000FF;
             35 
             36   /**沒有移動的標志*/
             37   private static final int NO_MOVE = -1;
             38 
             39   /**主窗體類*/
             40   private final control midlet;
             41 
             42   /**邏輯類*/
             43   private final gamelogic game;
             44 
             45   /**退出菜單*/
             46   private final Command exitCommand;
             47 
             48   /**重新開始菜單*/
             49   private final Command newGameCommand;
             50 
             51   /**隨機數*/
             52   private final Random random = new Random();
             53 
             54   /**屏幕寬度*/
             55   private int screenWidth;
             56 
             57   /**屏幕高度*/
             58   private int screenHeight;
             59 
             60 
             61   /**boardCellSize 正方形單元格的大小,
             62   * boardTop 棋盤top的位置,
             63   * boardLeft 棋盤left位置*/
             64   private int boardCellSize, boardTop, boardLeft;
             65 
             66 
             67   /**preCursorPosition 前一次光標選擇的位置,cursorPosition 當前光標選擇的位置*/
             68   private int preCursorPosition, cursorPosition;
             69 
             70   /**用于存儲被標記的地雷的位置 */
             71   private Vector BombVector = new Vector();
             72 
             73   private boolean isRestart;
             74 
             75 
             76   /**構造器
             77   * @param midlet 主窗體類 */
             78   public gameCanvas(control midlet) {
             79     this.midlet = midlet;
             80     game = new gamelogic(random);
             81     initializeBoard();
             82 
             83     /**初始化菜單*/
             84     exitCommand = new Command("退出", Command.EXIT, 1);
             85     newGameCommand = new Command("新游戲", Command.SCREEN, 2);
             86     addCommand(exitCommand);
             87     addCommand(newGameCommand);
             88     setCommandListener(this);
             89 
             90     /**開始游戲*/
             91     initialize();
             92   }
             93 
             94 
             95 
             96   /**添加一個地雷位置標記
             97   *@param matrix 位置標記 */
             98   private void addBomb(int matrix) {
             99     BombVector.addElement(Integer.toString(matrix));
            100   }
            101 
            102 
            103 
            104   /**刪除一個地雷位置標記
            105   *@param matrix 位置標記 */
            106   private void delBomb(int matrix) {
            107     BombVector.removeElement(Integer.toString(matrix));
            108   }
            109 
            110 
            111 
            112   /**搜索該位置是否被標記
            113   * @param matrix 位置標記
            114   * @return boolean 該位置是否被記錄,false為被記錄 */
            115   private boolean searchBomb(int matrix) {
            116     return BombVector.indexOf(Integer.toString(matrix)) == -1//-1表示沒有找到該位置的信息
            117   }
            118 
            119 
            120   /**初始化屏幕,取得棋盤的初始位置*/
            121   private void initializeBoard() {
            122     screenWidth = getWidth(); //取得屏幕寬度
            123     screenHeight = getHeight(); //取得屏幕高度
            124     if (screenWidth > screenHeight) {
            125       boardCellSize = (screenHeight - 1/ 8;
            126       boardLeft = (screenWidth - (boardCellSize * 8)) / 2;
            127       boardTop = 1;
            128     }
            129     else {
            130       boardCellSize = (screenWidth - 1/ 8;
            131       boardLeft = 1;
            132       boardTop = (screenHeight - boardCellSize * 8/ 2;
            133     }
            134   }
            135 
            136 
            137 
            138   /** 初始化游戲和屏幕. 使游戲重新啟動*/
            139   private void initialize() {
            140     preCursorPosition = cursorPosition = 0;
            141     game.setNewGame();
            142     game.InitArray();
            143     isRestart = true;
            144     BombVector.removeAllElements();
            145     repaint();
            146   }
            147 
            148 
            149 
            150   /**重畫canvas
            151   * @param g 重畫的Graphics對象*/
            152   public void paint(Graphics g) {
            153     game.isWin();
            154 
            155     if (!game.isGameOver()) {
            156       paintGame(g);
            157     }
            158     else {
            159       paintGameOver(g);
            160     }
            161   }
            162 
            163 
            164 
            165   /**游戲未結束時的重畫動作
            166   * @param g 重畫的Graphics對象*/
            167   private void paintGame(Graphics g) {
            168     if (isRestart) {
            169       /*清除畫板*/
            170       g.setColor(0xbbbbbb);
            171       g.fillRect(00, screenWidth, screenHeight);
            172       drawBoard(g);
            173       paintAll(g);
            174       //System.out.println("sss");//test
            175       drawBombTag(g);
            176     }
            177 
            178     drawCursor(g);
            179   }
            180 
            181 
            182 
            183   /**游戲結束時的重畫動作,畫出游戲統計結果
            184   * @param g 重畫的Graphics對象 */
            185   private void paintGameOver(Graphics g) {
            186     if (game.isGameOver()) {
            187       if (game.isWin()) {
            188         GameOver(g);
            189       }
            190       else {
            191         for (int i = 0; i < 8; i++) {
            192           for (int j = 0; j < 8; j++) {
            193             if (game.getBomb(i * 8 + j) == 20) {
            194               drawCircle(g, i * 8 + j);
            195             }
            196           }
            197         }
            198       }
            199     }
            200   }
            201 
            202 
            203 
            204   /**游戲結束時的重畫動作
            205   * @param g 重畫的Graphics對象 */
            206   private void GameOver(Graphics g) {
            207 
            208     String tallyMsg = "你贏了,恭喜!";
            209     Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,Font.SIZE_LARGE);
            210     int strHeight = font.getHeight();
            211     int tallyMsgWidth = font.stringWidth(tallyMsg);
            212     int strWidth = tallyMsgWidth;
            213 
            214     /*計算繪制文本的起始位置*/
            215     int x = (screenWidth - strWidth) / 2;
            216     x = x < 0 ? 0 : x;
            217     int y = (screenHeight - 2 * strHeight) / 2;
            218     y = y < 0 ? 0 : y;
            219 
            220     /* 清除畫板*/
            221     g.setColor(WHITE);
            222     g.fillRect(00, screenWidth, screenHeight);
            223 
            224     /* 畫出文本結果*/
            225     g.setColor(RED);
            226     g.drawString(tallyMsg, x + 5, (y + 1 + strHeight),
                                    (Graphics.TOP 
            | Graphics.LEFT));
            227   }
            228 
            229 
            230 
            231   /**監聽器響應接口
            232   * @param c 命令
            233   * @param d 消息源 */
            234   public void commandAction(Command c, Displayable d) {
            235     if (c == exitCommand) {
            236       midlet.quit();
            237     }
            238     else if (c == newGameCommand) {
            239       initialize();
            240     }
            241   }
            242 
            243 
            244 
            245   /** 畫圓,表示地雷位置
            246   * @param g 圖形物件
            247   * @param matrix 位置*/
            248   private void drawCircle(Graphics g, int matrix) {
            249     int x, y;
            250     y = matrix / 8;
            251     x = matrix % 8;
            252     g.setColor(RED);
            253     g.fillArc(x * boardCellSize + boardLeft + 3, y * boardCellSize + boardTop + 2,
                                 boardCellSize 
            - 2, boardCellSize - 20360);
            254   }
            255 
            256 
            257 
            258   /** 畫叉,表示地雷爆炸
            259   * @param g 圖形物件
            260   * @param x x坐標
            261   * @param y y坐標 */
            262   private void drawCross(Graphics g, int x, int y) {
            263     g.setColor(RED);
            264     g.drawLine(x + 1 + boardLeft, y + boardTop,
                                 x 
            + boardCellSize - 4 - 4 + boardLeft,
                                 y 
            + boardCellSize - 5 + boardTop);
            265     g.drawLine(x + 1 + boardLeft,
                                 y 
            + boardCellSize - 5 + boardTop,
                                 x 
            + boardCellSize - 4 - 4 + boardLeft, y + boardTop);
            266   }
            267 
            268 
            269 
            270   /**根據玩家和電腦的移動重畫棋盤*/
            271   private void doPlayerMove() {
            272     if (game.isFree(cursorPosition)) {
            273       game.openpan(cursorPosition);
            274     }
            275     repaint(); //畫出移動的圖形結果
            276   }
            277 
            278 
            279 
            280   /**重畫所有被揭開的文字
            281   * @param g 圖形物件 */
            282   private void paintAll(Graphics g) {
            283     for (int i = 0; i < 8; i++) {
            284       for (int j = 0; j < 8; j++) {
            285         if (game.getBomb(i * 8 + j) >= 10 && game.getBomb(i * 8 + j) < 20) {
            286           paintNum(g, i * 8 + j);
            287         }
            288       }
            289     }
            290   }
            291 
            292 
            293 
            294   /**畫出文字
            295   * @param g 圖形物件
            296   * @param matrix 位置 */
            297   private void paintNum(Graphics g, int matrix) {
            298     int i, j, s;
            299     Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
            300     s = game.getBomb(matrix);
            301     //System.out.println(s);
            302     i = matrix / 8;
            303     j = matrix % 8;
            304     g.setColor(WHITE);
            305     if (this.searchBomb(matrix)) {
            306       if (s != 20) {
            307         g.drawString(String.valueOf(s - 10),
                                           boardLeft 
            + j * boardCellSize + 5,
                                           boardTop 
            + i * boardCellSize - 2,
                                           (Graphics.TOP 
            | Graphics.LEFT));
            308       }
            309     }
            310   }
            311 
            312 
            313 
            314   /**捕獲按鍵消息
            315   * @param keyCode 按鍵代碼 */
            316   protected void keyPressed(int keyCode) {
            317     /**當游戲結束時返回*/
            318     if (game.isGameOver()) {
            319       return;
            320     }
            321 
            322     int gameAction = getGameAction(keyCode);
            323 
            324     switch (gameAction) {
            325       case FIRE:
            326         if (searchBomb(cursorPosition)) { //如果該位置被做出了地雷標志,則該位置不能被揭開
            327           doPlayerMove();
            328         }
            329         break;
            330       case RIGHT:
            331         doMoveCursor(10);
            332         break;
            333       case DOWN:
            334         doMoveCursor(01);
            335         break;
            336       case LEFT:
            337         doMoveCursor( -10);
            338         break;
            339       case UP:
            340         doMoveCursor(0-1);
            341         break;
            342       default:
            343         if (searchBomb(cursorPosition)) {
            344           addBomb(cursorPosition);
            345         }
            346         else {
            347           delBomb(cursorPosition);
            348         }
            349         repaint();
            350         break;
            351     }
            352   }
            353 
            354 
            355 
            356   /**畫出棋盤上的地雷標志
            357   * @param g 圖形物件 */
            358   private void drawBombTag(Graphics g) {
            359     int s, j, k;
            360     g.setColor(RED);
            361     for (int i = 0; i < BombVector.size(); i++) {
            362       s = Integer.parseInt( (String) BombVector.elementAt(i));
            363       j = s % 8;
            364       k = s / 8;
            365       g.drawString("!", boardLeft + j * boardCellSize + 7,
                                        boardTop 
            + k * boardCellSize - 2,
                                        (Graphics.TOP 
            | Graphics.LEFT));
            366     }
            367   }
            368 
            369 
            370 
            371   /**指定棋盤上的坐標
            372   * @param dx 左右偏移量x
            373   * @param dy 上下偏移量y */
            374   private void doMoveCursor(int dx, int dy) {
            375 
            376     int newCursorPosition = cursorPosition + dx + 8 * dy;
            377     if ( (newCursorPosition >= 0&& (newCursorPosition < 64)) {
            378       preCursorPosition = cursorPosition;
            379       cursorPosition = newCursorPosition;
            380       repaint();
            381     }
            382   }
            383 
            384 
            385 
            386   /** 在棋盤上畫出選擇光標
            387   * @param g 圖形物件 */
            388   private void drawCursor(Graphics g) {
            389 
            390     /** 清除之前的選擇光標*/
            391     g.setColor(0xbbbbbb);
            392     g.drawRect( ( (preCursorPosition % 8* boardCellSize) + 3 + boardLeft,
                                   ( (preCursorPosition 
            / 8* boardCellSize) + 1 + boardTop,
                                   boardCellSize 
            - 2, boardCellSize - 2);
            393 
            394     /**在當前選擇位置畫出選擇光標*/
            395     g.setColor(this.BLUE);
            396     g.drawRect( ( (cursorPosition % 8* boardCellSize) + 3 + boardLeft,
                                   ( (cursorPosition 
            / 8* boardCellSize) + 1 + boardTop,
                                   boardCellSize 
            - 2, boardCellSize - 2);
            397   }
            398 
            399 
            400 
            401   /** 畫出空位置
            402   * @param g 圖形物件 */
            403   private void drawNull(Graphics g) {
            404 
            405     /**在當前選擇位置畫出選擇光標*/
            406     g.setColor(this.WHITE);
            407     g.fillRect( ( (cursorPosition % 8* boardCellSize) + 3 + boardLeft,
                                   ( (cursorPosition 
            / 8* boardCellSize) + 1 + boardTop,
                                   boardCellSize 
            - 2, boardCellSize - 2);
            408   }
            409 
            410 
            411 
            412   /**畫一個盤
            413   * @param g 圖形物件 */
            414   private void drawBoard(Graphics g) {
            415 
            416     /** 清除盤*/
            417     g.setColor(0xbbbbbb);
            418     g.fillRect(00, screenWidth, screenHeight);
            419 
            420     /**畫盤*/
            421     g.setColor(BLACK);
            422     for (int i = 0; i < 9; i++) {
            423       g.fillRect(boardLeft + 2, boardCellSize * i + boardTop,
                                    (boardCellSize 
            * 8+ 11); /*畫四條黑色的橫線*/
            424       g.fillRect(boardCellSize * i + boardLeft + 2,
                                     boardTop, 
            1, boardCellSize * 8); /*畫四條黑色的豎線*/
            425     }
            426   }
            427 }
            428 



            來自:低調一賤男, 時間:2003-10-28 14:22:00, ID:2257393 | 編輯


             1 package games;
             2 
             3 import java.io.IOException;
             4 import javax.microedition.midlet.*;
             5 import javax.microedition.lcdui.*;
             6 import javax.microedition.io.*;
             7 
             8 
             9 
            10 public class control extends MIDlet {
            11 
            12   private gameCanvas gameScreen;
            13 
            14   /**構造器*/
            15   public control() {
            16   }
            17 
            18 
            19   /**開始MIDlet*/
            20   public void startApp() {
            21     Displayable current = Display.getDisplay(this).getCurrent();
            22     if (current == null) {
            23       Image logo = null;
            24       try {
            25         logo = Image.createImage("/icons/mine.png");
            26       }
            27       catch (IOException e) {
            28         System.err.println("none image");
            29       }
            30 
            31       Alert splashScreen = new Alert(null,
            32 
            33       " 掃雷 1.0 \n 劉昆制作 \n eMail:liukun966122@hotmail.com", logo,
            34 
            35       AlertType.INFO); //顯示一個logo
            36       splashScreen.setTimeout(4000);
            37 
            38       gameScreen = new gameCanvas(this);
            39 
            40       Display.getDisplay(this).setCurrent(splashScreen, gameScreen);
            41     }
            42     else {
            43       Display.getDisplay(this).setCurrent(current);
            44     }
            45   }
            46 
            47 
            48 
            49   /*暫停MIDlet*/
            50   public void pauseApp() {}
            51 
            52   /**銷毀MIDlet中需要銷毀的對象
            53   *@param unconditional 是否被立即銷毀 */
            54   public void destroyApp(boolean unconditional) {}
            55 
            56 
            57 
            58   /**退出MIDlet*/
            59 
            60   public void quit() {
            61     destroyApp(false);
            62     notifyDestroyed();
            63   }
            64 
            65 }

            posted on 2006-02-05 21:44 Khan 閱讀(1240) 評論(4)  編輯 收藏 引用 所屬分類: 跨平臺開發Java

            評論

            # re: 掃雷游戲的設計(Kjava 原創)  回復  更多評論   

            hao
            2006-05-15 20:35 | jav

            # re: 掃雷游戲的設計(Kjava 原創)  回復  更多評論   

            謝謝!
            已收藏,慢慢研究。

            下面是一個很久以前開發的基于Swing的掃雷:
            http://gro.clinux.org/projects/jmine
            2008-01-18 14:42 | RatKing

            # re: 掃雷游戲的設計(Kjava 原創)  回復  更多評論   

            嘿嘿. 這個好像是5年前寫的
            2008-03-06 17:23 | Khan's Notebook

            # re: 掃雷游戲的設計(Kjava 原創)  回復  更多評論   

            能比能下載您的代碼啊?
            2013-06-20 12:43 | cv
            久久综合亚洲鲁鲁五月天| 狠狠色丁香婷婷综合久久来 | 久久99精品九九九久久婷婷| 久久伊人精品青青草原高清| 久久久久久一区国产精品| 久久久噜噜噜久久中文字幕色伊伊 | 国产精品久久新婚兰兰| 亚洲国产精品一区二区久久hs| 国产精品美女久久久久| 色综合久久88色综合天天 | 色婷婷综合久久久中文字幕 | 久久精品午夜一区二区福利| 国产高清美女一级a毛片久久w| 久久精品国产精品亚洲精品| 99久久精品免费观看国产| 久久久久久久波多野结衣高潮 | 久久午夜综合久久| 国产精品久久免费| 久久热这里只有精品在线观看| 91久久精品无码一区二区毛片| 亚洲中文精品久久久久久不卡| 久久精品国产欧美日韩| 久久九九青青国产精品| A狠狠久久蜜臀婷色中文网| 伊人久久成人成综合网222| 国产日韩久久久精品影院首页| 亚洲AV无一区二区三区久久| 亚洲午夜久久久| 久久综合亚洲色HEZYO国产| 国产精品久久久久久久午夜片| 国产成人精品免费久久久久| 色婷婷综合久久久久中文| 亚洲欧美日韩中文久久| 久久天天躁狠狠躁夜夜2020一| 一级女性全黄久久生活片免费 | 亚洲欧美久久久久9999| 91精品国产色综久久| 岛国搬运www久久| 国产精品成人久久久久三级午夜电影 | 久久综合给久久狠狠97色| 久久无码高潮喷水|