?? missile.java
字號:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.List;
public class Missile {
int x, y;
Direction dir;
boolean live = true;
boolean good;
TankClient tc;
public static final int WIDTH = 10;
public static final int HEIGHT = 10;
public static final int XSPEED = 10;
public static final int YSPEED = 10;
public Missile(int x, int y, Direction dir, boolean good, TankClient tc) {
this.x = x;
this.y = y;
this.dir = dir;
this.good = good;
this.tc = tc;
}
public void draw(Graphics g) {
if(!live) {
tc.missiles.remove(this);
return;
}
this.collidesWithWall(tc.w1);
this.collidesWithWall(tc.w2);
collidesWithMissiles(tc.missiles);
hitTanks(tc.tanks);
hitTank(tc.myTank);
Color c = g.getColor();
if(good) g.setColor(Color.RED);
else g.setColor(Color.BLACK);
g.fillOval(x, y, WIDTH, HEIGHT);
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;
default:
break;
}
if(x < 0 || y < 0 || x > TankClient.GAME_WIDTH || y > TankClient.GAME_HEIGHT) {
live = false;
}
}
public boolean hitTank(Tank t) {
if(t.live && this.good != t.good && this.getRect().intersects(t.getRect())) {
if(!t.good)
t.live = false;
else {
t.life -= 20;
if(t.life <= 0) t.live = false;
}
live = false;
Explode e = new Explode(x, y, tc);
tc.explodes.add(e);
return true;
}
return false;
}
private boolean hitTanks(List<Tank> tanks) {
for(int i=0; i<tanks.size(); i++) {
Tank t = tanks.get(i);
if(hitTank(t)) {
return true;
}
}
return false;
}
private void collidesWithWall(Wall w) {
if(this.getRect().intersects(w.getRect())) {
this.live = false;
tc.explodes.add(new Explode(x, y, tc));
}
}
private boolean collidesWithMissile(Missile m) {
if(this != m && this.good != m.good && this.getRect().intersects(m.getRect())) {
this.live = false;
m.live = false;
tc.explodes.add(new Explode(x, y, tc));
return true;
}
return false;
}
private void collidesWithMissiles(List<Missile> missiles) {
for(int i=0; i<missiles.size(); i++) {
if(this.collidesWithMissile(missiles.get(i))) {
return;
}
}
}
public Rectangle getRect() {
return new Rectangle(x, y, WIDTH, HEIGHT);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -