📄 tollgate.java
字号:
import java.lang.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
public class TollGate{
//Download by http://www.codefans.net
public static int curTollGate=0;//当前关卡
public static int tollGates=17;//总关卡数
public static int[] gateWidth= { 7, 7, 7, 7,11,11,11,11,11,11,13,13,13,13,19,19,19,19};//格子数宽
public static int[] gateHeight= { 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7};//格子数高
public static int[] houseNum= { 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21};//房子数
public static int[] emptyHouseNum={ 0, 1, 2, 1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 4, 5, 4, 5};//空房子数
public static int[] ghostNum= { 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 8};//鬼的数目(对)
public static int[] balkNum= { 1, 1, 6, 3, 3, 3, 4, 4, 5, 6, 6, 7, 7, 8, 9, 9,10,10};//障碍数目
public static int[] monsterNum= { 0, 1, 2, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8};//怪物数目
public static int[] kindPropNum= { 1, 2, 3, 2, 2, 3, 4, 4, 5, 6, 5, 4, 6, 5, 7, 5, 7, 8};//有益道具
public static int[] badPropNum= { 1, 1, 2, 2, 2, 3, 4, 5, 4, 5, 4, 5, 5, 7, 6, 7, 7, 9};//有害道具
public static int[] gateTimes= {30,30,60,60,80,80,100,100,130,130,160,160,180,180,200,200,240,240};//关卡时间
public static int[][] gateMap=null;//地图表
public static int[] gateProp=null;//道具表
public static int gridW=25;
public static int gridH=25;
public static int bgX,bgY;//根据角色所在位置定义地图从哪画
public static Image[] mapImg=null;
public static Image tmpImg=null;
public static String[] imgFiles={"/res/pace.png","/res/blak.png","/res/house.png","/res/house.png","/res/stone.png"};
public static int w,h;
static RecordStore rs=null;
/**************************************
*
*游戏相关函数
*
**************************************/
public static void gameInit(int gate){
curTollGate=gate;
}
public static void unGame(){
curTollGate=0;
}
public static void gateInit(){
/*
*地图数据定义
*0-空地 1-障碍 2有鬼房子 3空房子 4-石头
*/
mapImg=new Image[imgFiles.length];
w=gateWidth[curTollGate];
h=gateHeight[curTollGate];
bgX=0;
bgY=33;
for (int i=0;i<imgFiles.length;i++){
try{
mapImg[i]=Image.createImage(imgFiles[i]);
}catch(Exception e){
}
}
gateMap=new int[w][h];
int gridNum=(w-1)/2 * (h-1)/2;//栅格数
int houseN=houseNum[curTollGate];//实心房子数
int hoNum=houseN;
int emptyHouseN=emptyHouseNum[curTollGate];//空心房子数
int ehNum=emptyHouseN;
int stoneN=gridNum-houseN-emptyHouseN;//石头数
int stNum=stoneN;
int balkN=balkNum[curTollGate];//障碍数
int baNum=balkN;
int spaceN=w*h-gridNum-balkN;//空地数
int spNum=spaceN;
int node=-1;
Random random=new Random();
for (int i=0;i<w;i++){
for(int j=0;j<h;j++){
if(i%2!=0 && j%2!=0){//填房子或空房子或栅格
boolean isFill=false;
while(!isFill){
int rnd=Math.abs(random.nextInt())%gridNum;
if (rnd<houseN){ //实房子
if (hoNum>0){
gateMap[i][j]=2;
hoNum--;
isFill=true;
}
}
else if((rnd<gridNum-stoneN)){//空房子
if(ehNum>0){
gateMap[i][j]=3;
ehNum--;
isFill=true;
}
}
else if((rnd<gridNum)){//石头数
if (stNum>0){
gateMap[i][j]=4;
stNum--;
isFill=true;
}
}
}
}
else{//填空地或障碍
boolean isFill=false;
while(!isFill){
int rnd=Math.abs(random.nextInt())%(balkN+spaceN);
if(rnd<spaceN-1){//空地
if (spNum>0){
gateMap[i][j]=0;
spNum--;
isFill=true;
}
}
else if(rnd<balkN+spaceN){
if(baNum>0){//障碍
gateMap[i][j]=1;
baNum--;
isFill=true;
}
}
}
}
}
}
}
public static void gateEnd(){
for (int i=0;i<imgFiles.length;i++){
mapImg[i]=null;
}
}
public static void drawMap(Graphics g)
{
for (int i=0;i<w;i++){
for(int j=0;j<h;j++){
g.drawImage(mapImg[0],bgX+i*gridH,bgY+j*gridH,20);
}
}
for (int i=0;i<w;i++){
for(int j=0;j<h;j++){
if (gateMap[i][j]==1){
g.drawImage(mapImg[gateMap[i][j]],bgX+i*gridH,bgY+j*gridH,20);
}
else if(gateMap[i][j]==2 || gateMap[i][j]==3 || gateMap[i][j]==4){
g.drawImage(mapImg[gateMap[i][j]],bgX+i*gridW,bgY+j*gridH-10,20);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -