?? gamescreen.java
字號:
/*
* 創建日期 2005-6-29
*
* TODO 要更改此生成的文件的模板,請轉至
* 窗口 - 首選項 - Java - 代碼樣式 - 代碼模板
*/
package game;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDletStateChangeException;
/**
* @author Administrator
*
* TODO 要更改此生成的類型注釋的模板,請轉至
* 窗口 - 首選項 - Java - 代碼樣式 - 代碼模板
*/
public class GameScreen extends Canvas implements CommandListener {
private static final int BLACK = 0x00000000;
private static final int WHITE = 0x00FFFFFF;
private static final int RED = 0x00FF0000;
private static final int BLUE = 0x000000FF;
private final HuaRongRoadMIDlet midlet;
private final Command exitCommand;
private final Command newGameCommand;
private int screenWidth, screenHeight;
private int boardCellSize, boardSize, boardTop, boardLeft;
private Image caocao;
private Image guanyu;
private Image huangzhong;
private Image kongbai;
private Image machao;
private Image zhangfei;
private Image zhaoyun;
private Image zu;
private boolean isRestart;
private int preCursorPosition, cursorPosition;
private int fireStep = 0;
private int from = -1;
private int wOld, hOld;
private boolean isGameOver = false;
private int[] ID = { 1, 2, 2, 3, // 地圖
1, 2, 2, 3, // 1:張飛 2:曹操 3:馬超
4, 5, 5, 6, // 4:趙云 5:關羽 6:黃忠
4, 7, 7, 6, // 7:卒 0:空白
7, 0, 0, 7};
private int[] Draw = { 1, 1, 0, 1, // 是否進行繪制處理
0, 0, 0, 0,
1, 1, 0, 1,
0, 1, 1, 0,
1, 1, 1, 1};
private Image[] Pic = {kongbai, zhangfei, caocao, machao, zhaoyun, guanyu, huangzhong, zu};
/**
*
*/
public GameScreen(HuaRongRoadMIDlet midlet) {
this.midlet = midlet;
// 獲取屏幕大小
screenWidth = getWidth();
screenHeight = getHeight();
boardCellSize = (screenHeight - 6) / 5;
boardLeft = (screenWidth - boardCellSize * 4) / 2;
boardTop = 1;
// 添加屏幕命令
exitCommand = new Command("退出", Command.EXIT, 1);
newGameCommand = new Command("開始", Command.SCREEN, 2);
addCommand(exitCommand);
addCommand(newGameCommand);
// 偵聽命令按鍵
setCommandListener(this);
// 初始化屏幕顯示
initialize();
}
private void initialize() {
// 先前行棋位置和當前行棋位置復位
preCursorPosition = cursorPosition = 0;
// 裝載所有位圖片段
try
{
// 裝載圖象片段
caocao = Image.createImage("/caocao.png");
guanyu = Image.createImage("/guanyu.png");
huangzhong = Image.createImage("/huangzhong.png");
kongbai = Image.createImage("/kongbai.png");
machao = Image.createImage("/machao.png");
zhangfei = Image.createImage("/zhangfei.png");
zhaoyun = Image.createImage("/zhaoyun.png");
zu = Image.createImage("/zu.png");
// 將圖片保存到數組
Pic[0] = kongbai;
Pic[1] = zhangfei;
Pic[2] = caocao;
Pic[3] = machao;
Pic[4] = zhaoyun;
Pic[5] = guanyu;
Pic[6] = huangzhong;
Pic[7] = zu;
}
catch (Exception e) {}
// 地圖
ID[0] = 1; ID[1] = 2; ID[2] = 2; ID[3] = 3;
ID[4] = 1; ID[5] = 2; ID[6] = 2; ID[7] = 3;
ID[8] = 4; ID[9] = 5; ID[10] = 5; ID[11] = 6;
ID[12] = 4; ID[13] = 7; ID[14] = 7; ID[15] = 6;
ID[16] = 7; ID[17] = 0; ID[18] = 0; ID[19] = 7;
// 是否進行繪制處理
Draw[0] = 1; Draw[1] = 1; Draw[2] = 0; Draw[3] = 1;
Draw[4] = 0; Draw[5] = 0; Draw[6] = 0; Draw[7] = 0;
Draw[8] = 1; Draw[9] = 1; Draw[10] = 0; Draw[11] = 1;
Draw[12] = 0; Draw[13] = 1; Draw[14] = 1; Draw[15] = 0;
Draw[16] = 1; Draw[17] = 1; Draw[18] = 1; Draw[19] = 1;
// 重繪屏幕
isRestart = true;
isGameOver = false;
repaint();
}
public void paint(Graphics g) {
if (isGameOver == false){
// 是否重繪
if (isRestart) {
// 以白色清空畫布
g.setColor(WHITE);
g.fillRect(0, 0, screenWidth, screenHeight);
// 繪制網格
// 白色清空畫布
g.setColor(WHITE);
g.fillRect(0, 0, screenWidth, screenHeight);
// 黑色繪制網格
g.setColor(BLACK);
// 6橫
for (int i = 0; i < 6; i++)
g.drawLine(boardLeft, boardCellSize * i + boardTop, boardLeft + (boardCellSize * 4), boardCellSize * i + boardTop);
// 5豎
for (int i = 0; i < 5; i++)
g.drawLine(boardCellSize * i + boardLeft, boardTop, boardCellSize * i + boardLeft, boardTop + boardCellSize * 5);
// 邊框
g.drawRect(boardLeft - 1, boardTop - 1, boardCellSize * 4 + 2, boardCellSize * 5 + 2);
for (int i = 0; i < 20; i++)
{
// 在當前位置進行繪制處理
if (Draw[i] == 1)
{
int x = (i % 4) * boardCellSize + 1 + boardLeft;
int y = ((int)(i / 4)) * boardCellSize + boardTop + 1;
g.drawImage(Pic[ID[i]], x, y, 0);
}
}
isRestart = false;
}
// 繪制光標
drawCursor(g);
}
else
paintGameOver(g);
}
private void paintGameOver(Graphics g){
// 狀態信息
String statusMsg = " 游戲結束!";
String tallyMsg = "曹操成功逃脫!";
// 設置字體
Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
int strHeight = font.getHeight();
int statusMsgWidth = font.stringWidth(statusMsg);
int tallyMsgWidth = font.stringWidth(tallyMsg);
int strWidth = tallyMsgWidth;
if (statusMsgWidth > tallyMsgWidth)
strWidth = statusMsgWidth;
// 計算字符繪制位置
int x = (screenWidth - strWidth) / 2;
x = x < 0 ? 0 : x;
int y = (screenHeight - 2 * strHeight) / 2;
y = y < 0 ? 0 : y;
// 白色清空畫布
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 + 1 + strHeight), (Graphics.TOP | Graphics.LEFT));
}
/* (非 Javadoc)
* @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
*/
public void commandAction(Command arg0, Displayable arg1) {
if (arg0 == exitCommand) {
try {
// 退出
midlet.quit();
} catch (MIDletStateChangeException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}
else if (arg0 == newGameCommand) {
// 開始游戲
initialize();
}
}
protected void keyPressed(int keyCode) {
// 游戲結束,不響應按鍵
if (ID[13] == 2 && ID[14] == 2 && ID[17] == 2 && ID[18] == 2)
return;
// 得到按鍵動作
int gameAction = getGameAction(keyCode);
switch (gameAction) {
case FIRE: // 選定
fireStep = fireStep + 1;
if (fireStep == 1){
from = cursorPosition;
preCursorPosition = from;
}
// 重繪
repaint();
break;
case RIGHT: // 右移
doMoveCursor(1, 0);
break;
case DOWN: // 下移
doMoveCursor(0, 1);
break;
case LEFT: // 左移
doMoveCursor(-1, 0);
break;
case UP: // 上移
doMoveCursor(0, -1);
break;
default:
break;
}
}
private void doMoveCursor(int dx, int dy) {
// 計算新位置
int newCursorPosition = cursorPosition + dx + 4 * dy;
// 檢驗當前位置是否為圖片的主索引位置
if (Draw[newCursorPosition] == 0){
// 當前進行的是右移操作
if (dx > 0){
// 新位置是否位于第一列
if (newCursorPosition % 4 > 0){
// 新移動到的位置是否與原位置同屬一個圖片
if (ID[newCursorPosition] == ID[cursorPosition]){
// 繼續右移,使新位置落于其他圖片
newCursorPosition = newCursorPosition + 1;
// 檢驗新點是否合法
if (newCursorPosition < 20){
// 進一步檢測新位置是否落在其他圖片的主索引位置,如不在則調整之
if (Draw[newCursorPosition] == 0)
newCursorPosition = newCursorPosition - 4;
}
}
else{
// 如新點落在其他圖片的非主索引位置則將落點調整至上一行
newCursorPosition = newCursorPosition - 4;
}
}
else{
// 如新點落在第一列且為非主索引位置,則將落點調整至下一行
if (Draw[newCursorPosition] == 0)
newCursorPosition = newCursorPosition + 4;
}
}
// 當前進行的是左移操作
if (dx < 0){
// 當前落點是否落在曹操圖片上
if (ID[newCursorPosition] == 2){
// 檢驗當前落點是否在第一行
if (newCursorPosition >= 5){
// 判斷當前圖片的主索引點與曹操主索引點的相對位置
if (ID[newCursorPosition - 5] == 2)
newCursorPosition = newCursorPosition - 5;
else
newCursorPosition = newCursorPosition - 1;
}
else
newCursorPosition = newCursorPosition - 1;
}
else{
// 檢驗新落點與其上一行是否同屬一個圖片,如是則將落點移到上一行
if (ID[newCursorPosition] == ID[newCursorPosition - 4])
newCursorPosition = newCursorPosition - 4;
// 檢驗新落點與其左點是否同屬一個圖片,如是則將落點移到左一點
if (ID[newCursorPosition] == ID[newCursorPosition - 1])
newCursorPosition = newCursorPosition - 1;
}
}
// 當前進行的是下移操作
if (dy > 0 ){
// 檢驗新舊落點是否同屬一個圖片
if (ID[newCursorPosition] == ID[cursorPosition]){
// 落點移動到下一行
newCursorPosition = newCursorPosition + 4;
// 新點是否合法
if (newCursorPosition < 20){
// 檢驗新點是否落在非主索引位置,如是則將落點移到左一點位置
if (Draw[newCursorPosition] == 0)
newCursorPosition = newCursorPosition - 1;
}
}
else{
// 如果新落點位于其他圖片的非主索引位置則將落點移到左一位置
newCursorPosition = newCursorPosition - 1;
}
}
// 當前進行的是上移操作
if (dy < 0){
// 檢驗新點是否落在關羽圖片上,如是則將落點左移一點
if (ID[newCursorPosition] == 5)
newCursorPosition = newCursorPosition - 1;
else if (ID[newCursorPosition] == 2){
// 當落在曹操圖片上時,根據兩圖片主索引位置的相對位置調整落點
if (ID[newCursorPosition - 5] == 2)
newCursorPosition = newCursorPosition - 5;
else
newCursorPosition = newCursorPosition - 4;
}else{
// 落點上移一行
newCursorPosition = newCursorPosition - 4;
}
}
}
if ((newCursorPosition >= 0) && (newCursorPosition < 20))
{
// 當前位置為前一位置,新位置為當前位置
preCursorPosition = cursorPosition;
cursorPosition = newCursorPosition;
// 重繪
repaint();
}
}
private void drawCursor(Graphics g) {
// 擦除舊光標
int x = (preCursorPosition % 4) * boardCellSize + 1 + boardLeft;
int y = ((int)(preCursorPosition / 4)) * boardCellSize + boardTop + 1;
g.drawImage(Pic[ID[preCursorPosition]], x, y, 0);
// 選定待移動的圖片
if (fireStep == 1 && ID[from] > 0){
// 在光標當前所處位置以黑線繪制光標框
g.setColor(RED);
wOld = boardCellSize;
hOld = boardCellSize;
if (from / 4 < 4)
if (Draw[from + 4] == 0 && ID[from] == ID[from + 4])
hOld = hOld * 2;
if (from % 4 < 3)
if (Draw[from + 1] == 0 && ID[from] == ID[from + 1])
wOld = wOld * 2;
// 繪制光標
g.drawRect(((from % 4) * boardCellSize) + 1 + boardLeft, ((int)(from / 4) * boardCellSize) + 1 + boardTop, wOld - 2, hOld - 2);
g.drawRect(((from % 4) * boardCellSize) + 2 + boardLeft, ((int)(from / 4) * boardCellSize) + 2 + boardTop, wOld - 4, hOld - 4);
}
// 移動到新位置
if (fireStep == 2){
fireStep = 0;
// 擦除選定標志
x = (from % 4) * boardCellSize + 1 + boardLeft;
y = ((int)(from / 4)) * boardCellSize + boardTop + 1;
g.drawImage(Pic[ID[from]], x, y, 0);
if (ID[cursorPosition] == 0){
// 張飛、馬超、趙云、黃忠、卒左/右移一格處理
if ((cursorPosition - from == 1 && cursorPosition % 4 > 0) || (cursorPosition - from == -1 && cursorPosition % 4 < 3)){
// 上下兩格的圖片(張飛、馬超、趙云、黃忠)左/右移處理
if (hOld == boardCellSize * 2 && ID[cursorPosition + 4] == 0 && wOld == boardCellSize){
ChangeV2_H(g);
return;
}
// 卒的一格左/右移處理
if (ID[from] == 7){
ChangeZu(g);
return;
}
}
// 張飛、馬超、趙云、黃忠左/右移一格處理
if ((cursorPosition - from == 5 && cursorPosition % 4 > 0) || (cursorPosition - from == 3 && cursorPosition % 4 < 3)){
// 上下兩格的圖片(張飛、馬超、趙云、黃忠)左/右移處理
if (hOld == boardCellSize * 2 && ID[cursorPosition - 4] == 0){
cursorPosition = cursorPosition - 4;
ChangeV2_H(g);
return;
}
}
// 曹操、關羽的左/右移處理
if ((cursorPosition - from == 2 && cursorPosition % 4 > 1) || (cursorPosition - from == -1 && cursorPosition % 4 < 2)){
// 曹操左/右移一格處理
if (ID[from] == 2){
if (cursorPosition - from == 2){
ChangeCaoCao_H(g, 1);
return;
}
if (cursorPosition - from == -1){
ChangeCaoCao_H(g, -1);
return;
}
}
// 關羽左/右移一格處理
if (ID[from] == 5){
if (cursorPosition - from == 2){
ChangeGuanYu_H(g, 1);
return;
}
if (cursorPosition - from == -1){
ChangeGuanYu_H(g, -1);
return;
}
}
}
// 關羽右移動兩格處理
if (cursorPosition - from == 3 && cursorPosition % 4 == 3){
if (ID[from + 2] == 0 && ID[from] == 5){
ChangeGuanYu_H(g, 2);
return;
}
}
// 關羽左移動兩格處理
if (cursorPosition - from == -2 && cursorPosition % 4 == 0){
if (ID[from - 1] == 0 && ID[from] == 5){
ChangeGuanYu_H(g, -2);
return;
}
}
// 曹操左/右移一格處理
if ((cursorPosition - from == 6 && cursorPosition % 4 > 1) || (cursorPosition - from == 3 && cursorPosition % 4 < 2)){
// 左移一格處理
if (cursorPosition - from == 3){
if (ID[from] == 2 && ID[cursorPosition - 1] == 0){
cursorPosition = cursorPosition - 4;
ChangeCaoCao_H(g, -1);
return;
}
}
// 右移一格處理
if (cursorPosition - from == 6){
if (ID[from] == 2 && ID[cursorPosition + 2] == 0){
cursorPosition = cursorPosition - 4;
ChangeCaoCao_H(g, 1);
return;
}
}
}
// 關羽、卒上下移動處理
if (cursorPosition - from == 4|| cursorPosition - from == -4){
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -