基于java语言的单机版坦克大战源代码.docx
- 文档编号:4444982
- 上传时间:2022-12-01
- 格式:DOCX
- 页数:23
- 大小:18.71KB
基于java语言的单机版坦克大战源代码.docx
《基于java语言的单机版坦克大战源代码.docx》由会员分享,可在线阅读,更多相关《基于java语言的单机版坦克大战源代码.docx(23页珍藏版)》请在冰豆网上搜索。
基于java语言的单机版坦克大战源代码
/******************************************************************************
显示游戏的主界面
******************************************************************************/
importjava.awt.*;
importjava.awt.event.*;
importjava.util.List;
importjava.util.*;
/**
*
*@author显示坦克的主窗口
*
*/
publicclassTankWarextendsFrame{
/**
*坦克主窗口的宽度
*/
publicstaticfinalintGAME_WIDTH=800;
/**
*坦克主窗口的高度
*/
publicstaticfinalintGAME_HEIGHT=600;
ImageoffScreenImage=null;
TankmyTank=newTank(50,50,true,Tank.Direction.STOP,this);
Wallw=newWall(150,200,20,150,this);
Bloodb=newBlood();
//TankenemyTank=newTank(100,100,false,this);
//Explodee=newExplode(150,150,this);
Missilem=null;
List
List
List
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=1;i<=5;i++){
tanks.add(newTank(50+40*i,50,false,Tank.Direction.D,this));
}
}
for(inti=0;i 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); for(inti=0;i Explodee=explodes.get(i); e.draw(g); } for(inti=0;i 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(){ for(inti=1;i<=10;i++){ 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.*; importjava.awt.event.*; importjava.util.*; 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){ if(! 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); break; caseU: g.drawLine(x+TANK_WIDTH/2,y+TANK_HEIGHT/2,x+TANK_WIDTH/2,y-7); break; caseRU: g.drawLine(x+TANK_WIDTH/2,y+TANK_HEIGHT/2,x+TANK_WIDTH,y); break; caseR: g.drawLine(x+TANK_WIDTH/2,y+TANK_HEIGHT/2,x+TANK_WIDTH+7,y+TANK_HEIGHT/2); break; caseRD: g.drawLine(x+TANK_WIDTH/2,y+TANK_HEIGHT/2,x+TANK_WIDTH,y+TANK_HEIGHT); break; caseD: g.drawLine(x+TANK_WIDTH/2,y+TANK_HEIGHT/2,x+TANK_WIDTH/2,y+TANK_HEIGHT+7); break; caseLD: g.drawLine(x+TANK_WIDTH/2,y+TANK_HEIGHT/2,x,y+TANK_HEIGHT); break; } move(); } publicvoidmove(){ this.oldX=x; this.oldY=y; switch(dir){ caseL: x-=XSPEED; break; caseLU: x-=XSPEED; y-=YSPEED; break; caseU: y-=YSPEED; break; caseRU: x+=XSPEED; y-=YSPEED; break; caseR: x+=XSPEED; break; caseRD: x+=XSPEED; y+=YSPEED; break; caseD: y+=YSPEED; break; caseLD: x-=XSPEED; y+=YSPEED; break; caseSTOP: break; } if(this.dir! =Direction.STOP){ this.ptDir=this.dir; } if(! good){ Direction[]dirs=Direction.values(); if(step==0){ step=rn.nextInt(20)+3; 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-8)x=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: if(! live){ live=true; life=100; } break; caseKeyEvent.VK_LEFT: bL=true; break; caseKeyEvent.VK_UP: bU=true; break; caseKeyEvent.VK_RIGHT: bR=true; break; caseKeyEvent.VK_DOWN: bD=true; break; } changeDir(); } publicvoidkeyReleased(KeyEvente){ intkey=e.getKeyCode(); switch(key){ caseKeyEvent.VK_CONTROL: fire(); break; caseKeyEvent.VK_LEFT: bL=false; break; caseKeyEvent.VK_UP: bU=false; break; caseKeyEvent.VK_RIGHT: bR=false; break; caseKeyEvent.VK_DOWN: bD=false; break; caseKeyEvent.VK_A: superFire(); break; } changeDir(); } publicvoidchangeDir(){ if(bL&&! bU&&! bR&&! bD)dir=Direction.L; elseif(bL&&bU&&! bR&&! bD)dir=Direction.LU; elseif(! bL&&bU&&! bR&&! bD)dir=Direction.U; elseif(! bL&&bU&&bR&&! bD)dir=Direction.RU; elseif(! bL&&! bU&&bR&&! bD)dir=Direction.R; elseif(! bL&&! bU&&bR&&bD)dir=Direction.RD; elseif(! bL&&! bU&&! bR&&bD)dir=Direction.D; elseif(bL&&! bU&&! bR&&bD)dir=Direction.LD; elseif(! bL&&! bU&&! bR&&! bD)dir=Direction.STOP; } publicMissilefire(){ if(! 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){ if(! 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,dir,good,this.tc); tc.missiles.add(m); returnm; } privatevoidsuperFire(){ Directiondirs[]=Direction.values(); for(inti=0;i //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){ if(this.live&&t.live&&this.getRect().intersects(t.getRect())){ this.stay(); t.stay(); returntrue; } } returnfalse; } publicbooleancollidesWithTanks(java.util.List for(inti=0;i Tankt=tanks.get(i); if(collidesWithTank(t))returntrue; } returnfalse; } publicvoidstay(){ x=oldX; y=oldY; } pu
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 基于 java 语言 单机 坦克 大战 源代码
![提示](https://static.bdocx.com/images/bang_tan.gif)