坦克类设置发射状态和间隔

该文章描述了一个名为Tank的类,它扩展了FaGame类,包含坦克的尺寸、方向、速度等属性,以及四个移动方法。Tank类还包括子弹发射逻辑,使用线程处理攻击冷却时间,并有状态判断来确保不会连续发射子弹。此外,还定义了获取坦克头部坐标的方法。
摘要由CSDN通过智能技术生成

public class Tank extends FaGame{
//尺寸,方向,速度
private int width = 40;
private int height = 50;
private int speed = 3;
private Direction direction = Direction.UP;
//四个方向图片
private String upimg;
private String downimg;
private String leftimg;
private String rightimg;
//定义子弹冷却状态和间隔时间
private boolean attackcooldown = true;
private attackcooltime = 1000;
//坦克类的构建函数
public Tank (全部args){
super(img,x,y,Game);
this.args = args
}
//上下左右四个方法
public void upward(){
y -=speed;
setimg(upimg);
direction = Direction.UP;
}
//创建setimg方法
public void setimg(String img){
this.img = Toolkit.getDefaultToolkit().getImage(img);
}
//继承线程
class Attackcd extends Thread{
public void run(){
attackcooldown = false;
try{
Thread.sleep(attackcooltime);
}catch(Exception e){
e.printStackTrace();
}
attackcooldown = true;
this.stop();
}
}
//发射方法
public void attack(){
//增加状态判断
if(attackcooldown){
Point p = this.getHeadPoint();
Bullet bullet = new Bullet("子弹图片地址",p.x,p.y,this.youxi,this.direction);
//把子弹加入游戏类的子弹集合
this.Game.bulletlist.add(bullet);
new Attackcd().start();
}
}
//定义子弹原始坐标的方法
public Point getHeadPoint(){
switch(direction){
case UP:
return new Point (x+width/2,y)
}
}
//Override  分别重写父类FaGame两个方法
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

surfing097279

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值