马士兵坦克大战经典源代码.doc

上传人:b****3 文档编号:2276091 上传时间:2022-10-28 格式:DOC 页数:18 大小:70.50KB
下载 相关 举报
马士兵坦克大战经典源代码.doc_第1页
第1页 / 共18页
马士兵坦克大战经典源代码.doc_第2页
第2页 / 共18页
马士兵坦克大战经典源代码.doc_第3页
第3页 / 共18页
马士兵坦克大战经典源代码.doc_第4页
第4页 / 共18页
马士兵坦克大战经典源代码.doc_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

马士兵坦克大战经典源代码.doc

《马士兵坦克大战经典源代码.doc》由会员分享,可在线阅读,更多相关《马士兵坦克大战经典源代码.doc(18页珍藏版)》请在冰豆网上搜索。

马士兵坦克大战经典源代码.doc

packagecom.bjsxt.tank;

importjava.awt.*;

importjava.awt.event.*;

importjava.util.List;

importjava.util.ArrayList;

/**

*这个类的作用是坦克游戏的主窗口

*@authormashibing

*

*/

publicclassTankClientextendsFrame{

/**

*整个坦克游戏的宽度

*/

publicstaticfinalintGAME_WIDTH=800;

publicstaticfinalintGAME_HEIGHT=600;

TankmyTank=newTank(50,50,true,Tank.Direction.STOP,this);

Wallw1=newWall(100,200,20,150,this),w2=newWall(300,100,300,20,this);

Listexplodes=newArrayList();

Listmissiles=newArrayList();

Listtanks=newArrayList();

ImageoffScreenImage=null;

Bloodb=newBlood();

publicvoidpaint(Graphicsg){

/*

*指明子弹-爆炸-坦克的数量

*以及坦克的生命值

*/

g.drawString("missilescount:

"+missiles.size(),10,50);

g.drawString("explodescount:

"+explodes.size(),10,70);

g.drawString("tankscount:

"+tanks.size(),10,90);

g.drawString("tankslife:

"+myTank.getLife(),10,110);

if(tanks.size()<=0){

for(inti=0;i<5;i++){

tanks.add(newTank(50+40*(i+1),50,false,Tank.Direction.D,this));

}

}

for(inti=0;i

Missilem=missiles.get(i);

m.hitTanks(tanks);

m.hitTank(myTank);

m.hitWall(w1);

m.hitWall(w2);

m.draw(g);

//if(!

m.isLive())missiles.remove(m);

//elsem.draw(g);

}

for(inti=0;i

Explodee=explodes.get(i);

e.draw(g);

}

for(inti=0;i

Tankt=tanks.get(i);

t.collidesWithWall(w1);

t.collidesWithWall(w2);

t.collidesWithTanks(tanks);

t.draw(g);

}

myTank.draw(g);

myTank.eat(b);

w1.draw(g);

w2.draw(g);

b.draw(g);

}

publicvoidupdate(Graphicsg){

if(offScreenImage==null){

offScreenImage=this.createImage(GAME_WIDTH,GAME_HEIGHT);

}

GraphicsgOffScreen=offScreenImage.getGraphics();

Colorc=gOffScreen.getColor();

gOffScreen.setColor(Color.GREEN);

gOffScreen.fillRect(0,0,GAME_WIDTH,GAME_HEIGHT);

gOffScreen.setColor(c);

paint(gOffScreen);

g.drawImage(offScreenImage,0,0,null);

}

/**

*本方法显示坦克主窗口

*

*/

publicvoidlauchFrame(){

for(inti=0;i<10;i++){

tanks.add(newTank(50+40*(i+1),50,false,Tank.Direction.D,this));

}

//this.setLocation(400,300);

this.setSize(GAME_WIDTH,GAME_HEIGHT);

this.setTitle("TankWar");

this.addWindowListener(newWindowAdapter(){

publicvoidwindowClosing(WindowEvente){

System.exit(0);

}

});

this.setResizable(false);

this.setBackground(Color.GREEN);

this.addKeyListener(newKeyMonitor());

setVisible(true);

newThread(newPaintThread()).start();

}

publicstaticvoidmain(String[]args){

TankClienttc=newTankClient();

tc.lauchFrame();

}

privateclassPaintThreadimplementsRunnable{

publicvoidrun(){

while(true){

repaint();

try{

Thread.sleep(50);

}catch(InterruptedExceptione){

e.printStackTrace();

}

}

}

}

privateclassKeyMonitorextendsKeyAdapter{

publicvoidkeyReleased(KeyEvente){

myTank.keyReleased(e);

}

publicvoidkeyPressed(KeyEvente){

myTank.keyPressed(e);

}

}

}

packagecom.bjsxt.tank;

importjava.awt.*;

importjava.awt.event.*;

importjava.util.*;

publicclassTank{

publicstaticfinalintXSPEED=5;

publicstaticfinalintYSPEED=5;

publicstaticfinalintWIDTH=30;

publicstaticfinalintHEIGHT=30;

privatebooleanlive=true;

privateBloodBarbb=newBloodBar();

privateintlife=100;

TankClienttc;

privatebooleangood;

privateintx,y;

privateintoldX,oldY;

privatestaticRandomr=newRandom();

privatebooleanbL=false,bU=false,bR=false,bD=false;

enumDirection{L,LU,U,RU,R,RD,D,LD,STOP};

privateDirectiondir=Direction.STOP;

privateDirectionptDir=Direction.D;

privateintstep=r.nextInt(12)+3;

publicTank(intx,inty,booleangood){

this.x=x;

this.y=y;

this.oldX=x;

this.oldY=y;

this.good=good;

}

publicTank(intx,inty,booleangood,Directiondir,TankClienttc){

this(x,y,good);

this.dir=dir;

this.tc=tc;

}

publicvoiddraw(Graphicsg){

if(!

live){

if(!

good){

tc.tanks.remove(this);

}

return;

}

Colorc=g.getColor();

if(good)g.setColor(Color.RED);

elseg.setColor(Color.BLUE);

g.fillOval(x,y,WIDTH,HEIGHT);

g.setColor(c);

if(good)bb.draw(g);

switch(ptDir){

caseL:

g.drawLine(x+Tank.WIDTH/2,y+Tank.HEIGHT/2,x,y+Tank.HEIGHT/2);

break;

caseLU:

g.drawLine(x+Tank.WIDTH/2,y+Tank.HEIGHT/2,x,y);

break;

caseU:

g.drawLine(x+Tank.WIDTH/2,y+Tank.HEIGHT/2,x+Tank.

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 金融投资

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1