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

📄 pengzhuang.java

📁 手机游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.game.Sprite;
import java.util.Vector;
import java.util.Random;

public class pengzhuang extends MIDlet
{
	private Display display;
	private myCanvas cs;
	
	public void startApp()
	{
		display=Display.getDisplay(this);
		cs=new myCanvas();
		cs.start();
		display.setCurrent(cs);
	}
	
	public void pauseApp()
	{
	}
	
	public void destroyApp(boolean unconditional)
	{
		cs.exit();
		System.gc();
		notifyDestroyed();
	}
}

class myCanvas extends GameCanvas implements Runnable
{
	private Image img1,img2;//我方坦克、背景图片
	private Sprite sp1;//我方坦克
	private TiledLayer tl1,tlcao;//背景层和草层
	private LayerManager lm1;
	private static final int mapcols=13,maprows=25;//地图13行,25列
	private static final int mapwidth=12,mapheight=6;//单位地图宽和高
	private static final int tankwidth=10,tankheight=10;
	private int[] map;
	private Vector bullets,tanks,deadtanks;//玩家子弹,敌方坦克,敌方被击中的坦克序列 
	private int sp1x=4*mapwidth,sp1y=(maprows-2)*mapheight;
	private int totaltanknum=20;
	private int delay=0,delay1=0;//坦克,被击中坦克子弹的延时
	boolean conti=true;
	int rate=10;
	
	int playerfireinterval=800;//玩家两发子弹之间的最小间隔(毫秒)
	private int firenum=0;
	private long firetime1=0,firetime2=-1;
	
	public myCanvas()
	{
		super(true);
		try
		{
			img1=Image.createImage("/tank.png");
			img2=Image.createImage("/bg.png");
		}catch(Exception exp){
			System.out.println(exp);
		}
		
		sp1=new Sprite(img1,tankwidth,tankheight);
		sp1.setPosition(sp1x,sp1y);
		sp1.setFrame(0);
		
		tl1=new TiledLayer(mapcols,maprows,img2,mapwidth,mapheight);
		
		//草层
		tlcao=new TiledLayer(1,3,img2,mapwidth,mapheight);
		tlcao.setCell(0,0,4);
		tlcao.setCell(0,1,4);
		tlcao.setCell(0,2,4);
		tlcao.setPosition(5*mapwidth,10*mapheight);
		
		lm1=new LayerManager();
		
		 int tmpmap[]={1,1,1,1,1,1,1,1,1,1,1,1,1,
			1,1,1,1,1,1,1,1,1,1,1,1,1,
			1,2,1,2,1,2,1,2,1,2,1,2,1,
			1,2,1,2,1,2,1,2,1,2,1,2,1,
			1,2,1,2,1,2,1,2,1,2,1,2,1,
			1,2,1,2,1,2,3,2,1,2,1,2,1,
			1,2,1,2,1,2,1,2,1,2,1,2,1,
			1,2,1,2,1,2,1,2,1,2,1,2,1,
			1,2,1,2,1,1,1,1,1,2,1,2,1,
			1,1,1,1,1,1,1,1,1,1,1,1,1,
			1,1,1,1,1,4,1,5,1,1,1,1,1,
			2,1,2,2,1,4,1,5,1,2,1,1,2,
			3,1,2,2,1,4,1,5,1,2,1,1,3,
			1,1,1,1,1,1,1,1,1,1,1,1,1,
			1,1,1,1,1,1,1,1,1,1,1,1,1,
			1,2,1,2,1,2,1,2,1,2,1,2,1,
			1,2,1,2,1,2,1,2,1,2,1,2,1,
			1,2,1,2,1,2,2,2,1,2,1,2,1,
			1,2,1,2,1,2,2,2,1,2,1,2,1,
			1,2,1,2,1,2,1,2,1,2,1,2,1,
			1,2,1,2,1,2,1,2,1,2,1,2,1,
			1,2,1,2,1,1,1,1,1,2,1,2,1,
			1,2,1,2,1,2,2,2,1,2,1,2,1,
			1,1,1,1,1,2,1,2,1,1,1,1,1,
			1,1,1,1,1,2,1,2,1,1,1,1,1,
			};
		map=tmpmap;
		bullets=new Vector();//子弹序列
		tanks=new Vector();//敌方坦克序列
		deadtanks=new Vector();//消亡的坦克的序列
		
		for(int i=0;i<map.length;i++)
		{
			int column=i%mapcols;
			int row=(i-column)/mapcols;
			tl1.setCell(column,row,map[i]);
		}
		lm1.append(tlcao);
		lm1.append(sp1);
		lm1.append(tl1);
		
		Graphics g=getGraphics();
		drawScreen(g);
	}
	
	public void start()
	{
		Thread t=new Thread(this);
		t.start();
	}
	
	public void exit()
	{
		conti=false;
	}	
	
	public void run()
	{
		System.out.println("run");
		long st=0,et=0;
		Graphics g=getGraphics();
		while(conti)
		{
			st=System.currentTimeMillis();
			input();
			drawScreen(g);//画玩家子弹
			et=System.currentTimeMillis();
			if((et-st)<rate)
			{
				try
				{
					Thread.sleep(rate-(et-st));
				}catch(Exception exp){
				}
			}
			
			//画敌方坦克
			if(tanks.size()>0)
			{
				delay++;
				if(delay==2)
				{
					drawTank();
					delay=0;
				}
			}
			
			//继续运行被击中坦克的子弹
			delay1++;
			if(delay1==2)
			{
					for(int i=0;i<deadtanks.size();i++)
					{
						tanksprite deadtank=(tanksprite)deadtanks.elementAt(i);
						if(deadtank.bullet.isVisible())
						{
							movebullet(deadtank);
							
							
							//如果玩家坦克击中消失的坦克的子弹的话则该子弹消失
							for(int j=0;j<bullets.size();j++)
							{
								bulletsprite sp1bullet=(bulletsprite)bullets.elementAt(j);
							
								if(sp1bullet.bullet.collidesWith(deadtank.bullet,true))
								{
									//玩家子弹消失
									bullets.removeElementAt(j);
									lm1.remove(sp1bullet.bullet);
									j--;
									
									//敌方子弹消失
									deadtank.bullet.setVisible(false);
								}
							
							}
						}
						else
						{
							deadtanks.removeElementAt(i);
							i--;
						}
				
					}
					delay1=0;
			}
			
			//判断玩家坦克是否被击中
			//sp1isshoted();
					
		}
	}
	
	private void drawScreen(Graphics g)
	{
		g.setColor(0xffffff);
		g.fillRect(0,0,getWidth(),getHeight());
		g.setColor(0x0000ff);
		
		sp1.setPosition(sp1x,sp1y);//玩家坦克的位置
		drawBullet();//画子弹
		lm1.paint(g,40,30);
		flushGraphics();
	}
	
	//判断我方坦克是否被击中
	private void sp1isshoted()
	{
			for(int i=0;i<tanks.size();i++)
			{
				tanksprite tanksp=(tanksprite)tanks.elementAt(i);
				if(tanksp.bullet.collidesWith(sp1,true))
				{
					tanksp.bullet.setVisible(false);
					sp1x=4*mapwidth;
					sp1y=(maprows-2)*mapheight;
					sp1.setFrame(0);
				}
			}
	}
	
	private void drawBullet()
	{
		boolean finish=false;
		
		for(int i=0;i<bullets.size();i++)
		{
				bulletsprite tmp=(bulletsprite)bullets.elementAt(i);//子弹是否越界,如果越界的话就消失
				
				if(tmp.bullet.getY()>maprows*mapheight||tmp.bullet.getY()<0||tmp.bullet.getX()>mapcols*mapwidth||tmp.bullet.getX()<0)
				{
					bullets.removeElementAt(i);
					lm1.remove(tmp.bullet);
					i--;
				}
				else//判断玩家子弹是否击中敌方坦克,如果击中的话就让玩家子弹和敌方坦克消失
				{
					for(int l=0;l<tanks.size();l++)
					{
						tanksprite tanksp=(tanksprite)tanks.elementAt(l);
						
						//击中敌方坦克的话敌方坦克消失
						if(tmp.bullet.collidesWith(tanksp.tank,true))
						{
							finish=true;
							
							bullets.removeElementAt(i);
							lm1.remove(tmp.bullet);
							i--;
							
							deadtanks.addElement(tanksp);
							tanks.removeElementAt(l);
							lm1.remove(tanksp.tank);
							l--;
							
							break;
						}
						
						//击中敌方子弹的话两方子弹消失
						if(tmp.bullet.collidesWith(tanksp.bullet,true))
						{
							finish=true;
							bullets.removeElementAt(i);
							lm1.remove(tmp.bullet);
							i--;
							
							tanksp.bullet.setVisible(false);
							break;
						}
						
					}
				}
				//玩家子弹没有击中敌方坦克
				if(!finish)
				{
					if(tmp.bulletway%4==0)
					{
						tmp.bullet.move(0,-3);
						//如果是砖或者是钢铁的话:则可能打掉,从而使子弹消亡
						for(int j=0;j<3;j++)
							//打砖或钢铁
							switch(tl1.getCell((tmp.bullet.getX()+j)/mapwidth,(tmp.bullet.getY()-1)/mapheight))
							{
								case(2)://前方是砖
									tl1.setCell((tmp.bullet.getX()+j)/mapwidth,(tmp.bullet.getY()-1)/mapheight,1);
									bullets.removeElementAt(i);
									lm1.remove(tmp.bullet);
									i--;
									j=3;//退出循环
								break;
								case(3)://前方钢铁
									bullets.removeElementAt(i);
									lm1.remove(tmp.bullet);
									i--;
									j=3;
								break;
							}	
					}
				
					if(tmp.bulletway%4==1)
					{
						tmp.bullet.move(3,0);
						for(int j=0;j<3;j++)
							//打砖或钢铁
							if(tmp.bullet.getX()+2<mapcols*mapwidth)
							{
								switch(tl1.getCell((tmp.bullet.getX()+1)/mapwidth,(tmp.bullet.getY()+j)/mapheight))
								{
									case(2):
										tl1.setCell((tmp.bullet.getX()+1)/mapwidth,(tmp.bullet.getY()+j)/mapheight,1);
										bullets.removeElementAt(i);
										lm1.remove(tmp.bullet);
										i--;
										j=3;
									break;
									case(3):
										bullets.removeElementAt(i);
										lm1.remove(tmp.bullet);
										i--;
										j=3;
									break;
								}
							}
					}
					
					if(tmp.bulletway%4==2)
					{
						tmp.bullet.move(0,3);
						for(int j=0;j<3;j++)
							//打砖或钢铁
							if(tmp.bullet.getY()+2<maprows*mapheight)
							{	
								switch(tl1.getCell((tmp.bullet.getX()+j)/mapwidth,(tmp.bullet.getY()+1)/mapheight))
								{
									case(2):
										tl1.setCell((tmp.bullet.getX()+j)/mapwidth,(tmp.bullet.getY()+1)/mapheight,1);
										bullets.removeElementAt(i);
										lm1.remove(tmp.bullet);
										i--;
										j=3;
									break;
									case(3):
										bullets.removeElementAt(i);
										lm1.remove(tmp.bullet);
										i--;
										j=3;
									break;
								}
							}
					}
				
					if(tmp.bulletway%4==3)
					{
						tmp.bullet.move(-3,0);
						for(int j=0;j<3;j++)
							//打砖或钢铁
							switch(tl1.getCell((tmp.bullet.getX()-1)/mapwidth,(tmp.bullet.getY()+j)/mapheight))
							{
								case(2):
									tl1.setCell((tmp.bullet.getX()-1)/mapwidth,(tmp.bullet.getY()+j)/mapheight,1);
									bullets.removeElementAt(i);
									lm1.remove(tmp.bullet);
									i--;
									j=3;
								break;
								case(3):
									bullets.removeElementAt(i);
									lm1.remove(tmp.bullet);
									i--;
									j=3;
								break;
							}
					}
				
				  //遇到边界时子弹消亡	
			 	 if(tmp.bullet.getY()>maprows*mapheight||tmp.bullet.getY()<0||tmp.bullet.getX()>mapcols*mapwidth||tmp.bullet.getX()<0)
					{
						bullets.removeElementAt(i);
						lm1.remove(tmp.bullet);
						i--;
						break;
					}
				}
			}
	}
	
	private void drawTank()
	{
		for(int i=0;i<tanks.size();i++)
		{
			tanksprite tankclasstmp=(tanksprite)tanks.elementAt(i);
			
			tankclasstmp.tankwaytimeplus();//记录坦克行走的时间
			tankclasstmp.bulletwaytimeplus();
			
			if(tankclasstmp.tank.getY()>=0&&tankclasstmp.tank.getY()<=maprows*mapheight&&tankclasstmp.tank.getX()>=0&&tankclasstmp.tank.getX()<=mapcols*mapwidth)
			{
				
				//坦克的方向
				switch(tankclasstmp.tank.getFrame())
				{
					case(0):
						if(canMove(tankclasstmp.tank))
						{
							tankclasstmp.tank.move(0,-1);
							if((!tankclasstmp.bullet.isVisible())&&tankclasstmp.getbulletwaytime())
							{
								tankclasstmp.bullet.setPosition(tankclasstmp.tank.getX()+3,tankclasstmp.tank.getY());
								tankclasstmp.setBulletWay(0);
								tankclasstmp.bullet.setVisible(true);
							}
							if(tankclasstmp.gettankwaytime())
							{
								tankclasstmp.tank.setFrame(randomWay());
							}
						}
						else
							tankclasstmp.tank.setFrame(randomWay());
						
						if(tankclasstmp.bullet.isVisible())
							movebullet(tankclasstmp);//移动子弹
						else
							tankclasstmp.setBulletWay(tankclasstmp.tank.getFrame());
							
							
					break;
					
					case(1):
						if(canMove(tankclasstmp.tank))
						{
							tankclasstmp.tank.move(1,0);
							//子弹不可见并且开火后有了一会儿
							if((!tankclasstmp.bullet.isVisible())&&tankclasstmp.getbulletwaytime())
							{
								tankclasstmp.bullet.setPosition(tankclasstmp.tank.getX()+tankwidth,tankclasstmp.tank.getY()+3);
								tankclasstmp.setBulletWay(1);
								tankclasstmp.bullet.setVisible(true);
							}
							if(tankclasstmp.gettankwaytime())
							{
								tankclasstmp.tank.setFrame(randomWay());
							}
						}
						else
							tankclasstmp.tank.setFrame(randomWay());
						
						if(tankclasstmp.bullet.isVisible())
							movebullet(tankclasstmp);//移动子弹
						else
							tankclasstmp.setBulletWay(tankclasstmp.tank.getFrame());
					break;
					
					case(2):
						if(canMove(tankclasstmp.tank))
						{
							tankclasstmp.tank.move(0,1);
							if((!tankclasstmp.bullet.isVisible())&&tankclasstmp.getbulletwaytime())
							{
								tankclasstmp.bullet.setPosition(tankclasstmp.tank.getX()+3,tankclasstmp.tank.getY()+tankheight);
								tankclasstmp.setBulletWay(2);
								tankclasstmp.bullet.setVisible(true);
							}	
							if(tankclasstmp.gettankwaytime())
							{
								tankclasstmp.tank.setFrame(randomWay());
							}
						}
						else
							tankclasstmp.tank.setFrame(randomWay());
							
						if(tankclasstmp.bullet.isVisible())
							movebullet(tankclasstmp);//移动子弹
						else
							tankclasstmp.setBulletWay(tankclasstmp.tank.getFrame());
					break;
					
					case(3):
						if(canMove(tankclasstmp.tank))
						{
							tankclasstmp.tank.move(-1,0);
							if((!tankclasstmp.bullet.isVisible())&&tankclasstmp.getbulletwaytime())
							{
								tankclasstmp.bullet.setPosition(tankclasstmp.tank.getX(),tankclasstmp.tank.getY()+3);
								tankclasstmp.setBulletWay(3);
								tankclasstmp.bullet.setVisible(true);
							}	
							if(tankclasstmp.gettankwaytime())
							{
								tankclasstmp.tank.setFrame(randomWay());
							}
						}
						else
								tankclasstmp.tank.setFrame(randomWay());
								

⌨️ 快捷键说明

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