📄 missile.java
字号:
package org.MyTank;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.List;
public class Missile {
private int x, y;
Direction dir;
private TankWarGUI tw;
private boolean good;
private boolean live = true;
public static final double XSPEED = 10.0;
public static final double YSPEED = 10.0;
public static final int WIDTH = 10;
public static final int HEIGHT = 10;
public Missile(int x, int y, Direction dir) {
this.x = x;
this.y = y;
this.dir = dir;
}
public Missile(int x, int y, boolean good, Direction dir, TankWarGUI tw) {
this(x, y, dir);
this.good = good;
this.tw = tw;
}
public void draw(Graphics g) {
if (!this.live) {
tw.missiles.remove(this);
return;
}
Color c = g.getColor();
if (!good) {
g.setColor(Color.BLACK);
} else
g.setColor(Color.BLUE);
g.fillOval(x, y, 10, 10);
g.setColor(c);
move();
}
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 > TankWarGUI.GAME_WIDTH
|| y > TankWarGUI.GAME_HEIGHT) {
live = false;
}
}
private Rectangle getRec() {
return new Rectangle(x, y, WIDTH, HEIGHT);
}
private Explode getExp(Tank t) {
int x, y;
x = t.getRec().x;
y = t.getRec().y;
return new Explode(x, y, tw);
}
public boolean hitTank(Tank t) {
if (this.live && this.getRec().intersects(t.getRec()) && t.isLive()
&& this.good != t.isGood() && t.isLive()) {
if (t.isGood()){
t.setLife(t.getLife() - 20);
if(t.getLife() <= 0) t.setLive(false);
}else t.setLive(false);
this.live = false;
tw.explodes.add(this.getExp(t));
return true;
}
return false;
}
public boolean hitWall(Wall w) {
if (this.live && this.getRec().intersects(w.getRec())) {
this.live = false;
return true;
}
return false;
}
public boolean hitTanks(List<Tank> t) {
for (int i = 0; i < t.size(); i++) {
if (hitTank(t.get(i)))
return true;
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -