📄 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 + -