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

110 篇文章 6 订阅
27 篇文章 1 订阅







首先新建一个子弹类

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

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

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

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

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

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


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



而子弹的自动移动就得到了,和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键开火事件


fire方法内容为:


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


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


最后就可以运行了。

运行结果如图:


全部代码点击下载


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值