论坛首页 Java企业应用论坛

坦克大战

浏览 12889 次
锁定老帖子 主题:坦克大战
精华帖 (0) :: 良好帖 (4) :: 新手帖 (0) :: 隐藏帖 (7)
作者 正文
   发表时间:2010-08-12   最后修改:2010-08-12
读书时的做的东西,今天把它贴出来,望大家不吝赐教!坦克大战的部份代码我提供下载,大家可以试着去玩一下!那时候写代码没有什么注释,以至于现在都有点看不懂了,见笑了!
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的主类.

  • 大小: 105.1 KB
   发表时间:2010-08-12  
这个坦克大战,让我想起以前的我对java编程是多么的“脆弱”!回想起来一个字:悲!
0 请登录后投票
   发表时间:2010-08-12  
说点啥吧你们.
0 请登录后投票
   发表时间:2010-08-12   最后修改:2010-08-12
haha学习膜拜一下,最近跟着老师做项目,就用的java,于是开始自学java。下了个贪吃蛇的代码在学习,现在有你这个,在游戏中体会学习的乐趣。

3Q!楼主!

如果有注释的话或许会更好
0 请登录后投票
   发表时间:2010-08-12  
myloveiscomealone 写道
说点啥吧你们.


界面很杯具,没有怀旧效果啊
0 请登录后投票
   发表时间:2010-08-12  
aflyeagle 写道
haha学习膜拜一下,最近跟着老师做项目,就用的java,于是开始自学java。下了个贪吃蛇的代码在学习,现在有你这个,在游戏中体会学习的乐趣。

3Q!楼主!

如果有注释的话或许会更好

你还真别说,那时候我也是在学校里学习做的.没有什么规范,有注释绝对会更好!希望你在现在的学习阶段养成良好的编程习惯!
0 请登录后投票
   发表时间:2010-08-12  
linliangyi2007 写道
myloveiscomealone 写道
说点啥吧你们.


界面很杯具,没有怀旧效果啊

做这个坦克大点本身很怀旧了.小时候玩过的小霸王.呵呵!
0 请登录后投票
   发表时间:2010-08-12  
myloveiscomealone 写道
linliangyi2007 写道
myloveiscomealone 写道
说点啥吧你们.


界面很杯具,没有怀旧效果啊

做这个坦克大点本身很怀旧了.小时候玩过的小霸王.呵呵!


   偶玩坏了二个
0 请登录后投票
   发表时间:2010-08-12  
哈哈,我也做过类似的,不过貌似代码比楼主长多了,设计也不好~~
0 请登录后投票
   发表时间:2010-08-12  
报了如下错误

Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:99)
at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:113)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:240)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics