坦克大战 70版.docx
- 文档编号:4451839
- 上传时间:2022-12-01
- 格式:DOCX
- 页数:17
- 大小:17.63KB
坦克大战 70版.docx
《坦克大战 70版.docx》由会员分享,可在线阅读,更多相关《坦克大战 70版.docx(17页珍藏版)》请在冰豆网上搜索。
坦克大战70版
packagejava1;
importjavax.swing.*;
importjava.awt.event.*;
importjava.awt.*;
importjava.util.*;
publicclassText1extendsJFrame{
Mypanelmp=null;
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
Text1mm=newText1();
}
publicText1()
{
mp=newMypanel();
Threadt=newThread(mp);
t.start();
this.add(mp);
this.addKeyListener(mp);
this.setSize(400,300);
this.setVisible(true);
}
}
classMypanelextendsJPanelimplementsKeyListener,Runnable
{
Herohero=null;
Vector
Vector
intensize=6;
Imageimage1=null;
Imageimage2=null;
Imageimage3=null;
publicMypanel()
{
hero=newHero(20,200);
for(inti=0;i { Emenyet=newEmeny((i+1)*50,0); et.setColor(0); et.setDriect (2); Threadt=newThread(et); t.start(); Shots=newShot(et.x+10,et.y+30,2); et.ss.add(s); Threadt2=newThread(s); t2.start(); ets.add(et); } image1=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_1.gif")); image2=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_2.gif")); image3=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_3.gif")); } publicvoidpaint(Graphicsg) { super.paint(g); g.fillRect(0,0,400,300); this.drawTank(hero.getX(),hero.getY(),g,this.hero.driect,1); for(inti=0;i {Shotmyshot=hero.ss.get(i); if(myshot! =null&&myshot.islive==true) { g.draw3DRect(myshot.x,myshot.y,1,1,false); } if(myshot.islive==false) { hero.ss.remove(myshot); } } for(inti=0;i { Bombb=bombs.get(i); if(b.life>6) { g.drawImage(image1,b.x,b.y,30,30,this); }elseif(b.life>3) { g.drawImage(image2,b.x,b.y,30,30,this); }else { g.drawImage(image3,b.x,b.y,30,30,this); } b.lifedown(); if(b.life==0) { bombs.remove(b); } } for(inti=0;i { Emenyet=ets.get(i); if(et.islive) { this.drawTank(et.getX(),et.getY(),g,et.getDriect(),0); for(intj=0;j { Shotenemyshot=et.ss.get(j); if(enemyshot.islive) { g.draw3DRect(enemyshot.x,enemyshot.y,1,1,false); } } } } } publicvoidhittank(Shots,Emenyet) { switch(et.driect) { case0: case2: if(s.x>et.x&&s.x { s.islive=false; et.islive=false; Bombb=newBomb(et.x,et.y); bombs.add(b); } case1: case3: if(s.x>et.x&&s.x { s.islive=false; et.islive=false; Bombb=newBomb(et.x,et.y); bombs.add(b); } } } publicvoiddrawTank(intx,inty,Graphicsg,intdriect,inttype) { switch(type) { case0: g.setColor(Color.blue); break; case1: g.setColor(Color.CYAN); break; } switch(driect) { case0: g.fill3DRect(x,y,5,30,false); g.fill3DRect(x+15,y,5,30,false); g.fill3DRect(x+5,y+5,10,20,false); g.fillOval(x+5,y+10,10,10); g.drawLine(x+10,y+15,x+10,y); break; case1: g.fill3DRect(x,y,30,5,false); g.fill3DRect(x,y+15,30,5,false); g.fill3DRect(x+5,y+5,20,10,false); g.fillOval(x+10,y+5,10,10); g.drawLine(x+15,y+10,x+30,y+10); break; case2: g.fill3DRect(x,y,5,30,false); g.fill3DRect(x+15,y,5,30,false); g.fill3DRect(x+5,y+5,10,20,false); g.fillOval(x+5,y+10,10,10); g.drawLine(x+10,y+15,x+10,y+30); break; case3: g.fill3DRect(x,y,30,5,false); g.fill3DRect(x,y+15,30,5,false); g.fill3DRect(x+5,y+5,20,10,false); g.fillOval(x+10,y+5,10,10); g.drawLine(x+15,y+10,x,y+10); break; } } @Override publicvoidkeyPressed(KeyEventarg0){ if(arg0.getKeyCode()==KeyEvent.VK_W) { this.hero.setDriect(0); this.hero.moveup(); }elseif(arg0.getKeyCode()==KeyEvent.VK_D) { this.hero.setDriect (1); this.hero.moveright(); }elseif(arg0.getKeyCode()==KeyEvent.VK_S) { this.hero.setDriect (2); this.hero.movedown(); }elseif(arg0.getKeyCode()==KeyEvent.VK_A) { this.hero.setDriect(3); this.hero.moveleft(); } if(arg0.getKeyCode()==KeyEvent.VK_J) {if(this.hero.ss.size()<5) { this.hero.shotemeny(); } } this.repaint(); //TODOAuto-generatedmethodstub } @Override publicvoidkeyReleased(KeyEventarg0){ //TODOAuto-generatedmethodstub } @Override publicvoidkeyTyped(KeyEventarg0){ //TODOAuto-generatedmethodstub } @Override publicvoidrun(){ while(true) { try { Thread.sleep(100); }catch(Exceptione){ e.printStackTrace(); } //判断是否击中 for(inti=0;i { //quchutanke Shotmyshot=hero.ss.get(i); if(myshot.islive) { for(intj=0;j { Emenyet=ets.get(i); if(et.islive) { this.hittank(myshot,et); } } } } this.repaint(); } } } classShotimplementsRunnable { intx; inty; intdriect; intspeed=5; booleanislive=true; publicShot(intx,inty,intdriect) { this.x=x; this.y=y; this.driect=driect; } @Override publicvoidrun(){ //TODOAuto-generatedmethodstub while(true) {try { Thread.sleep(50); }catch(Exceptione) { } switch(driect){ case0: y-=speed; break; case1: x+=speed; break; case2: y+=speed; break; case3: x-=speed; break; } if(x<0||x>400||y<0||y>300) { this.islive=false; break; } } } } classTank { intcolor; publicintgetColor(){ returncolor; } publicvoidsetColor(intcolor){ this.color=color; } publicintgetSpeed(){ returnspeed; } publicvoidsetSpeed(intspeed){ this.speed=speed; } intspeed=6; intdriect=0; publicintgetDriect(){ returndriect; } publicvoidsetDriect(intdriect){ this.driect=driect; } intx=0; publicintgetX(){ returnx; } publicvoidsetX(intx){ this.x=x; } publicintgetY(){ returny; } publicvoidsetY(inty){ this.y=y; } inty=0; publicTank(intx,inty) { this.x=x; this.y=y; } } classEmenyextendsTankimplementsRunnable { booleanislive=true; Vector publicEmeny(intx,inty) { super(x,y); } @Override publicvoidrun(){ while(true) { switch(this.driect) { case0: for(inti=0;i<20;i++) {try{ Thread.sleep(50); }catch(Exceptione) { e.printStackTrace(); } if(y>0) { y-=speed; }} break; case1: for(inti=0;i<15;i++) {try{ Thread.sleep(50); }catch(Exceptione) { e.printStackTrace(); } if(x<360) { x+=speed; } } break; case2: for(inti=0;i<18;i++) {try{ Thread.sleep(50); }catch(Exceptione) { e.printStackTrace(); } if(y<240) { y+=speed; } } break; case3: for(inti=0;i<10;i++) {try{ Thread.sleep(50); }catch(Exceptione) { e.printStackTrace(); }if(x>0) { x-=speed;} } break; } //suijishudriect this.driect=(int)(Math.random()*4); if(this.islive==false) { break; } } //TODOAuto-generatedmethodstub } } classHeroextendsTank { //Shots=null; Vector Shots=null; publicHero(intx,inty) { super(x,y); } publicvoidshotemeny() { switch(this.driect) { case0: s=newShot(x+10,y,0); ss.add(s); break; case1: s=newShot(x+30,y+10,1); ss.add(s); break; case2: s=newShot(x+10,y+30,2); ss.add(s); break; case3: s=newShot(x,y+10,3); ss.add(s); break; } Threadt=newThread(s); t.start(); } publicvoidmoveup() { y-=speed; } publicvoidmovedown() { y+=speed; } publicvoidmoveleft() { x-=speed; } publicvoidmoveright() { x+=speed; } } classBomb { intx; inty; intlife=9; booleanislive=true; publicBomb(intx,inty) { this.x=x; this.y=y; } publicvoidlifedown() { if(life>0) { life--; }else { this.islive=false; } } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 坦克大战 70版 坦克 大战 70