首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> J2ME

Eclipse 開發 J2ME 手機遊戲入門講座(六):打造圖形化井字遊戲「花皮貓大戰流氓兔」

     編輯:J2ME

第六講:打造圖形化手機遊戲

遊戲設計規劃

  • 類型:人機對弈(井字棋變體)
  • 流程:封面動畫(4秒) → 角色選擇(貓/兔) → 遊戲主循環 → 勝負判定 → 戰績顯示 → 重玩/退出
  • 規則:3x3 棋盤,雙方輪流落子,橫/直/斜連線三子獲勝,滿盤平局。

專案架構

  • 專案建立 → 新增 MIDlet TicTacToe
  • 新增三個類別:ChoosePieceScreen、GameScreen、Game
  • 資源管理:PNG 圖片透過「連結至檔案系統」加入專案
    新增檔案對話框

封面與角色選擇實作

// 封面顯示 (startApp 中)
Image logo = null;
try { logo = Image.createImage("/logo.png"); }
catch (IOException e) {}
Alert splash = new Alert(null, "郎銳2004年作\n版權所有(c)\n2004--2005", logo, AlertType.INFO);
splash.setTimeout(4000);  // 4 秒後自動切換

// 角色選擇畫面
choosePieceScreen = new ChoosePieceScreen(this);
Display.getDisplay(this).setCurrent(splash, choosePieceScreen);

ChoosePieceScreen 繼承 List (IMPLICIT 模式),append(文字, 圖片) 建立選項,setCommandListener(this) 監聽選擇,commandAction 中以 getSelectedIndex() 判斷選擇貓/兔,呼叫 midlet.choosePieceScreenDone(isPlayerCat) 進入遊戲。

截圖示意:
封面 角色選擇 遊戲中 戰績顯示

遊戲畫面繪製 (GameScreen extends Canvas)

棋盤幾何計算

screenWidth = getWidth();
screenHeight = getHeight();
if (screenWidth > screenHeight) {
    boardCellSize = (screenHeight - 2) / 3;
    boardLeft = (screenWidth - boardCellSize * 3) / 2;
    boardTop = 1;
} else {
    boardCellSize = (screenWidth - 2) / 3;
    boardLeft = 1;
    boardTop = (screenHeight - boardCellSize * 3) / 2;
}

繪製棋盤網格

g.setColor(WHITE);
g.fillRect(0, 0, screenWidth, screenHeight);
g.setColor(BLACK);
for (int i = 0; i < 4; i++) {
    g.fillRect(boardLeft, boardCellSize*i + boardTop, boardCellSize*3 + 2, 2); // 橫線
    g.fillRect(boardCellSize*i + boardLeft, boardTop, 2, boardCellSize*3);     // 直線
}

繪製棋子 (以貓為例)

private void drawCat(Graphics g, int x, int y) {
    Image img = null;
    try { img = Image.createImage("/cat.png"); }
    catch (Exception e) {}
    g.drawImage(img, x + 1, y + 1, 0);
}

游標移動處理

目標格繪製緊貼網格的黑框 → 原位置依狀態重繪(空白填白、有子重繪子圖),消除殘影。

戰績顯示 (遊戲結束)

Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
int h = font.getHeight();
int w1 = font.stringWidth(statusMsg), w2 = font.stringWidth(tallyMsg);
int x = (screenWidth - Math.max(w1,w2)) / 2; if (x < 0) x = 0;
int y = (screenHeight - 2*h) / 2; if (y < 0) y = 0;
g.setColor(WHITE); g.fillRect(0,0,screenWidth,screenHeight);
g.setColor(BLACK);
g.drawString(statusMsg, x, y, Graphics.TOP|Graphics.LEFT);
g.drawString(tallyMsg, x, y+h+1, Graphics.TOP|Graphics.LEFT);

人工智能核心 (Game 類別)

棋盤 9 格編號 0-8,獲勝組合 8 種:{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6},以 WINS[][] 陣列記錄。

電腦落子策略

int move = getWinningComputerMove();      // 能贏就贏
if (move == -1) {
    move = getRequiredBlockingComputerMove(); // 擋住玩家贏
    if (move == -1)
        move = getRandomComputerMove();       // 隨手棋
}
computerState |= bit(move);

獲勝檢測演算法

int getWinningComputerMove() {
    for (int i = 0; i < 9; i++)
        if (isFree(i) && isWin(computerState | bit(i)))
            return i;
    return -1;
}

getRequiredBlockingComputerMove 邏輯相同,改檢查 playerState。核心僅數十行,展現 AI 並不神秘。

開發環境總結

  • OS: Windows 2000 Pro + SP4
  • JDK: Java 2 SDK 1.5.0
  • WTK: 2.1 + SonyEricsson J2ME SDK (WTK 1.0.4)
  • 手機: SonyEricsson T628
  • IDE: Eclipse 3.0.1 + EclipseMe 0.5.5 + NLpack

啟發與延伸

本框架通用性強,替換遊戲劇本即可實現「華容道」、「俄羅斯方塊」等類似遊戲。J2ME 遊戲開發的門檻已打破,關鍵在於持續精進創意與技術。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved