?? map類的源代碼.txt
字號:
package src;
import java.io.*;
import java.util.Date;
import java.util.Random;
import javax.microedition.lcdui.*;
class Map
{
private static int px = 128;
private static int py = 128;
private static int picx = 13;
private static int picy = 13;
private static int mx = 10;
private static int my = 9;
private static int picCount = 14;
private Image image[];
private int rolex, roley, selectx, selecty;
private boolean flag = false;
private int map[][];
private static int e = 255;
private Image start;
private int imagecount;
private boolean load;
private boolean flagmap;
private int state;
private int number;
private int cx[], cy[], cmc;
private int sx, sy, ex, ey;
private boolean clean;
static int max = 10;
private int level[];
private int num;
private boolean timeFlag;
private Image end;
private int value;
public int gamex;
private int gameCount;
public Map()
{
rolex = 3;
roley = 3;
map = new int[my][mx];
cx = new int[my * 3];
cy = new int[my * 3];
for (int y = 0; y < my; y++)
{
for (int x = 0; x < mx; x++)
{
map[y][x] = e;
}
}
load = false;
clean = false;
imagecount = 0;
flagmap = false;
level = new int[max];
num = 0;
timeFlag = false;
value = 0;
gamex = 0;
gameCount = 0;
}
private int GetGameLevel()
{
int iV;
Date tDate = new Date();
Random tRand = new Random(tDate.getTime());
if ((num == 0) || (num == max))
{
iV = Math.abs(tRand.nextInt()) % max;
level[0] = iV;
num = 1;
return iV;
}
while(true)
{
iV = Math.abs(tRand.nextInt()) % max;
for (int i = 0; i < num; i++)
{
if (level[i] == iV)
break;
if (i == (num - 1))
{
num++;
level[i + 1] = iV;
return iV;
}
}
}
}
public boolean LoadGameLevelMap()
{
InputStream is = null;
int iT;
char iC;
String strFile;
iT = 0;
iC = 0;
timeFlag = false;
end = null;
value ++;
strFile = "/Res/Data/" + GetGameLevel() + ".llk";
try
{
is = getClass().getResourceAsStream(strFile);
for (int y = 0; y < my; y++)
{
for (int x = 0; x < mx; x++)
{
if ((y == 0) || (x == 0) || (y == my - 1)
|| (x == mx - 1))
{
map[y][x] = e;
continue;
}
iT = is.read();
iC = (char) iT;
map[y][x] = iC;
if (iC < picCount)
imagecount++;
}
}
is.close();
}
catch (java.io.IOException ex)
{
ex.printStackTrace();
return false;
}
flagmap = true;
return true;
}
public boolean LoadGameImage()
{
String strFileName;
if (load)
return true;
image = new Image[picCount];
strFileName = "";
for (int i = 0; i < picCount; i++)
{
try
{
strFileName = "/Res/Icons/" + i + ".png";
image[i] = Image.createImage(strFileName);
}
catch (java.io.IOException e)
{
e.printStackTrace();
return false;
}
}
try
{
start = Image.createImage("/Res/Icons/Statusbar.png");
}
catch (java.io.IOException e)
{
e.printStackTrace();
return false;
}
load = 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
{
end = Image.createImage(strFile);
}
catch (java.io.IOException e)
{
e.printStackTrace();
return;
}
}
public int DrawScreen(Graphics g, int iTimeCount, boolean bCleanFlag,boolean bCleanStart)
{
int iRtn;
iRtn = 0;
if (flagmap)
{
return iRtn;
}
if ((bCleanFlag) && (bCleanStart == false))
{
map[sy][sx] = e;
map[ey][ex] = e;
clean = false;
gameCount += 10;
if (imagecount == 0)
iRtn = 1;
}
PaintGameMap(g);
PaintCourse(g);
PaintStatusBar(g, iTimeCount);
if ((bCleanFlag) && (bCleanStart))
{
PaintCleanFlag(g);
}
if (timeFlag)
{
iRtn = 2;
}
return iRtn;
}
public void PaintGameEnd(Graphics g,int iTime)
{
String strGameAmt,strGameLevelValue,strTime;
strTime = String.valueOf(iTime);
strGameAmt = String.valueOf(gameCount);
strGameLevelValue = String.valueOf(value);;
try
{
g.drawImage(end, 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 (Exception ex)
{
ex.printStackTrace();
}
}
private void PaintIniMap(Graphics g)
{
int iy, ihigh;
g.setColor(0, 223, 255);
iy = state * 8;
ihigh = (8 - state) * 18;
g.fillRect(0, iy, 128, ihigh);
System.out.println("PaintIniMap: " + state);
}
public void SetIniMapState(int ms)
{
state = ms;
if (ms == 0)
{
flagmap = false;
}
System.out.println("SetIniMapState: " + ms);
}
private void PaintCleanStatus()
{
System.out.println("sx:" + sx + "/sy:" + sy);
System.out.println("ex:" + ex + "/ey:" + ey);
for (int i = 0; i < cmc; i++) {
System.out.println("x:" + cx[i] + "/y:" + cy[i]);
}
}
public void PaintCleanFlag1(Graphics g)
{
int tx1, ty1, tx2, ty2;
g.setColor(2, 255, 2);
tx1 = 0;
ty1 = 0;
tx2 = 0;
ty2 = 0;
tx1 = cx[0];
ty1 = cy[0];
for (int i = 1; i < cmc; i++)
{
tx2 = cx[i];
ty2 = cy[i];
if ((tx1 == tx2) || (ty1 == ty2))
continue;
tx2 = cx[i - 1];
ty2 = cy[i - 1];
// up
if ((ty1 == 0) && (ty2 == 0))
{
if (tx1 == 0)
{
tx1 = 2;
}
else if (tx1 == mx - 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 == mx - 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 == my - 1) && (ty2 == my - 1))
{
if (tx1 == 0)
{
tx1 = 2;
}
else if (tx1 == mx - 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 == mx - 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 == my - 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 == my - 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 == mx - 1) && (tx2 == mx - 1))
{
if (ty1 == 0)
ty1 = 2;
else if (ty1 == my - 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 == my - 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 = cx[i];
ty1 = cy[i];
}
}
public void PaintCleanFlag2(Graphics g)
{
int tx1, ty1, tx2, ty2;
tx1 = 0;
ty1 = 0;
tx2 = 0;
ty2 = 0;
tx1 = cx[0];
ty1 = cy[0];
g.setColor(2, 255, 2);
for (int i = 1; i < cmc - 1; i++) {
tx2 = cx[i + 1];
ty2 = cy[i + 1];
if ((tx1 == tx2) || (ty1 == ty2))
continue;
tx2 = cx[i];
ty2 = cy[i];
if (((tx1 > 0) && (ty1 == 0))|| ((tx1 > 0) && (ty1 == my - 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)) ||(tx1 == mx - 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 == my - 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)) ||(tx2 == mx - 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 = cx[i];
ty1 = cy[i];
}
}
//畫連接線
private void PaintCleanFlag(Graphics g)
{
int tx, ty;
boolean isMid, isChange;
// isChange = false;
g.setColor(2, 255, 2);
for (int i = 0; i < cmc; i++)
{
tx = cx[i];
ty = cy[i];
isMid = false;
g.setColor(16, 24, 173);
if (((tx > 0) && (ty == 0)) || ((tx > 0) && (ty == my - 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)) || (tx == mx - 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);
}
else
g.fillArc(tx, ty, 5, 5, 45, 360);
}
}
private void PaintStatusBar(Graphics g, int iTimeCount)
{
String strV;
int xPos;
try
{
if (iTimeCount > 12)
{
timeFlag = true;
return;
}
g.drawImage(start, 0, 113, Graphics.LEFT | Graphics.TOP);
g.setColor(24,142,24);
xPos = 20 + 52 - (iTimeCount * 4);
g.drawRect(xPos,113 + 4 ,iTimeCount * 4,4);
g.fillRect(xPos,113 + 4,iTimeCount * 4,4);
g.setColor(0, 0, 255);
strV = String.valueOf(value);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -