任务
代码
首先构造 Blood 类,含有画出自己的 draw() 方法,漂浮的 flutter() 方法和得到阴影的 getRect() 方法
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Blood {
int x,y,w,h;
private boolean live = true;
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
public Blood() {
this.x = pos[0][0];
this.y = pos[0][1];
this.w = 15;
this.h = 15;
}
private int step = 0;
int[][] pos = {{400,360},{420,370},{430,380},{450,400},{460,420},{470,430},{480,450}};
public void draw(Graphics g) {
if(!live) return ;
Color c = g.getColor();
g.setColor(Color.YELLOW);
g.fillRect(x, y, w, h);
g.setColor(c);
flutter();
}
//飘
public void flutter() {
step++;
if(step == pos.length)
step = 0;
x = pos[step][0];
y = pos[step][1];
}
public Rectangle getRect() {
return new Rectangle(x,y,w,h);
}
}
在 Tank 类中添上 eatBlood() 方法,坦克撞上血块,坦克血回满
//吃血
public boolean eatBlood(Blood b) {
if(this.live && this.getRect().intersects(b.getRect()) && this.good){
this.life = 100;
b.setLive(false);
return true;
}
return false;
}
最后在 TankClient 类中定义出血块类并画出血块