⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 monster.java

📁 J2me手机游戏捉鬼源代码
💻 JAVA
字号:
//Monster.java
//怪物类
//Download by http://www.codefans.net
import java.lang.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class Monster{
	//0-僵尸 1-幽灵 2-夜叉 3-牛头 4-马面
	static int monTypes=5;
	static int[] monType={0,1,2,3,4};
	static String[] monPng={"/res/mon0.png","/res/mon1.png","/res/mon2.png","/res/mon3.png","/res/mon4.png"};
	static int [] monUse={0,0,0,0,0};
	static int[] monWidth={30,30,30,30,30};//图片宽度
	static int[] monHeight={30,30,30,30,30};//图片高度
	public int moveRate=2;//怪物移动速度
	static int[] baseRate={2,2,2,2,2};
	static int[][][] MoveFrame={
							{{0},{1},{2},{3}},
							{{0},{1},{2},{3}},
							{{0},{1},{3},{2}},
							{{0},{1},{1},{2}},
							{{0},{1},{1},{2}}
							};
	static Image[] monImgs=new Image[5];
	public int curType=0;
	public Sprite monster=null;
	static int[] actX={0,0,-1,1};//移动时坐标改变值
	static int[] actY={-1,1,0,0};
	int curDir;//当前方向(上,下,左,右)
	int curx,cury;//当前坐标
	int curX,curY;
	int nextx,nexty;
	int nextLx,nextRx,nextUy,nextDy;//角色的四个边界位置
	int nextX,nextY;
	int moveSteps=100;
	public static int w,h;
	static Random random=new Random();
	int posi=-2;
	public int creatTime=0;
        static int i,j;
	public Monster(){//初始化函数
	}
	public static void gameInit(){
		GhostCanvas.mons=new Vector();	
	}
	public static void unGame(){
		GhostCanvas.mons=null;
	}
	public static void gateInit(){
		w=TollGate.gateWidth[TollGate.curTollGate];
		h=TollGate.gateHeight[TollGate.curTollGate];
		for (i=0;i<TollGate.monsterNum[TollGate.curTollGate];i++){
			Monster ms=new Monster();
			ms.getMonster();
			ms.monster.setVisible(true);
			GhostCanvas.mons.addElement(ms);
			ms=null;
		}
		for(i=0;i<monUse.length;i++){
			if(monUse[i]==0){
				monImgs[i]=null;
			}
		}
	}
	public static void gateEnd(){
		GhostCanvas.mons.removeAllElements();
		for(int i=0;i<monUse.length;i++){
			monUse[i]=0;
		}
		for(int i=0;i<monImgs.length;i++){
				monImgs[i]=null;
		}
	}
	public void getMonster()
	{
		int rnd=Math.abs(random.nextInt())%monTypes;
		curType=rnd;
		monUse[curType]=1;
		if(monImgs[curType]==null){
			try{
				monImgs[curType]=Image.createImage(monPng[curType]);
			}
			catch(Exception e){
			}
		}
		monster=new Sprite(monImgs[curType],monWidth[curType],monHeight[curType]);
		int t=findPaceXY();
		setPositionXY(t%w,t/w);
	}
	public static  void  creatMonster(Monster mon){
	
		Monster mm=new Monster();
		mm.curType=mon.curType;
		mm.monster=new Sprite(monImgs[mm.curType],monWidth[mm.curType],monHeight[mm.curType]);
		mm.monster.setVisible(true);
		GhostCanvas.mons.addElement(mm);			
		mm.curDir=mon.curDir;
		mm.setPositionxy(mon.curx,mon.cury);
		mm.changeDir();	
		
	}
	public int findPaceXY()
	{
		int rnd=Math.abs(random.nextInt())%(w*h);
		int t1=Math.abs(rnd%w-Man.curX);
		int t2=Math.abs(rnd/w-Man.curY);
		while(TollGate.gateMap[rnd%w][rnd/w]!=0 || (t1<4 && t2<4) || rnd%w==0 || rnd%w==TollGate.gateWidth[TollGate.curTollGate]){
			rnd++;
			if (rnd>w*h-1){
				rnd=Math.abs(random.nextInt())%(w*h);
			}
			t1=Math.abs(rnd%w-Man.curX);
			t2=Math.abs(rnd/w-Man.curY);
		}
		return rnd;
	}
	public void move()
	{
		if (curType==3 && !Man.bInHouse){//牛头
			if (isManNear(GhostCanvas.man)){
				int di=isManSameLine(GhostCanvas.man);
				if (di!=9){
					if (isShowMan(GhostCanvas.man,di)){
						changeDir(di);
					}
				}
			}
		}//
		moveRate=baseRate[curType];
		getNextXy();//得到下一步的坐标
		while(!canMove() && moveRate>0)
		{   
			moveRate--;
			getNextXy();
			//System.out.println("8888");
		}
		if (moveRate==0 || moveSteps==0){//前方确实不能再走,改变方向
			moveRate=baseRate[curType];
			moveSteps=Math.abs(random.nextInt())%20+8;
			changeDir();
		}
		if (canMove()){
			curx=curx+actX[curDir]*moveRate;
			cury=cury+actY[curDir]*moveRate;
			curX=curx/25;
			curY=cury/25;
			setPositionxy(curx,cury);
			moveSteps--;
		}

	//	System.out.println("6");
	}
	public void changeDir()//改变方向,找一个可以通过的方向
	{
		curDir=Math.abs(random.nextInt())%4;
		if(!canMove()){
			curDir++;
			if (curDir>3){
				curDir=0;
			}
			if (curType==4){
				goNewPosition();
				//	System.out.println("77");	
			}
		//	System.out.println("curType:"+curType);	
			getNextXy();
		}
		monster.setFrameSequence(MoveFrame[curType][curDir]);
	//	System.out.println("7");
	}
	public void changeDir(int dir)
	{
		curDir=dir;
		monster.setFrameSequence(MoveFrame[curType][curDir]);
		//System.out.println("8");
	}
	public void getNextXy()
	{
		nextx=curx+actX[curDir]*moveRate;
		nexty=cury+actY[curDir]*moveRate;
		nextLx=nextx-monster.getWidth()/2;
		nextRx=nextx+monster.getWidth()/2;
		nextUy=nexty-monster.getHeight();
		nextDy=nexty;
		nextX=nextx/25;
		nextY=nexty/25;
		//System.out.println("9");
	}
	public boolean canMove()//判断前方是否可以移动
	{
		boolean b=true;
		if (isOverPale()){
			b=false;
		}
		else{
			switch(curType){
				case 0://僵尸
					if(isHitHouse() || isHitBalk()){
						b=false;
					} 
					break;
				case 1://幽灵
					if(isHitHouse()){
						b=false;
					}
					break;
				case 2://夜叉
				case 3://牛头
				case 4://马面
					if(isHitHouse() || isHitBalk()){
						b=false;
					} 
					break;										
			}						
		}
		//System.out.println("11");
		return b;
	}
	public boolean isOverPale()//是否超出地图界线
	{
		boolean b=false;
		if (nextLx<0 || nextRx>w*25-1 || nextUy<-20 || nextDy>174){
			b=true;
		}

		return b;
		
	}	
	public boolean isHitHouse()//是否将要碰房子或石头
	{
		boolean b=false;
		if (nextX<0 || nextX>w-1 || nextY<0 || nextY>h-1){
			//	System.out.println("b");
			b=true;
		}
		else{
			int t=TollGate.gateMap[nextX][nextY];
			if (t==2 || t==3 || t==4){
				//	System.out.println("c");
				b=true;
			}	
		}

		return b;		
	}
	public boolean isHitBalk()//是否将要碰障碍
	{
		boolean b=false;
		if (nextX<0 || nextX>w-1 || nextY<0 || nextY>h-1){
		//	System.out.println("d");
			b=true;
		}
		else{
			int t=TollGate.gateMap[nextX][nextY];
			if (t==1){
			//	System.out.println("e");
				b=true;
			}			
		}
		return b;
	}
	public boolean isManNear(Man man){//角色是否在怪物附近
		boolean b=false;
		int mx=man.curx;
		int my=man.cury;
		int n1=Math.abs(mx-curx);
		int n2=Math.abs(my-cury);
		if (n1<120 && n2<120){
			b=true;
		}
		return b;
	}
	public boolean isShowMan(Man man, int dir){//角色和怪物中间是否有障碍 dir是角色的方向(假设在一条线上)
		
		boolean b=true;
		int sX=man.curX;
		int sY=man.curY;
		int tX=curX;
		int tY=curY;
		int addx=0;
		int addy=0;
		int steps=0;
		if (dir==0){//上
			addy=1;
		}
		else if(dir==1){//下
			addy=-1;
		}
		else if(dir==2){//左
			addx=1;
		}
		else if(dir==3){//右
			addx=-1;
		}
		while(sX!=tX || sY!=tY){
			//System.out.println("13");
			steps++;
			if (TollGate.gateMap[sX][sY]!=0){
				b=false;
				break;
			}
			sX=sX+addx;
			sY=sY+addy;
		}
		if (b){
			moveSteps=(steps*25)/baseRate[curType]+10;
		} 
		return b;
	}
	public int isManSameLine(Man man)//角色是否和怪物在一条线上,返回角色的方向
	{//返回0-3代表精灵在一条线上的方向,9代表不在一条线上,只有牛头才会追(假设怪物和精灵不在一个格子内)
		int r;
		int mX=man.curX;
		int mY=man.curY;
		if (mX==curX && mY==curY){
			r=9;
		}
		else if (mX==curX){
			if (mY<curY){
				r=0;
			}
			else{
				r=1;
			}
		}
		else if(mY==curY){
			if(mX<curX){
				r=2;
			}
			else{
				r=3;
			}
		}
		else{
			r=9;
		}
		return r;
	}
	public void goNewPosition()
	{
		int ms=w*h;
		int rnd=Math.abs(random.nextInt())%ms;
		int tmp=TollGate.gateMap[rnd%w][rnd/w];
		boolean find=false;
		for (i=0;i<10;i++){
			if (tmp==0){
				find=true;
				break;
			}
			else
			{
				rnd=Math.abs(random.nextInt())%ms;
				tmp=TollGate.gateMap[rnd%w][rnd/w];
			}
		}
		if (find){
			setPositionXY(rnd%w,rnd/w);
		}
	}
	private void setPositionXY(int X,int Y){
		setPositionxy(X*25+monster.getWidth()/2-5,Y*25+monster.getHeight()-10);
	}
	private void setPositionxy(int x,int y){
		posi=-posi;
		int ps=0;
		if (curType==0){
			ps=posi;
		}
		monster.setPosition(TollGate.bgX+x-monster.getWidth()/2,TollGate.bgY+y-monster.getHeight()+ps);
		curx=x;
		cury=y;
		curX=curx/25;
		curY=cury/25;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -