基于java语言的单机版坦克大战源代码Word下载.docx

上传人:b****5 文档编号:17431747 上传时间:2022-12-01 格式:DOCX 页数:15 大小:17.50KB
下载 相关 举报
基于java语言的单机版坦克大战源代码Word下载.docx_第1页
第1页 / 共15页
基于java语言的单机版坦克大战源代码Word下载.docx_第2页
第2页 / 共15页
基于java语言的单机版坦克大战源代码Word下载.docx_第3页
第3页 / 共15页
基于java语言的单机版坦克大战源代码Word下载.docx_第4页
第4页 / 共15页
基于java语言的单机版坦克大战源代码Word下载.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

基于java语言的单机版坦克大战源代码Word下载.docx

《基于java语言的单机版坦克大战源代码Word下载.docx》由会员分享,可在线阅读,更多相关《基于java语言的单机版坦克大战源代码Word下载.docx(15页珍藏版)》请在冰豆网上搜索。

基于java语言的单机版坦克大战源代码Word下载.docx

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

explodescount:

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

tankscount:

"

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

tankslife:

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

if(tanks.size()<

=0){

for(inti=1;

i<

=5;

i++){

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

}}

for(inti=0;

missiles.size();

Missilem=missiles.get(i);

m.hitTanks(tanks);

m.hitTank(myTank);

m.hitWall(w);

//m.hitTank(enemyTank);

m.draw(g);

}myTank.draw(g);

myTank.eat(b);

w.draw(g);

b.draw(g);

//enemyTank.draw(g);

explodes.size();

Explodee=explodes.get(i);

e.draw(g);

}for(inti=0;

tanks.size();

Tankt=tanks.get(i);

t.draw(g);

t.collidesWithWall(w);

t.collidesWithTank(myTank);

t.collidesWithTanks(tanks);

}publicvoidupdate(Graphicsg){

if(offScreenImage==null){

offScreenImage=this.createImage(GAME_WIDTH,GAME_HEIGHT);

}

GraphicsoffGraphis=offScreenImage.getGraphics();

Colorc=offGraphis.getColor();

offGraphis.setColor(Color.GREEN);

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

offGraphis.setColor(c);

paint(offGraphis);

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

}publicvoidlaunchFrame(){

=10;

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

}//TankWarTankFrame=newTankWar();

this.setLocation(200,100);

this.setSize(GAME_WIDTH,GAME_HEIGHT);

this.setResizable(false);

this.setBackground(Color.GREEN);

this.addKeyListener(newKeyMonitor());

this.setVisible(true);

newThread(newPaintThread()).start();

this.addWindowListener(newWindowAdapter(){

publicvoidwindowClosing(WindowEvente){

System.exit

(0);

}});

}/****@author启动一个线程,让其不断的重画**/

privateclassPaintThreadimplementsRunnable{}/**

*publicvoidrun(){

try{

while(true){

repaint();

Thread.sleep

(100);

}}catch(InterruptedExceptione){

e.printStackTrace();

*@author响应键盘操作**/

privateclassKeyMonitorextendsKeyAdapter{

publicvoidkeyReleased(KeyEvente){

myTank.keyReleased(e);

}publicvoidkeyPressed(KeyEvente){

//System.out.println("

ok"

);

myTank.keyPressed(e);

*主函数

*@paramargs

publicstaticvoidmain(String[]args){

newTankWar().launchFrame();

/******************************************************************************添加坦克的类

*****************************************************************************/importjava.awt.*;

publicclassTank{

publicstaticfinalintTANK_WIDTH=30,TANK_HEIGHT=30;

publicstaticfinalintXSPEED=5;

publicstaticfinalintYSPEED=5;

TankWartc;

intx,y;

intoldX,oldY;

booleanbL=false,bU=false,bR=false,bD=false;

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

Directiondir=Direction.STOP;

DirectionptDir=Direction.R;

privateBloodBarbb=newBloodBar();

privateintlife=100;

publicintgetLife(){

returnlife;

}publicvoidsetLife(intlife){

this.life=life;

}privatebooleangood;

publicbooleanisGood(){

returngood;

}privatebooleanlive=true;

publicstaticRandomrn=newRandom();

privateintstep=rn.nextInt

(20)+3;

publicbooleanisLive(){

returnlive;

}publicvoidsetLive(booleanlive){

this.live=live;

}publicTank(intx,inty,booleangood){

this.x=x;

this.y=y;

this.oldX=x;

this.oldY=y;

this.good=good;

}publicTank(intx,inty,booleangood,Directiondir,TankWartc){

this(x,y,good);

this.dir=dir;

this.tc=tc;

}publicvoiddraw(Graphicsg){

if(!

live){

good)tc.tanks.remove(this);

return;

}Colorc=g.getColor();

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

elseg.setColor(Color.BLUE);

g.fillOval(x,y,TANK_WIDTH,TANK_HEIGHT);

g.setColor(c);

if(good)bb.draw(g);

switch(ptDir){

caseL:

g.drawLine(x+TANK_WIDTH/2,y+TANK_HEIGHT/2,x-7,y+TANK_HEIGHT/2);

break;

caseLU:

g.drawLine(x+TANK_WIDTH/2,y+TANK_HEIGHT/2,x,y);

caseU:

g.drawLine(x+TANK_WIDTH/2,y+TANK_HEIGHT/2,x+TANK_WIDTH/2,y-7);

caseRU:

g.drawLine(x+TANK_WIDTH/2,y+TANK_HEIGHT/2,x+TANK_WIDTH,y);

caseR:

g.drawLine(x+TANK_WIDTH/2,y+TANK_HEIGHT/2,x+TANK_WIDTH+7,y+TANK_HEIGHT/2);

caseRD:

g.drawLine(x+TANK_WIDTH/2,y+TANK_HEIGHT/2,x+TANK_WIDTH,y+TANK_HEIGHT);

caseD:

g.drawLine(x+TANK_WIDTH/2,y+TANK_HEIGHT/2,x+TANK_WIDTH/2,y+TANK_HEIGHT+7);

caseLD:

g.drawLine(x+TANK_WIDTH/2,y+TANK_HEIGHT/2,x,y+TANK_HEIGHT);

}move();

}publicvoidmove(){

switch(dir){

x-=XSPEED;

y-=YSPEED;

x+=XSPEED;

y+=YSPEED;

caseSTOP:

}if(this.dir!

=Direction.STOP){

this.ptDir=this.dir;

}if(!

good){

Direction[]dirs=Direction.values();

if(step==0){

step=rn.nextInt

intr=rn.nextInt(dirs.length);

dir=dirs[r];

}step--;

if(rn.nextInt

(40)>

37)this.fire();

}if(x<

10)x=10;

if(y<

35)y=35;

if(x>

TankWar.GAME_WIDTH-Tank.TANK_WIDTH

TankWar.GAME_WIDTH-Tank.TANK_WIDTH-8;

if(y>

TankWar.GAME_HEIGHT-Tank.TANK_HEIGHT-8)

y=TankWar.GAME_HEIGHT-Tank.TANK_HEIGHT-8;

}publicvoidkeyPressed(KeyEvente){

intkey=e.getKeyCode();

switch(key){

caseKeyEvent.VK_F2:

live=true;

life=100;

}break;

caseKeyEvent.VK_LEFT:

bL=true;

caseKeyEvent.VK_UP:

bU=true;

-8)x=break;

caseKeyEvent.VK_RIGHT:

bR=true;

caseKeyEvent.VK_DOWN:

bD=true;

}changeDir();

}publicvoidkeyReleased(KeyEvente){

caseKeyEvent.VK_CONTROL:

fire();

bL=false;

bU=false;

bR=false;

bD=false;

caseKeyEvent.VK_A:

superFire();

}publicvoidchangeDir(){

if(bL&

&

!

bU&

bR&

bD)dir=Direction.L;

elseif(bL&

bU&

bD)dir=Direction.LU;

elseif(!

bL&

bD)dir=Direction.U;

bR&

bD)dir=Direction.RU;

bD)dir=Direction.R;

bD)dir=Direction.RD;

bD)dir=Direction.D;

bD)dir=Direction.LD;

bD)dir=Direction.STOP;

}publicMissilefire(){

live)returnnull;

intx=this.x+Tank.TANK_WIDTH/2-Missile.MISSILE_WIDTH/2;

inty=this.y+Tank.TANK_HEIGHT/2-Missile.MISSILE_HEIGHT/2;

Missilem=newMissile(x,y,ptDir,good,this.tc);

tc.missiles.add(m);

returnm;

}publicMissilefire(Directiondir){

Missilem=newMissile(x,y,dir,good,this.tc);

}privatevoidsuperFire(){

Directiondirs[]=Direction.values();

dirs.length-1;

//tc.missiles.add(fire(dirs[i]));

fire(dirs[i]);

publicRectanglegetRect(){

returnnewRectangle(x,y,this.TANK_WIDTH,this.TANK_HEIGHT);

}publicbooleancollidesWithWall(Wallw){

if(this.live&

this.getRect().intersects(w.getRect())){

//this.dir=Direction.STOP;

stay();

returntrue;

}returnfalse;

}publicbooleancollidesWithTank(Tankt){

if(this!

=t){

t.live&

this.getRect().intersects(t.getRect())){

this.stay();

t.stay();

returnfalse;

}publicbooleancollidesWithTanks(java.util.List<

tanks){

if(collidesWithTank(t))returntrue;

}publicvoidstay(){

x=oldX;

y=oldY;

}publicbooleaneat(Bloodb){

b.isLive()&

this.getRect().intersects(b.getRect())){

this.life=100;

b.setLive(false);

}privateclassBloodBar{

publicvoiddraw(Graphicsg){

Colorc=g.getColor();

g.setColor(Color.WHITE);

g.drawRect(x,y-20,TANK_WIDTH,10);

intw=TANK_WIDTH*life/100;

g.setColor(Color.RED);

g.fillRect(x,y-20,w,10);

}}}/******************************************************************************添加子弹的类

publicclassMissile{

publicstaticfinalintXSPEED=20;

publicstaticfinalintYSPEED=20;

publicstaticfinalintMISSILE_WIDTH=10;

publicstaticfinalintMISSILE_HEIGHT=10;

privatebooleanlive=true;

privatebooleangood;

Tank.Directiondir;

privateTankWartw;

publicMissile(intx,inty,Tank.Directiondir){

}publicMissile(intx,inty,Tank.Directiondir,booleangood,TankWartw){

this(x,y,dir);

this.tw=t

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

当前位置:首页 > 法律文书 > 调解书

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

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