📄 missile.java
字号:
package com.sun;
import java.awt.*;
import java.util.List;
/**
* 子弹类
* @author sun
*
*/
public class Missile {
public static final int XSPEED=10;
public static final int YSPEED=10;
/**
* 子弹的大小
*/
public static final int WIDTH=10,HEIGHT=10;
int x,y;
TanK.Direction dir;
private boolean live=true;
private boolean good;
private Tanke tc;
public Missile(int x,int y,TanK.Direction dir){
this.x=x;
this.y=y;
this.dir=dir;
}
/**
* 子弹的构造方法
* @param x 子弹的位置x坐标
* @param y 子弹的位置y坐标
* @param good 发射子弹的坦克的身份
* @param dir 发射子弹的坦克的方向
* @param tc 发射子弹的坦克
*/
public Missile(int x, int y,boolean good,TanK.Direction dir, Tanke tc) {
this.x = x;
this.y = y;
this.good=good;
this.dir = dir;
this.tc = tc;
}
public void draw(Graphics g){
if(!live){
tc.missiles.remove(this);
return;
}
Color c=g.getColor();
if(this.isGood()){
g.setColor(Color.black);
}
else g.setColor(Color.red);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
move();
}
void move(){
switch(dir){
case L:
x-=XSPEED;
break;
case LU:
x-=XSPEED;
y-=YSPEED;
break;
case U:
y-=YSPEED;
break;
case RU:
y-=YSPEED;
x+=XSPEED;
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>Tanke.WIDTH||y>Tanke.HEIGHT){
live=false;
tc.missiles.remove(this);
}
}
public boolean isLive() {
return live;
}
public Rectangle getRec(){
return new Rectangle(x,y,WIDTH,HEIGHT);
}
/**
* 射击坦克
* @param t 攻打的坦克
* @return 如果射中返回true,否则false
*/
public boolean hitTanK(TanK t){
if(this.isLive()&&this.getRec().intersects(t.getRec())&&t.isLive()&&this.good!=t.isGood()){
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(x,y,tc);
tc.explodes.add(e);
return true;
}
return false;
}
public boolean hitTanKs(List<TanK> tanks){
for(int i=0;i<tanks.size();i++){
if (hitTanK(tanks.get(i))){
return true;
}
}
return false;
}
/**
* 子弹撞墙检测
* @param w 要撞的墙
* @return 撞到返回true否着false
*/
public boolean hitWall(Wall w){
if(this.live&&this.getRec().intersects(w.getRec())){
this.live=false;
return true;
}
return false;
}
public boolean isGood() {
return good;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -