java项目之——坦克大战25

来源:互联网 发布:python抢电影票 编辑:程序博客网 时间:2024/05/29 12:27

本版本:添加一个血块类,让其固定轨迹运动。

定义相关量:

 int x,y,w,h;   TankClient tcClient;      private boolean live = true;

简单轨迹(在游戏轨迹里面,是给出曲线,然后沿着它运动,在这里简单处理为几个点)

private int[][] pos = {{350,300},{360,300},{375,275},{400,200}                      ,{360,270},{365,290},{340,280}} ;  

定义step:

 int step  = 0; 


构造函数方法与画方法:

public  Blood(){   x= pos[0][0];   y= pos[0][1];   w=h=15;   }      public void draw(Graphics g){   if(!live) return;      Color c = g.getColor();   g.setColor(Color.MAGENTA);   g.fillRect(x, y, w, h);   g.setColor(c);          move();                   //花一次 move一次    }

写move方法

 private void move() {    step++;    if(step == pos.length){    step = 0;    }    x = pos[step][0];    y = pos[step][0];    }

2.在坦克类中 写吃方法:

public boolean eat(Blood b){if(this.live && this.getRect().intersects(b.getRect()) && b.isLive()){this.life = 100;b.setLive(false);return true;}return false;}

在加血类中补充:

 public boolean isLive(){       return live;      }      public void setLive(boolean live){        this.live = live;   }

3.在主类中:创建对象,画出血块,调用eat;

Blood b =  new Blood(); 

b.draw(g);    w1.draw(g);    w2.draw(g);myTank.draw(g);myTank.eat(b);

总结;到此坦克项目基本结束,补充一个版本,重新开始,重新加入坦克。

附 加血代码:

public class Blood {   int x,y,w,h;   TankClient tcClient;      private boolean live = true;      int step = 0;      private int[][] pos = {{350,300},{360,300},{375,275},{400,200},{360,270},{365,290},{340,280}} ;      public  Blood(){   x= pos[0][0];   y= pos[0][1];   w=h=15;   }      public void draw(Graphics g){   if(!live) return;      Color c = g.getColor();   g.setColor(Color.MAGENTA);   g.fillRect(x, y, w, h);   g.setColor(c);          move();   }   private void move() {    step++;    if(step == pos.length){    step = 0;    }    x = pos[step][0];    y = pos[step][0];    }      public  Rectangle getRect(){   return new Rectangle(x, y, w, h);   }      public boolean isLive(){       return live;      }      public void setLive(boolean live){        this.live = live;   }   }








0 0
原创粉丝点击