代码拉取完成,页面将自动刷新
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.List;
public class Missile {
public static final int XSPEED = 10;
public static final int YSPEED = 10;
public static final int WIDTH = 10;
public static final int HEIGHT = 10;
private boolean good ;
private int x, y;
//判断子弹是否活着
private boolean live = true;
Tank.Direction dir;
TankClient tc;
public Missile(int x, int y, Tank.Direction ptDir, TankClient tc, boolean good) {
this.x = x;
this.y = y;
this.dir = ptDir;
this.tc = tc;
this.good = good;
}
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
public Missile(int x, int y, Tank.Direction dir) {
this.x = x;
this.y = y;
this.dir = dir;
}
public Missile(int x, int y, Tank.Direction dir, TankClient tc) {
this.x = x;
this.y = y;
this.dir = dir;
this.tc = tc;
}
public void draw(Graphics g) {
if (!live) {
tc.missiles.remove(this);
return;
}
Color c = g.getColor();
g.setColor(Color.BLACK);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
move();
}
//getRect()可以正好拿到包在坦克外面的方块
public Rectangle getRect() {
return new Rectangle(x, y, WIDTH, HEIGHT);
}
public boolean hitTanks(List<Tank> tanks) {
for(int i = 0; i < tanks.size(); i++) {
if(hitTank(tanks.get(i))) {
return true;
}
}
return false;
}
public boolean hitTank(Tank t) {
//System.out.println(this.getRect().intersects(t.getRect()));
if(this.live && this.getRect().intersects(t.getRect()) && t.isLive() && (this.good != t.isGood()) ){
//System.out.println("111111111111111111111");
if(t.isGood()){
t.setLife(t.getLife() - 20);
if(t.getLife() <= 0) { t.setLive(false);
}
}else{
t.setLive(false);
}
this.live = false;
//在这里,以坦克的坐标来画这个爆炸
Explode e = new Explode(t.getX(), t.getY(), tc); tc.explodes.add(e);
return true;
}
return false;
}
public boolean hitWall(Wall w) {
if(this.live &&
this.getRect().intersects(w.getRect())) {
this.live = false; return true;
}
return false;
}
private void move() {
switch (dir) {
case L:
x -= XSPEED;
break;
case LU:
x -= XSPEED;
y -= YSPEED;
break;
case U:
y -= YSPEED;
break;
case RU:
x += XSPEED;
y -= YSPEED;
break;
case R:
x += XSPEED;
break;
case RD:
x += XSPEED;
y += YSPEED;
break;
case D:
y += YSPEED;
break;
case LD:
x -= XSPEED;
y += YSPEED;
break;
}
if (x < 0 || y < 0 || x > TankClient.GAME_WIDTH || y > TankClient.GAME_HEIGHT) {
live = false;
tc.missiles.remove(this);
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。