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

📄 npc.java

📁 坦克大战类游戏实现坦克的基本功能控制坦克地图行走
💻 JAVA
字号:
import java.util.Random;

import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;


public class NPC extends Sprite 
{
	int f,t,t1;
	Random r;
	public NPC(Image im, int w,int h,int x,int y,int[]a2)
	{
	 super(im,w,h);
	 this.setFrameSequence(a2);
		this.setPosition(x,y);
		r=new Random();
		t=Math.abs(r.nextInt()%40)+20;
		f=0;
		t1=40;
	}
	public void change(int f1)
	{
		f=f1;
		if(f==0)
		{
			this.setFrameSequence(new int[]{0,1});
		}
		if(f==1)
		{
			this.setFrameSequence(new int[]{2,3});
		}
		if(f==2)
		{
			this.setFrameSequence(new int[]{4,5});
		}
		if(f==3)
		{
			this.setFrameSequence(new int[]{6,7});
		}
	}
	public void move(BJ bj,BJ bj2,WJ wj,ZD nz)
	{
		this.nextFrame();
//		t--;
		
		if(f==0)
		{
			this.move(0,-5);
			if(this.collidesWith(bj,true)==true||this.collidesWith(bj2,true)==true)
			{
//				f=Math.abs(r.nextInt()%4);
				this.move(0,5);
				change(Math.abs(r.nextInt()%4));
//				gai();
			}
		}
		if(f==1)
		{
			this.move(0,5);
			if(this.collidesWith(bj,true)==true||this.collidesWith(bj2,true)==true)
			{
//				f=Math.abs(r.nextInt()%4);
				this.move(0,-5);
				change(Math.abs(r.nextInt()%4));
//				gai();
			}
		}
		if(f==2)
		{
			this.move(-5,0);
			if(this.collidesWith(bj,true)==true||this.collidesWith(bj2,true)==true)
			{
//				f=Math.abs(r.nextInt()%4);
				this.move(5,0);
				change(Math.abs(r.nextInt()%4));
//				gai();
			}
		}
		if(f==3)
		{
			this.move(5,0);
			if(this.collidesWith(bj,true)==true||this.collidesWith(bj2,true)==true)
			{
//				f=Math.abs(r.nextInt()%4);
				this.move(-5,0);
				change(Math.abs(r.nextInt()%4));
//				gai();
			}
		}
		t--;
			if(t<=0)
			{
//				f=Math.abs(r.nextInt()%4);
				change(Math.abs(r.nextInt()%4));
//				gai();
				t=Math.abs(r.nextInt()%40)+20;
			}
			if(this.getX()==wj.getX())
			{
				if(Math.abs(r.nextInt()%100)<30)
				{
				if(this.getY()>wj.getY())
				{
//					f=0;
//					gai();
					change(0);
//					nz.create(this.getX(),this.getY(),f);
				}
				else
				{

//					f=1;
//					gai();
					change(1);
//					nz.create(this.getX(),this.getY(),f);
				}
				}
			}
			if(this.getY()==wj.getY())
			{
				if(Math.abs(r.nextInt()%100)<30)
				{
					if(this.getX()>wj.getX())
					{
						change(2);
					}
					else
					{
						change(3);
					}
				}
			}
			t1--;
			if(t1<=0)
			{
				nz.create(this.getX()+4,this.getY()+4,f);
				t1=40;
			}
		}
//		if(f==1)
//		{
//			this.move(0,5);
//			if(this.collidesWith(bj,true)==true||this.collidesWith(bj2,true)==true)
//			{
//				m=Math.abs(r.nextInt()%4);
//				this.move(0,-5);
//				gai();
//			}
//			if(t1==0)
//			{
//				f=Math.abs(r.nextInt()%4);
//				gai();
//				t1=Math.abs(r.nextInt()%40)+20;
//			}
//			if(this.getX()==wj.getX())
//			{
//				if(this.getY()>wj.getY())
//				{
//					f=0;
//					gai();
//					nz.create(this.getX(),this.getY(),f);
//				}
//				else
//				{
//
//					f=1;
//					gai();
//					nz.create(this.getX(),this.getY(),f);
//				}
//			}
//		}
//		if(f==2)
//		{
//			this.move(-5,0);
//			if(this.collidesWith(bj,true)==true||this.collidesWith(bj2,true)==true)
//			{
//				f=Math.abs(r.nextInt()%4);
//				this.move(5,0);
//				gai();
//			}
//			if(t==0)
//			{
//				f=Math.abs(r.nextInt()%4);
//				gai();
//				t=Math.abs(r.nextInt()%40)+20;
//			}
//			if(this.getX()==wj.getX())
//			{
//				if(this.getY()>wj.getY())
//				{
//					f=0;
//					gai();
//					nz.create(this.getX(),this.getY(),f);
//				}
//				else
//				{
//
//					f=1;
//					gai();
//					nz.create(this.getX(),this.getY(),f);
//				}
//			}
//		}
//		if(f==3)
//		{
//			this.move(5,0);
//			if(this.collidesWith(bj,true)==true||this.collidesWith(bj2,true)==true)
//			{
//				f=Math.abs(r.nextInt()%4);
//				this.move(-5,0);
//				gai();
//			}
//			if(t==0)
//			{
//				f=Math.abs(r.nextInt()%4);
//				gai();
//				t=Math.abs(r.nextInt()%40)+20;
//			}
//			if(this.getX()==wj.getX())
//			{
//				if(this.getY()>wj.getY())
//				{
//					f=0;
//					gai();
//					nz.create(this.getX(),this.getY(),f);
//				}
//				else
//				{
//
//					f=1;
//					gai();
//					nz.create(this.getX(),this.getY(),f);
//				}
//			}
//		}
//		
//	}
//	private void gai() 
//	{
//		if(f==0)
//		{
//			this.setFrafeSequence(new int[]{0,1});
//		}
//		
//		
//	}

}

⌨️ 快捷键说明

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