TankWar 单机(JAVA版) 版本1.0~版本1.4 坦克方向打出多发子弹 并解决子弹不消亡问题_实例化

TankWar 单机(JAVA版) 版本1.0~版本1.4 坦克方向打出多发子弹 并解决子弹不消亡问题_子弹不消亡问题_02

TankWar 单机(JAVA版) 版本1.0~版本1.4 坦克方向打出多发子弹 并解决子弹不消亡问题_子弹不消亡问题_03

TankWar 单机(JAVA版) 版本1.0~版本1.4 坦克方向打出多发子弹 并解决子弹不消亡问题_2d_04

TankWar 单机(JAVA版) 版本1.0~版本1.4 坦克方向打出多发子弹 并解决子弹不消亡问题_子弹不消亡问题_05


首先新建一个子弹类

由于要画子弹,所以变量肯定要有坐标x,y,宽高width,height.方法要有draw方法用来绘制子弹

而子弹移动还有子弹的方向dir.子弹的移动速度speed。

要判断子弹是否消亡 所以还要布尔变量bLive表示当前子弹的存亡状态

所有变量的Get set方法一定要有哦。

只有这些  你会发现子弹是不会动的  只会停留在原地  要怎么才能让子弹动呢?

就是线程,每当我们实例化一个子弹 我们就为其开启一个线程,来实现当前子弹的移动

TankWar 单机(JAVA版) 版本1.0~版本1.4 坦克方向打出多发子弹 并解决子弹不消亡问题_2d_06

至于子弹的消亡 可以根据其坐标是否出了界面边界来判断


TankWar 单机(JAVA版) 版本1.0~版本1.4 坦克方向打出多发子弹 并解决子弹不消亡问题_TankWar 单机JAVA版_07

而子弹的自动移动就得到了,和tank的移动相似

// 子弹的自动移动
@Override
public void run() {
// TODO Auto-generated method stub
while (bLive) {
if (dir == Direction.D) {
y += speed;
}
if (dir == Direction.U) {
y -= speed;
}
if (dir == Direction.L) {
x -= speed;
}
if (dir == Direction.R) {
x += speed;
}
if (dir == Direction.LU) {
y -= Math.sqrt(2) * speed / 2;
x -= Math.sqrt(2) * speed / 2;
}
if (dir == Direction.LD) {
y += Math.sqrt(2) * speed / 2;
x -= Math.sqrt(2) * speed / 2;
}
if (dir == Direction.RU) {
y -= Math.sqrt(2) * speed / 2;
x += Math.sqrt(2) * speed / 2;
}
if (dir == Direction.RD) {
y += Math.sqrt(2) * speed / 2;
x += Math.sqrt(2) * speed / 2;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//如果当前子弹移动出界面边界 使此子弹消亡
if(x>TankClient.SCREENWIDTH||x<0||y<0||y>TankClient.SCREENHEIGHT){
bLive=false;
}
}
}



Missile类全部代码:

package tankWar;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;

public class Missile implements Runnable {
// 子弹的x坐标
private int x;
// 子弹的y坐标
private int y;
// 子弹的宽度
private int width = 10;
// 子弹的高度
private int height = 10;
// 子弹移动的速度
private int speed = 10;
// 子弹发射的方向
private Direction dir;
// 子弹是否消亡 默认存活
private boolean bLive=true;

public Missile() {
super();
// TODO Auto-generated constructor stub
}

public Missile(int x, int y, Direction dir) {
super();
this.x = x;
this.y = y;
this.dir = dir;
// 每实例化一个子弹 就为其开启一个线程
Thread t = new Thread(this);
t.start();
}

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

public int getWidth() {
return width;
}

public void setWidth(int width) {
this.width = width;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}

public Direction getDir() {
return dir;
}

public void setDir(Direction dir) {
this.dir = dir;
}

public int getSpeed() {
return speed;
}

public void setSpeed(int speed) {
this.speed = speed;
}

public boolean isbLive() {
return bLive;
}

public void setbLive(boolean bLive) {
this.bLive = bLive;
}

// 子弹的绘制
public void draw(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Ellipse2D e2 = new Ellipse2D.Double(x, y, width, height);
g2.setColor(Color.RED);
g2.fill(e2);
}

// 子弹的自动移动
@Override
public void run() {
// TODO Auto-generated method stub
while (bLive) {
if (dir == Direction.D) {
y += speed;
}
if (dir == Direction.U) {
y -= speed;
}
if (dir == Direction.L) {
x -= speed;
}
if (dir == Direction.R) {
x += speed;
}
if (dir == Direction.LU) {
y -= Math.sqrt(2) * speed / 2;
x -= Math.sqrt(2) * speed / 2;
}
if (dir == Direction.LD) {
y += Math.sqrt(2) * speed / 2;
x -= Math.sqrt(2) * speed / 2;
}
if (dir == Direction.RU) {
y -= Math.sqrt(2) * speed / 2;
x += Math.sqrt(2) * speed / 2;
}
if (dir == Direction.RD) {
y += Math.sqrt(2) * speed / 2;
x += Math.sqrt(2) * speed / 2;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//如果当前子弹移动出界面边界 使此子弹消亡
if(x>TankClient.SCREENWIDTH||x<0||y<0||y>TankClient.SCREENHEIGHT){
bLive=false;
}
}
}
}

做完这些还没完。

我们还没有在按键事件中判断是否发子弹

在Tank类的键盘事件中加入:按F键开火事件

TankWar 单机(JAVA版) 版本1.0~版本1.4 坦克方向打出多发子弹 并解决子弹不消亡问题_子弹不消亡问题_08

fire方法内容为:

TankWar 单机(JAVA版) 版本1.0~版本1.4 坦克方向打出多发子弹 并解决子弹不消亡问题_实例化_09

在这里实例化了一个子弹。而为了发出多发子弹 所以我们在TankClient类中新增了一个子弹集合missileList。

TankWar 单机(JAVA版) 版本1.0~版本1.4 坦克方向打出多发子弹 并解决子弹不消亡问题_实例化_10


在绘制子弹时可以通过遍历子弹集合来绘制

TankWar 单机(JAVA版) 版本1.0~版本1.4 坦克方向打出多发子弹 并解决子弹不消亡问题_2d_11

最后就可以运行了。

运行结果如图:

TankWar 单机(JAVA版) 版本1.0~版本1.4 坦克方向打出多发子弹 并解决子弹不消亡问题_子弹不消亡问题_12

​全部代码点击下载​