?? pengzhuang.java
字號:
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 + -