1 Star 0 Fork 0

oosad 004/TankWar_宁诗婷

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Missile.java 4.39 KB
一键复制 编辑 原始数据 按行查看 历史
y 提交于 2023-04-16 20:39 . version 2.5 添加“血块”
import java.awt.*;
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 int x, y;
private boolean good;
Tank.Direction dir;
private boolean live = true;
private 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 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, y, 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();
}
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);
}
}
//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;
}
//intersects可以判断这两个方块是否相交
//如果已经碰撞,那么就将子弹和坦克都设置为false
public boolean hitTank(Tank t){
if(this.live&&this.getRect().intersects(t.getRect())&&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(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;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/oosad22-23-2/tank-war-ning-shiting.git
git@gitee.com:oosad22-23-2/tank-war-ning-shiting.git
oosad22-23-2
tank-war-ning-shiting
TankWar_宁诗婷
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385