坦克大战
程序员文章站
2022-07-14 17:50:33
...
读书时的做的东西,今天把它贴出来,望大家不吝赐教!坦克大战的部份代码我提供下载,大家可以试着去玩一下!那时候写代码没有什么注释,以至于现在都有点看不懂了,见笑了!
//--------------此类是tank的主类.
package tank; import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.event.KeyEvent; import java.util.List; import java.util.Random; public class Tank { int x; int y; int bdx; int step=0; int superZid=3; int zuanting=0; int zuantcount=0; int guancount=0; int zuantingkai=0; enum Direction{L,LU,U,UR,R,RD,D,DL,STOP}; boolean l=false,u=false,r=false,d=false; Direction dir=Direction.STOP; Direction gunDir=Direction.U; TankClient tc=null; public static final int XSPEED=5; public static final int YSPEED=5; Toolkit tk=Toolkit.getDefaultToolkit(); Image img=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankU1.gif")); Image img1=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankU.gif")); Image lfimg=tk.getImage(TankClient.class.getClassLoader().getResource("images/life.gif")); int Dinx=0,Diny=0; boolean good=false; boolean life=true; public Tank(){} public Tank(int x, int y,Direction dir, TankClient tc,boolean good) { this.x = x; this.y = y; this.dir=dir; this.tc=tc; this.good=good; } public Tank(int x, int y, TankClient tc,boolean good,int bdx) { this.x = x; this.y = y; this.tc=tc; this.good=good; this.bdx=bdx; } public void draw(Graphics g){ if(life){ if(good) { g.drawImage(img, x, y, null); g.drawImage(lfimg, x, y-20, null); } else g.drawImage(img1, x, y, null); move(); } } public void setDirB(){ if(dir==Direction.L) { gunDir=Direction.L; if(this.bdx==2||this.bdx==3||this.bdx==4) img1=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankL2.gif")); else img1=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankL.gif")); } else if(dir==Direction.LU) { gunDir=Direction.LU; if(this.bdx==2||this.bdx==3||this.bdx==4) img1=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankLU2.gif")); else img1=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankLU.gif")); } else if(dir==Direction.U) { gunDir=Direction.U; if(this.bdx==2||this.bdx==3||this.bdx==4) img1=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankU2.gif")); else img1=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankU.gif")); } else if(dir==Direction.UR) { gunDir=Direction.UR; if(this.bdx==2||this.bdx==3||this.bdx==4) img1=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankRU2.gif")); else img1=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankRU.gif")); } else if(dir==Direction.R) { gunDir=Direction.R; if(this.bdx==2||this.bdx==3||this.bdx==4) img1=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankR2.gif")); else img1=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankR.gif")); } else if(dir==Direction.RD) { gunDir=Direction.RD; if(this.bdx==2||this.bdx==3||this.bdx==4) img1=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankRD2.gif")); else img1=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankRD.gif")); } else if(dir==Direction.D) { gunDir=Direction.D; if(this.bdx==2||this.bdx==3||this.bdx==4) img1=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankD2.gif")); else img1=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankD.gif")); } else if(dir==Direction.DL) { gunDir=Direction.DL; if(this.bdx==2||this.bdx==3||this.bdx==4) img1=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankLD2.gif")); else img1=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankLD.gif")); } } public void keyPressed(KeyEvent e) { int key=e.getKeyCode(); switch(key) { case KeyEvent.VK_RIGHT: r=true; break; case KeyEvent.VK_DOWN: d=true; break; case KeyEvent.VK_LEFT: l=true; break; case KeyEvent.VK_UP: u=true; break; } setDir(); } public void setDir(){ if(l&&!u&&!r&&!d) { dir=Direction.L; gunDir=Direction.L; img=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankL1.gif")); } else if(l&&u&&!r&&!d) { dir=Direction.LU; gunDir=Direction.LU; img=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankLU1.gif")); } else if(!l&&u&&!r&&!d) { dir=Direction.U; gunDir=Direction.U; img=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankU1.gif")); } else if(!l&&u&&r&&!d) { dir=Direction.UR; gunDir=Direction.UR; img=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankRU1.gif")); } else if(!l&&!u&&r&&!d) { dir=Direction.R; gunDir=Direction.R; img=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankR1.gif")); } else if(!l&&!u&&r&&d) { dir=Direction.RD; gunDir=Direction.RD; img=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankRD1.gif")); } else if(!l&&!u&&!r&&d) { dir=Direction.D; gunDir=Direction.D; img=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankD1.gif")); } else if(l&&!u&&!r&&d) { dir=Direction.DL; gunDir=Direction.DL; img=tk.getImage(TankClient.class.getClassLoader().getResource("images/tankLD1.gif")); } else if(!l&&!u&&!r&&!d) { dir=Direction.STOP; } } public void move(){ zhangq(tc.walls); zhuangxing(); Dinx=x; Diny=y; if(dir==Direction.L) x-=XSPEED; else if(dir==Direction.LU) { x-=XSPEED; y-=YSPEED; } else if(dir==Direction.U) { y-=YSPEED; } else if(dir==Direction.UR) { x+=XSPEED; y-=YSPEED; } else if(dir==Direction.R) { x+=XSPEED; } else if(dir==Direction.RD) { x+=XSPEED; y+=YSPEED; } else if(dir==Direction.D) { y+=YSPEED; } else if(dir==Direction.DL) { x-=XSPEED; y+=YSPEED; } if(x>tc.getWidth()-50) { stop(); }else if(x<0) { stop(); } if (y>tc.getHeight()-50) { stop(); }else if(y<20) { stop(); } Direction[] dirs = Direction.values(); if(zuanting==0) { if(!good) { if(step==0) { Random r=new Random(); dir=dirs[r.nextInt(9)]; setDirB(); step=10+r.nextInt(20); } step--; Random k=new Random(); if(k.nextInt(10)==0) { tc.bullets.add(fire()); } } } if(zuanting==1){ if(zuantingkai==0){ this.dir=dir=Direction.STOP; zuantingkai=1; } } if(zuanting==1) { if(zuantcount==200) { for(int i=0;i<tc.enemy.size();i++) { ((Tank)tc.enemy.get(i)).zuanting=0; ((Tank)tc.enemy.get(i)).zuantcount=0; } tc.myTank.zuanting=0; tc.myTank.zuantcount=0; } zuantcount++; } } public void zhuangxing() { for(int i=0;i<tc.dj.size();i++) { if(good) { if(this.getRectangle().intersects(((Csqj)tc.dj.get(i)).getRectangle())) { if(((Csqj)tc.dj.get(i)).getDj()==0) { tc.setFlaga(false); tc.gaiditu(1); tc.lopad(); tc.goujimiaoshan(1); }else if(((Csqj)tc.dj.get(i)).getDj()==1) { zuantingkai=0; for(int n=0;n<tc.enemy.size();n++) { ((Tank)tc.enemy.get(n)).zuanting=1; } tc.myTank.zuanting=1; } else if(((Csqj)tc.dj.get(i)).getDj()==2) { tc.zidsuducount=1; } else if(((Csqj)tc.dj.get(i)).getDj()==3) { tc.addlife(); } else if(((Csqj)tc.dj.get(i)).getDj()==4) { this.superZid+=3; tc.superZid+=3; } else if(((Csqj)tc.dj.get(i)).getDj()==5) { tc.flaga=true;tc.gb=1; } else if(((Csqj)tc.dj.get(i)).getDj()==6) { tc.husheng=1; } tc.dj.remove(i); } } } } public void keyReleased(KeyEvent e) { int key=e.getKeyCode(); switch(key) { case KeyEvent.VK_CONTROL: if(life){ tc.bullets.add(fire()); } break; case KeyEvent.VK_RIGHT: r=false; break; case KeyEvent.VK_DOWN: d=false; break; case KeyEvent.VK_LEFT: l=false; break; case KeyEvent.VK_K: break; case KeyEvent.VK_UP: u=false; break; case KeyEvent.VK_F1: if(guancount==0) {tc.guan(); guancount=1; }else { tc.kai(); guancount=0; } break; case KeyEvent.VK_C: if(life&&superZid>0){ superfire(); } if(superZid>0) { superZid--; tc.superZid--; } break; } setDir(); } public void stop() { x=Dinx; y=Diny; } public void superfire(){ Direction[] dirs=Direction.values(); for(int i=0;i<8;i++) { Zid b1=new Zid(x,y,dirs[i],tc,this.good); tc.bullets.add(b1); } } public Zid fire(){ Zid b=new Zid(x,y,gunDir,tc,this.good); return b; } public boolean isGood() { return good; } public boolean isLife() { return life; } public Rectangle getRectangle(){ return new Rectangle(x,y,50,50); } public void zhangq(List<Wall> wall){ for(int i=0;i<wall.size();i++){ if(this.getRectangle().intersects(wall.get(i).getRectangle())) { if ((int)wall.get(i).shu!=3){ stop(); } } } } public boolean zhangtk(Tank tk) { boolean flag=false; if(this!=tk) flag=this.getRectangle().intersects(tk.getRectangle()); return flag; } public void setDir(Direction dir) { this.dir = dir; } public void setGunDir(Direction gunDir) { this.gunDir = gunDir; } }
//--------------此类是tank的主类.
上一篇: 两道C语言题 CC++C#
下一篇: 生活杂感 生活StrutsBlog工作