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

📄 pengzhuang.java

📁 手机游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
						if(tankclasstmp.bullet.isVisible())
							movebullet(tankclasstmp);//移动子弹
						else
							tankclasstmp.setBulletWay(tankclasstmp.tank.getFrame());
					break;
				}
				
			}
			else
			{
				tanks.removeElementAt(i);
				lm1.remove(tankclasstmp.tank);
				i--;
				break;
			}
				
		}
	}
	public void movebullet(tanksprite tanksp)
	{
			switch(tanksp.getBulletWay())
			{
				case(0):
					tanksp.bullet.move(0,-2);
					for(int j=0;j<3;j++)
					{
						if(tanksp.bullet.getY()>0)
						{
							switch(tl1.getCell((tanksp.bullet.getX()+j)/mapwidth,(tanksp.bullet.getY()-1)/mapheight))
							{
								case(2):
									tl1.setCell((tanksp.bullet.getX()+j)/mapwidth,(tanksp.bullet.getY()-1)/mapheight,1);
									tanksp.bullet.setVisible(false);
									j=3;
								break;
								case(3):
									tanksp.bullet.setVisible(false);
									j=3;
								break;
							}
						}
						else
						{
							tanksp.bullet.setVisible(false);
							j=3;
						}
					}
				break;
				
				case(1):
					tanksp.bullet.move(2,0);
					for(int j=0;j<3;j++)
					{
						//打砖或钢铁
						if(tanksp.bullet.getX()+2<mapcols*mapwidth)
						{
							switch(tl1.getCell((tanksp.bullet.getX()+1)/mapwidth,(tanksp.bullet.getY()+j)/mapheight))
							{
								case(2):
									tl1.setCell((tanksp.bullet.getX()+1)/mapwidth,(tanksp.bullet.getY()+j)/mapheight,1);
									tanksp.bullet.setVisible(false);
									j=3;
								break;
								case(3):
									tanksp.bullet.setVisible(false);
									j=3;
								break;
							}
						}
						else
						{
								tanksp.bullet.setVisible(false);
								j=3;
						}
					}
				break;
				
				case(2):
					
					tanksp.bullet.move(0,2);
					for(int j=0;j<3;j++)
					{
						//打砖或钢铁
						if(tanksp.bullet.getY()+2<maprows*mapheight)
						{
							switch(tl1.getCell((tanksp.bullet.getX()+j)/mapwidth,(tanksp.bullet.getY()+1)/mapheight))
							{
								case(2):
									tl1.setCell((tanksp.bullet.getX()+j)/mapwidth,(tanksp.bullet.getY()+1)/mapheight,1);
									tanksp.bullet.setVisible(false);
									j=3;
								break;
								case(3):
									tanksp.bullet.setVisible(false);
									j=3;
								break;
							}
						}
						else
						{
							tanksp.bullet.setVisible(false);
							j=3;
						}
					}
				break;
				
				case(3):
					tanksp.bullet.move(-2,0);
					for(int j=0;j<3;j++)
					{
						//打砖或钢铁
						if(tanksp.bullet.getX()>0)
						{
							switch(tl1.getCell((tanksp.bullet.getX()-1)/mapwidth,(tanksp.bullet.getY()+j)/mapheight))
							{
								case(2):
									tl1.setCell((tanksp.bullet.getX()-1)/mapwidth,(tanksp.bullet.getY()+j)/mapheight,1);
									tanksp.bullet.setVisible(false);
									j=3;
								break;
								case(3):
									tanksp.bullet.setVisible(false);
									j=3;
								break;
							}
						}
						else
						{
								tanksp.bullet.setVisible(false);
								j=3;
						}
					}
				break;
			
		}
	}
	
	public int randomWay()
	{
		Random random = new Random();
		int randnum = random.nextInt(); 
   	int tankway = Math.abs(randnum % 4); 
		
		return (tankway);
	}
	
	//按键
	public void input()
	{
		int keystate=getKeyStates();
		//System.out.println(keystate);
		
		if(keystate==512)
		{
			tanksprite tankclass=null;
			
			if(totaltanknum>0)
			{
				tankclass=new tanksprite(totaltanknum--);
				if(tankclass!=null)
				{
					tanks.addElement(tankclass);
					lm1.insert(tankclass.tank,1);
					lm1.insert(tankclass.bullet,1);
				}
			}
			
		}	
		if(keystate==1024)
		{
			if(tanks.size()>0)
			{
				tanksprite tmptank=(tanksprite)tanks.elementAt(0);
				tanks.removeElementAt(0);
				lm1.remove(tmptank.tank);
			}
		}
	
		//我方开火
		if((keystate&FIRE_PRESSED)!=0)
		{
			boolean mayfire=false;
			switch(firenum%2)
			{
				case(0):
					firetime1=System.currentTimeMillis();
				break;
				case(1):
					firetime2=System.currentTimeMillis();
					//firenum=0;
				break;
			}
			if(firetime2-firetime1<playerfireinterval)
				firenum=1;
			else
			{
				firetime1=firetime2;
				mayfire=true;
			}
				
				bulletsprite bullet=null;
			
				if(bullets.size()<1&&mayfire)
				{
					bullet=new bulletsprite();
					switch(sp1.getFrame())//根据当前坦克的方向创建子弹
					{
						case(0):
							bullet.setbulletway(0);
						break;
						case(1):
							bullet.setbulletway(1);
						break;
						case(2):
							bullet.setbulletway(2);
						break;
						case(3):
							bullet.setbulletway(3);
						break;
					}
				}
			
				if(bullet!=null)
				{
					switch(bullet.bulletway)
					{
						case(0):
							bullet.bullet.setPosition(sp1x+3,sp1y);
						break;
						case(1):
							bullet.bullet.setPosition(sp1x+tankwidth,sp1y+3);
						break;	
						case(2):
							bullet.bullet.setPosition(sp1x+3,sp1y+tankheight);
						break;
						case(3):
							bullet.bullet.setPosition(sp1x,sp1y+3);
						break;
					}
				
					bullets.addElement(bullet);
					lm1.insert(bullet.bullet,1);
				}
			
		}
		
		if((keystate&UP_PRESSED)!=0)
		{
			sp1.setFrame(0);
			if(canMove(sp1))
				sp1y--;
		}
		else if((keystate&RIGHT_PRESSED)!=0)
		{
			sp1.setFrame(1);
			if(canMove(sp1))
				sp1x++;
		}
		else if((keystate&DOWN_PRESSED)!=0)
		{
			sp1.setFrame(2);
			if(canMove(sp1))
				sp1y++;
		}
		else if((keystate&LEFT_PRESSED)!=0)
		{
			sp1.setFrame(3);
			if(canMove(sp1))
				sp1x--;
		}
			
	}
	
	public void keyReleased(int keyCode)
	{
	}
	
	//根据坦克前方的物体来判断坦克能否移动
	public boolean canMove(Sprite sp)//(int way)
	{
		int cellnum,x,y;
		
		x=sp.getX();
		y=sp.getY();
		if(sp.getX()<0)
			x=0;
		if(sp.getY()<0)
			y=0;
			
		//敌方坦克之间是否发生碰撞
		for(int i=0;i<tanks.size();i++)
		{
			tanksprite tankclass=(tanksprite)tanks.elementAt(i);
			if(sp!=tankclass.tank)
			{
				switch(sp.getFrame())
				{
					case(0):
						if(sp.getY()==(tankclass.tank.getY()+tankheight)||sp.getY()==(tankclass.tank.getY()+tankheight-1))
							if(sp.getX()>(tankclass.tank.getX()-tankwidth)&&sp.getX()<(tankclass.tank.getX()+tankwidth))
								return false;
					break;
				
					case(1):
						if((sp.getX()+tankwidth)==tankclass.tank.getX()||(sp.getX()+tankwidth)==tankclass.tank.getX()+1)
							if(sp.getY()>(tankclass.tank.getY()-tankheight)&&sp.getY()<(tankclass.tank.getY()+tankheight))
								return false;
					break;
				
					case(2):
						if((sp.getY()+tankheight)==tankclass.tank.getY()||(sp.getY()+tankheight)==tankclass.tank.getY()+1)
							if(sp.getX()>(tankclass.tank.getX()-tankwidth)&&sp.getX()<(tankclass.tank.getX()+tankwidth))
								return false;
					break;
				
					case(3):
						if(sp.getX()==(tankclass.tank.getX()+tankwidth)||sp.getX()==(tankclass.tank.getX()+tankwidth)-1)
							if(sp.getY()>(tankclass.tank.getY()-tankheight)&&sp.getY()<(tankclass.tank.getY()+tankheight))
								return false;
					break;
				}
			}
		}
		
		//我方坦克与敌方坦克之间是否发生碰撞
		if(sp!=sp1)//如果是敌方坦克
		{
			switch(sp.getFrame())
			{
				case(0):
					if(sp.getY()==(sp1y+tankheight)||sp.getY()==(sp1y+tankheight-1))
						if(sp.getX()>(sp1.getX()-tankwidth)&&sp.getX()<(sp1.getX()+tankwidth))
							return false;
				break;
				
				case(1):
					if((sp.getX()+tankwidth)==sp1.getX()||(sp.getX()+tankwidth)==sp1.getX()+1)
						if(sp.getY()>(sp1.getY()-tankheight)&&sp.getY()<(sp1.getY()+tankheight))
							return false;
				break;
				
				case(2):
					if((sp.getY()+tankheight)==sp1.getY()||(sp.getY()+tankheight)==sp1.getY()+1)
						if(sp.getX()>(sp1.getX()-tankwidth)&&sp.getX()<(sp1.getX()+tankwidth))
							return false;
				break;
				
				case(3):
					if(sp.getX()==(sp1.getX()+tankwidth)||sp.getX()==(sp1.getX()+tankwidth)-1)
						if(sp.getY()>(sp1.getY()-tankheight)&&sp.getY()<(sp1.getY()+tankheight))
							return false;
				break;
		
			}
		}
		else//如果是我方坦克的话,判断前方是否是敌方坦克如果是的话我我方坦克不能移动
		{
			
			for(int i=0;i<tanks.size();i++)
			{
				tanksprite tankclass=(tanksprite)tanks.elementAt(i);
				if(sp.collidesWith(tankclass.tank,true))
					return false;
			}
		}
		
		//坦克和墙、草、钢铁、之间是否发生碰撞
		switch(sp.getFrame())
		{
			case(0)://UP_PRESSED
				for(int i=0;i<tankwidth;i++)
				{
					if(y-1<0)
						return false;
					else
						cellnum=tl1.getCell((x+i)/mapwidth,(y-1)/mapheight);
					if(cellnum!=1&&cellnum!=4)
						return false;
				}
			break;
			case(2)://DOWN_PRESSED
			for(int i=0;i<tankwidth;i++)
			{
				if((y+tankheight+1)/mapheight>=maprows)
					return false;
				else
					cellnum=tl1.getCell((x+i)/mapwidth,(y+tankheight+1)/mapheight);
				if(cellnum!=1&&cellnum!=4)
					return false;
			}
			break;
			case(3)://LEFT_PRESSED
			for(int i=0;i<tankheight;i++)
			{
				if(x-1<0)
					return false;
				else
					cellnum=tl1.getCell((x-1)/mapwidth,(y+i)/mapheight);
				if(cellnum!=1&&cellnum!=4)
					return false;
			}
			break;
			case(1)://RIGHT_PRESSED
			for(int i=0;i<tankheight;i++)
			{
				if((x+tankwidth+1)/mapwidth>=mapcols)
					return false;
				else
					cellnum=tl1.getCell((x+tankwidth+1)/mapwidth,(y+i)/mapheight);
				
				if(cellnum!=1&&cellnum!=4)
					return false;
			}
			break;
		}
		
		return true;
	}
}

class bulletsprite
{
	public Sprite bullet;
	public int bulletway;
	public Image bulletImage;
	
	public bulletsprite()
	{
		try
		{
			this.bulletImage=Image.createImage("/bullet.png");
		}catch(Exception e){
			System.out.println(e);
		}
		this.bullet=new Sprite(bulletImage,3,3);
		
	}
	public void setbulletway(int way)
	{
		this.bulletway=way;
	}
}

class tanksprite
{
	public Sprite tank,bullet;
	public Image tankImage,bulletImage;
	public int way=2,bulletway=2;//坦克方向,子弹方向
	public int tankwaytime=0,bulletwaytime=0;//坦克运动时间,子弹运动时间
	public int maxtankwaytime=150,maxbulletwaytime=30;//坦克运行多长时间改变一次方向,子弹至少间隔多长时间发射一次
	
	public tanksprite(int num)
	{
		try
		{
			this.tankImage=Image.createImage("/tank2.png");
			this.bulletImage=Image.createImage("/bullet.png");
		}catch(Exception e){
			System.out.println(e);
		}
		this.tank=new Sprite(tankImage,10,10);
		this.bullet=new Sprite(bulletImage,3,3);
		tank.setFrame(2);
		tank.setPosition(num%3*6*12,1);
		bullet.setPosition(num%3*6*12+3,10);
	}
	//得到坦克的方向和设置坦克的方向
	public int getWay()
	{
		return this.way;
	}
	public void setWay(int way)
	{
		this.way=way;
	}
	//得到子弹的方向和设置子弹的方向
	public int getBulletWay()
	{
		return this.bulletway;
	}
	public void setBulletWay(int bulletway)
	{
		this.bulletway=bulletway;
	}
	public void bulletwaytimeplus()
	{
		this.bulletwaytime++;
	}
	public boolean getbulletwaytime()
	{
		if(bulletwaytime>maxbulletwaytime)
		{
			this.bulletwaytime=0;
			return true;
		}
		else
			return false;
	}
	public void tankwaytimeplus()
	{
		this.tankwaytime++;
	}
	public boolean gettankwaytime()
	{
		if(tankwaytime>maxtankwaytime)
		{
			this.tankwaytime=0;
			return true;
		}
		else
			return false;
	}
}

⌨️ 快捷键说明

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