/*
* 创建日期 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 (Dra
评论0
最新资源