?? pengzhuang.java
字號(hào):
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;//坦克,被擊中坦克子彈的延時(shí)
boolean conti=true;
int rate=10;
int playerfireinterval=800;//玩家兩發(fā)子彈之間的最小間隔(毫秒)
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;
}
}
//繼續(xù)運(yùn)行被擊中坦克的子彈
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;//退出循環(huán)
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;
}
}
//遇到邊界時(shí)子彈消亡
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();//記錄坦克行走的時(shí)間
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);//移動(dòng)子彈
else
tankclasstmp.setBulletWay(tankclasstmp.tank.getFrame());
break;
case(1):
if(canMove(tankclasstmp.tank))
{
tankclasstmp.tank.move(1,0);
//子彈不可見并且開火后有了一會(huì)兒
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);//移動(dòng)子彈
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);//移動(dòng)子彈
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());
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -