📄 gamemap.java
字号:
package LLK;
//import java.io.InputStream;
import java.io.*;
import java.util.Date;
import java.util.Random;
import javax.microedition.lcdui.*;
//import java.util.*;
class GameMap {
//定义屏幕最大分辨率
private static int stPixel_x = 128;
private static int stPixel_y = 128;
//定义图片大小
private static int stPic_x = 13;
private static int stPic_y = 13;
//定义地图大小
private static int stMap_x = 10;
private static int stMap_y = 9;
//图片文件个数
private static int stMAX_PIC = 14;
private Image m_image[];
//光标所在的坐标,已经选择了的图片的坐标
private int m_role_x, m_role_y, m_select_x, m_select_y;
//是否已经选择了一个的标志
private boolean m_SelectOne = false;
//地图信息
private int m_map[][];
//地图位置,为空标志
private static int ISEMPTY = 255;
//状态条图片
private Image m_StatusImage;
//还剩下的图片个数,当为0的时候,游戏结束
private int m_iLeaveImageCount;
//标志是否已经转载了图片信息
private boolean m_IsLoadImage;
//标志是否是出现地图初始化效果的时候,还是在游戏进程中。
private boolean m_isIniMap;
//指示地图初始化效果的进行状态
private int m_IniMapState;
//指示进行何种地图初始化效果
private int m_IniMapNum;
//连接轨迹信息
private int m_CntX[], m_CntY[], m_CntMapsCount;
private int m_cf_sx, m_cf_sy, m_cf_ex, m_cf_ey;//保存开始,结束坐标
private boolean m_cleanFlag;//成功连接标志
//地图文件总数
static int MAX_GAME_MAP_FILES = 10;
private int m_mf_Level[];
private int m_mf_CurNo;
//标志是否游戏超时,该退出了。
private boolean m_gameTimeout;
//游戏结束的时候的显示的文件名
private Image m_ImgGameEnd;
private int m_GameLevelValue;//游戏关数
public int m_GameMaxAmt; //本游戏的最大金币
private int m_GameCurAmt; //本游戏的当前的金币数
public GameMap() {
m_role_x = 3;
m_role_y = 3;
m_map = new int[stMap_y][stMap_x];
m_CntX = new int[stMap_y * 3];
m_CntY = new int[stMap_y * 3];
for (int y = 0; y < stMap_y; y++) {
for (int x = 0; x < stMap_x; x++) {
m_map[y][x] = ISEMPTY;
}
}
m_IsLoadImage = false;
m_cleanFlag = false;
m_iLeaveImageCount = 0;
m_isIniMap = false;
m_mf_Level = new int[MAX_GAME_MAP_FILES];
m_mf_CurNo = 0;
m_gameTimeout = false;
m_GameLevelValue = 0;
m_GameMaxAmt = 0;
m_GameCurAmt = 0;
}
private int GetGameLevel() {
int iV;
Date tDate = new Date();
Random tRand = new Random(tDate.getTime());
if ((m_mf_CurNo == 0) || (m_mf_CurNo == MAX_GAME_MAP_FILES)) {
iV = Math.abs(tRand.nextInt()) % MAX_GAME_MAP_FILES;
m_mf_Level[0] = iV;
m_mf_CurNo = 1;
//System.out.println("[0]: " + iV);
return iV;
}
for (;;) {
iV = Math.abs(tRand.nextInt()) % MAX_GAME_MAP_FILES;
for (int i = 0; i < m_mf_CurNo; i++) {
if (m_mf_Level[i] == iV)
break;
if (i == (m_mf_CurNo - 1)) {
m_mf_CurNo++;
m_mf_Level[i + 1] = iV;
//System.out.println("[" + (i + 1) + "]: " + iV);
return iV;
}
}
}
}
public boolean LoadGameLevelMap() {
InputStream is = null;
int iT;
char iC;
String strFile;
iT = 0;
iC = 0;
m_gameTimeout = false;
m_ImgGameEnd = null;
m_GameLevelValue ++;
strFile = "/Res/Data/" + GetGameLevel() + ".llk";
System.out.println("LoadFile: " + strFile);
try {
is = getClass().getResourceAsStream(strFile);///"/Res/Data/0.llk");
for (int y = 0; y < stMap_y; y++) {
for (int x = 0; x < stMap_x; x++) {
if ((y == 0) || (x == 0) || (y == stMap_y - 1)
|| (x == stMap_x - 1)) {
m_map[y][x] = ISEMPTY;
continue;
}
iT = is.read();
iC = (char) iT;
m_map[y][x] = iC;
//保存单个游戏里面,一共有多小张图片
if (iC < stMAX_PIC)
m_iLeaveImageCount++;
// System.out.println("x:" + x + ";y:" + y);
// System.out.println(" m_Map: " + iT);
}
}
is.close();
} catch (java.io.IOException ex) {
System.out.println("Load Resource file failed");
return false;
}
System.out.println("Load Resource file sucess");
m_isIniMap = true;
return true;
}
public boolean LoadGameImage() {
String strFileName;
if (m_IsLoadImage)
return true;
// Date tDate = new Date();
// Random tRand = new Random(tDate.getTime());
/*
* int iIM;
*
* m_map = new int[stMap_y][stMap_x]; iIM = 0; for (int y = 0; y <
* stMap_y; y++) { for (int x = 0; x < stMap_x; x++) {
*
* m_map[y][x] = iIM;//tRand.nextInt() % stMAX_Png;
*
* iIM++; if (iIM == 8) iIM = 0; } }
*/
m_image = new Image[stMAX_PIC];
strFileName = "";
for (int i = 0; i < stMAX_PIC; i++) {
try {
strFileName = "/Res/Icons/" + i + ".png";
m_image[i] = Image.createImage(strFileName);
} catch (java.io.IOException e) {
System.err.println("Unable to load file: " + strFileName);
return false;
}
}
try {
m_StatusImage = Image.createImage("/Res/Icons/Statusbar.png");
} catch (java.io.IOException e) {
System.err.println("Unable to load status bar error ");
return false;
}
System.err.println("InitGameMap success !!!");
m_IsLoadImage = true;
return true;
}
public void LoadGameEndImg(boolean isSuccessFile) {
String strFile;
if (isSuccessFile)
strFile = "/Res/Icons/End_Success.PNG";
else
strFile = "/Res/Icons/End_Failed.PNG";
try {
m_ImgGameEnd = Image.createImage(strFile);
} catch (java.io.IOException e) {
System.err.println("Unable to load LoadGameEndImg error ");
return;
}
}
public int DrawScreen(Graphics g, int iTimeCount, boolean bCleanFlag,
boolean bCleanStart) {
int iRtn;
iRtn = 0;
if (m_isIniMap) {
//暂时去掉地图初始化效果
//PaintGameMap(g);
//PaintIniMap(g);
return iRtn;
}
if ((bCleanFlag) && (bCleanStart == false)) {
m_map[m_cf_sy][m_cf_sx] = ISEMPTY;
m_map[m_cf_ey][m_cf_ex] = ISEMPTY;
m_cleanFlag = false;
m_GameCurAmt += 10;
//检查是否玩完了,如果完了,返回1标志。
if (m_iLeaveImageCount == 0)
iRtn = 1;
}
PaintGameMap(g);
PaintCourse(g);
PaintStatusBar(g, iTimeCount);
if ((bCleanFlag) && (bCleanStart)) {
PaintCleanFlag(g);
}
//游戏超时没有玩完,返回2标志
if (m_gameTimeout) {
iRtn = 2;
}
return iRtn;
}
//传入游戏花费的时间
public void PaintGameEnd(Graphics g,int iTime) {
String strGameAmt,strGameLevelValue,strTime;
strTime = String.valueOf(iTime);
strGameAmt = String.valueOf(m_GameCurAmt);
strGameLevelValue = String.valueOf(m_GameLevelValue);;
try {
g.drawImage(m_ImgGameEnd, 0, 0, Graphics.LEFT | Graphics.TOP);
g.setColor(0, 0, 255);
g.drawString(strGameLevelValue, 55, 74, 33);
g.drawString(strGameAmt, 70, 94, 33);
g.drawString(strTime, 80, 114, 33);
} catch (IllegalArgumentException ei) {
System.err.println("catch(IllegalArgumentException ei)");
} catch (NullPointerException en) {
System.err.println("catch(NullPointerException en");
}
}
private void PaintIniMap(Graphics g) {
//m_IniMapNum;m_IniMapState
//m_IniMapState
int iy, ihigh;
g.setColor(0, 223, 255);
iy = m_IniMapState * 8;
ihigh = (8 - m_IniMapState) * 18;
g.fillRect(0, iy, 128, ihigh);
//g.drawRect(0, 0, 16, 16);
System.out.println("PaintIniMap: " + m_IniMapState);
}
//显示初始化地图的效果。"没有用了"
public void SetIniMapState(int ms) {
m_IniMapState = ms;
if (ms == 0) {
m_isIniMap = false;
}
System.out.println("SetIniMapState: " + ms);
}
private void PaintCleanStatus() {
//if (! m_cleanFlag)
// return;
System.out.println("sx:" + m_cf_sx + "/sy:" + m_cf_sy);
System.out.println("ex:" + m_cf_ex + "/ey:" + m_cf_ey);
for (int i = 0; i < m_CntMapsCount; i++) {
System.out.println("x:" + m_CntX[i] + "/y:" + m_CntY[i]);
}
//m_map[m_cf_sy][m_cf_sx] = ISEMPTY;
//m_map[m_cf_ey][m_cf_ex] = ISEMPTY;
//m_cleanFlag = false;
}
public void PaintCleanFlag1(Graphics g) {
int tx1, ty1, tx2, ty2;
//boolean bStart;
g.setColor(2, 255, 2);
tx1 = 0;
ty1 = 0;
tx2 = 0;
ty2 = 0;
tx1 = m_CntX[0];
ty1 = m_CntY[0];
for (int i = 1; i < m_CntMapsCount; i++) {
tx2 = m_CntX[i];
ty2 = m_CntY[i];
//在相同行
if ((tx1 == tx2) || (ty1 == ty2))
continue;
tx2 = m_CntX[i - 1];
ty2 = m_CntY[i - 1];
// up
if ((ty1 == 0) && (ty2 == 0)) {
if (tx1 == 0) {
tx1 = 2;
} else if (tx1 == stMap_x - 1) {
tx1 = 5 + tx1 * (tx1 - 1) * 13 + (tx1 - 1) * 2 + 2;
} else
tx1 = 5 + tx1 * (tx1 - 1) * 13 + (tx1 - 1) * 2 + 6;
ty1 = 2;
if (tx2 == 0) {
tx2 = 2;
} else if (tx2 == stMap_x - 1) {
tx2 = 5 + tx2 * (tx2 - 1) * 13 + (tx2 - 1) * 2 + 2;
} else
tx2 = 5 + tx2 * (tx2 - 1) * 13 + (tx2 - 1) * 2 + 6;
ty2 = 2;
// down
} else if ((ty1 == stMap_y - 1) && (ty2 == stMap_y - 1)) {
if (tx1 == 0) {
tx1 = 2;
} else if (tx1 == stMap_x - 1) {
tx1 = 5 + tx1 * (tx1 - 1) * 13 + (tx1 - 1) * 2 + 2;
} else
tx1 = 5 + tx1 * (tx1 - 1) * 13 + (tx1 - 1) * 2 + 6;
ty1 = 5 + ty1 * (ty1 - 1) * 13 + (ty1 - 1) * 2 + 2;
if (tx2 == 0) {
tx2 = 2;
} else if (tx2 == stMap_x - 1) {
tx2 = 5 + tx2 * (tx2 - 1) * 13 + (tx2 - 1) * 2 + 2;
} else
tx2 = 5 + tx2 * (tx2 - 1) * 13 + (tx2 - 1) * 2 + 6;
ty2 = ty1;
// left
} else if ((tx1 == 0) && (tx2 == 0)) {
if (ty1 == 0)
ty1 = 2;
else if (ty1 == stMap_y - 1)
ty1 = 5 + ty1 * (ty1 - 1) * 13 + (ty1 - 1) * 2 + 2;
else
ty1 = 5 + ty1 * (ty1 - 1) * 13 + (ty1 - 1) * 2 + 6;
tx1 = 2;
if (ty2 == 0)
ty2 = 2;
else if (ty2 == stMap_y - 1)
ty2 = 5 + ty2 * (ty2 - 1) * 13 + (ty2 - 1) * 2 + 2;
else
ty2 = 5 + ty2 * (ty2 - 1) * 13 + (ty2 - 1) * 2 + 6;
tx2 = 2;
//right
} else if ((tx1 == stMap_x - 1) && (tx2 == stMap_x - 1)) {
if (ty1 == 0)
ty1 = 2;
else if (ty1 == stMap_y - 1)
ty1 = 5 + ty1 * (ty1 - 1) * 13 + (ty1 - 1) * 2 + 2;
else
ty1 = 5 + ty1 * (ty1 - 1) * 13 + (ty1 - 1) * 2 + 6;
tx1 = 5 + ty1 * (ty1 - 1) * 13 + (ty1 - 1) * 2 + 2;
if (ty2 == 0)
ty2 = 2;
else if (ty2 == stMap_y - 1)
ty2 = 5 + ty2 * (ty2 - 1) * 13 + (ty2 - 1) * 2 + 2;
else
ty2 = 5 + ty2 * (ty2 - 1) * 13 + (ty2 - 1) * 2 + 6;
tx2 = tx1;
} else {
tx1 = 5 + tx1 * (tx1 - 1) * 13 + (tx1 - 1) * 2 + 6;
ty1 = 5 + ty1 * (ty1 - 1) * 13 + (ty1 - 1) * 2 + 6;
tx2 = 5 + tx2 * (tx2 - 1) * 13 + (tx2 - 1) * 2 + 6;
ty2 = 5 + ty2 * (ty2 - 1) * 13 + (ty2 - 1) * 2 + 6;
}
g.drawLine(tx1, ty1, tx2, ty2);
tx1 = m_CntX[i];
ty1 = m_CntY[i];
//g.fillArc(tx1,ty1,5,5,45,360);
//g.drawArc(tx,ty,5,5,45,360);
}
}
public void PaintCleanFlag2(Graphics g) {
int tx1, ty1, tx2, ty2;
tx1 = 0;
ty1 = 0;
tx2 = 0;
ty2 = 0;
tx1 = m_CntX[0];
ty1 = m_CntY[0];
g.setColor(2, 255, 2);
for (int i = 1; i < m_CntMapsCount - 1; i++) {
tx2 = m_CntX[i + 1];
ty2 = m_CntY[i + 1];
//在相同行
if ((tx1 == tx2) || (ty1 == ty2))
continue;
tx2 = m_CntX[i];
ty2 = m_CntY[i];
//position 1
if (((tx1 > 0) && (ty1 == 0))
|| ((tx1 > 0) && (ty1 == stMap_y - 1))) //up down
{
tx1 = 5 + (tx1 - 1) * 13 + (tx1 - 1) * 2 + 2;
if (ty1 != 0)
ty1 = 5 + (ty1 - 1) * 13 + (ty1 - 1) * 2 + 2;
} else if (((tx1 == 0) && (ty1 > 0)) || // left right
(tx1 == stMap_x - 1) && ty1 > 0) {
ty1 = 5 + (ty1 - 1) * 13 + (ty1 - 1) * 2 + 2;
if (tx1 != 0)
tx1 = 5 + (tx1 - 1) * 13 + (tx1 - 1) * 2 + 2;
} else if ((tx1 == 0) && (ty1 == 0)) {
} else {
tx1 = 5 + (tx1 - 1) * 13 + (tx1 - 1) * 2 + 7;
ty1 = 5 + (ty1 - 1) * 13 + (ty1 - 1) * 2 + 7;
}
//position 2
if (((tx2 > 0) && (ty2 == 0))
|| ((tx2 > 0) && (ty2 == stMap_y - 1))) //up down
{
tx2 = 5 + (tx2 - 1) * 13 + (tx2 - 1) * 2 + 2;
if (ty2 != 0)
ty2 = 5 + (ty2 - 1) * 13 + (ty2 - 1) * 2 + 2;
} else if (((tx2 == 0) && (ty2 > 0)) || // left right
(tx2 == stMap_x - 1) && ty2 > 0) {
ty2 = 5 + (ty2 - 1) * 13 + (ty2 - 1) * 2 + 2;
if (tx2 != 0)
tx2 = 5 + (tx2 - 1) * 13 + (tx2 - 1) * 2 + 2;
} else if ((tx2 == 0) && (ty2 == 0)) {
} else {
tx2 = 5 + (tx2 - 1) * 13 + (tx2 - 1) * 2 + 7;
ty2 = 5 + (ty2 - 1) * 13 + (ty2 - 1) * 2 + 7;
}
g.drawLine(tx1, ty1, tx2, ty2);
tx1 = m_CntX[i];
ty1 = m_CntY[i];
}
}
//画连接线
private void PaintCleanFlag(Graphics g) {
int tx, ty;
boolean isMid, isChange;
// isChange = false;
g.setColor(2, 255, 2);
for (int i = 0; i < m_CntMapsCount; i++) {
tx = m_CntX[i];
ty = m_CntY[i];
isMid = false;
// if (isChange)
g.setColor(16, 24, 173);
// else
// g.setColor(255, 2, 255);
// isChange = ! isChange;
//stMap_x
if (((tx > 0) && (ty == 0)) || ((tx > 0) && (ty == stMap_y - 1))) //up
// down
{
tx = 5 + (tx - 1) * 13 + (tx - 1) * 2 + 4;
if (ty != 0)
ty = 5 + (ty - 1) * 13 + (ty - 1) * 2;
} else if (((tx == 0) && (ty > 0)) || // left right
(tx == stMap_x - 1) && ty > 0) {
ty = 5 + (ty - 1) * 13 + (ty - 1) * 2 + 4;
if (tx != 0)
tx = 5 + (tx - 1) * 13 + (tx - 1) * 2;
} else if ((tx == 0) && (ty == 0)) {
} else {
isMid = true;
tx = 5 + (tx - 1) * 13 + (tx - 1) * 2 + 2;
ty = 5 + (ty - 1) * 13 + (ty - 1) * 2 + 2;
}
if (isMid) {
g.fillArc(tx, ty, 10, 10, 45, 360);
//g.setColor(125, 125, 125);
//g.fillArc(tx + 2,ty + 2,5,5,45,360);
} else
g.fillArc(tx, ty, 5, 5, 45, 360);
//g.drawArc(tx,ty,5,5,45,360);
}
}
private void PaintStatusBar(Graphics g, int iTimeCount) {
String strV;
int xPos;
try {
//游戏超时结束
if (iTimeCount > 12) {
m_gameTimeout = true;
return;
}
g.drawImage(m_StatusImage, 0, 113, Graphics.LEFT | Graphics.TOP);
//239,239,0;24,142,24
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -