Java-坦克大战2

博客展示了坦克游戏的代码实现,包括我方坦克、子弹动作,敌方坦克不动。但存在我方坦克子弹不能向左及向上发射的问题,代码涉及Java的JFrame、JPanel等类,定义了子弹、坦克等类。

//这里实现了我方坦克以及子弹的动作,敌方坦克不会动,有个BUG,我方坦克子弹不能向左以及向上发射,求助!!!!
package TanKe.lbl;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.util.concurrent.atomic.DoubleAdder;
//JFrame为事件源
public class MytankGame extends JFrame{

Mypanel mp = null;

public static void main(String[] args) {
    MytankGame mg = new MytankGame();

}
//构造方法设置画布属性,定义自己的画框,并将划款添加进JFrame
public MytankGame() {
    this.setTitle("坦克大战");
    mp = new Mypanel();
    this.add(mp);
    //启动线程
    Thread t = new Thread(mp);
    t.start();
    //创建监听
    this.addKeyListener(mp);
    this.setSize(400,300);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}
//定义自己的画框,并且作为监听者类
class Mypanel extends JPanel implements KeyListener,Runnable{
Ltank LL = null;
Vector vv = new Vector();
int enSize = 6;

//构造函数实例化我的坦克
public Mypanel() {
    LL = new Ltank(170,130);//坦克的初始坐标
    //创建敌人坦克引用
    for(int i = 0; i < enSize; i ++) {
        Dtank dd = new Dtank((i+1) * 60, 0);
        //设置颜色
        dd.setColor(1);
        //设置方向
        dd.setDirect(1);
        //将引用添加到集合里
        vv.add(dd);
    }
}

//重写父类的画笔方法
public void paint(Graphics g) {
    super.paint(g);
    //设置地图为黑色
    g.fillRect(0, 0, 400, 300);
    //画出自己的坦克
    this.drawTank(LL.getX(), LL.getY(), g, LL.direct, 0);

    //判断子弹不为空
    if(LL.ss != null&&LL.ss.isLive == true) {
        g.draw3DRect(LL.ss.x, LL.ss.y, 1, 1, false);
    }
    //画出敌方坦克
    for(int i = 0; i < vv.size(); i ++) {

        //取出坦克并且赋予坐标等属性画出来
        this.drawTank(vv.get(i).getX(), vv.get(i).getY(), g, vv.get(i).direct, 1);
    }
}
//画的动作,这么画,如何画
public void drawTank(int x, int y, Graphics g, int direct, int type) {
    switch (type) {
    case 0:
        g.setColor(Color.cyan);
        break;

    case 1:
        g.setColor(Color.blue);
        break;
    }
    switch (direct) {
    //向上
    case 0:
        g.fill3DRect(x, y, 5, 30, false);
        g.fill3DRect(x + 15, y, 5, 30, false);
        g.fill3DRect(x + 5, y + 5, 10, 20, false);
        g.fillOval(x + 5, y + 10, 10, 10);
        g.drawLine(x + 10, y + 15, x + 10, y);
        break;
    //向下
    case 1:
        g.fill3DRect(x, y, 5, 30, false);
        g.fill3DRect(x + 15, y, 5, 30, false);
        g.fill3DRect(x + 5, y + 5, 10, 20, false);
        g.fillOval(x + 5, y + 10, 10, 10);
        g.drawLine(x + 10, y + 15, x + 10, y + 30);
        break;
    //向左
    case 2:
        g.fill3DRect(x, y, 30, 5, false);
        g.fill3DRect(x, y + 15, 30, 5, false);
        g.fill3DRect(x + 5, y + 5, 20, 10, false);
        g.fillOval(x + 10, y + 5, 10, 10);
        g.drawLine(x + 15, y + 10, x, y + 10);
        break;
    //向右
    case 3:
        g.fill3DRect(x, y, 30, 5, false);
        g.fill3DRect(x, y + 15, 30, 5, false);
        g.fill3DRect(x + 5, y + 5, 20, 10, false);
        g.fillOval(x + 10, y + 5, 10, 10);
        g.drawLine(x + 15, y + 10, x + 30, y + 10);
        break;
    }
}
//重写监听者的方法
public void keyPressed(KeyEvent arg0) {
    //识别按键
    if(arg0.getKeyCode() == KeyEvent.VK_W) {
        this.LL.setDirect(0);
        this.LL.moveup();

    }
    else if(arg0.getKeyCode() == KeyEvent.VK_S) {
        this.LL.setDirect(1);
        this.LL.movedown();
    }
    else if(arg0.getKeyCode() == KeyEvent.VK_A) {
        this.LL.setDirect(2);
        this.LL.moveleft();
    }
    else if(arg0.getKeyCode() == KeyEvent.VK_D) {
        this.LL.setDirect(3);
        this.LL.moveright();
    }
    //坦克开火
    if (arg0.getKeyCode() == KeyEvent.VK_J) {
        this.LL.shotDtank();
    }

    this.repaint();     
}
@Override
public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub

}
@Override
public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void run() {
    // TODO Auto-generated method stub
    while(true) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.repaint();
    }
}

}
//定义子弹类,实现线程接口
class Shot implements Runnable{
int x;
int y;
int direct;
int speed = 2;
boolean isLive = true;

public Shot(int x, int y, int direct) {
    this.x = x;
    this.y = y;
    this.direct = direct;
}

//线程方法
    public void run() {
        while(true) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            switch(direct) {
            case 0:
                y = y - speed;
            case 1:
                y = y + speed;
            case 2:
                x = x - speed;
            case 3:
                x = x + speed;
            }
            //何时死亡
            //子弹碰到边缘
            if(x<0||x>400||y<0||y>300) {
                this.isLive = false;
                break;
            }
        }
    }

}

//定义一个坦克类
class Tank {
int x = 0;
int y = 0;
//0,1,2,3分别是上下左右
int direct = 0;
int speed = 1;
int color;
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public Tank( int x, int y) {
this.x = x;
this.y = y;
}
//获取坦克的x,y坐标
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 getDirect() {
    return direct;
}

public void setDirect(int direct) {
    this.direct = direct;
}
//速度
public int getSpeed() {
    return speed;
}

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

}
//我方坦克类
class Ltank extends Tank {
Shot ss = null;

public Ltank(int x, int y) {
    super(x,y);
}
//开火方法
public void shotDtank() {
    //判断子弹方向
    switch(this.direct){
    case 0:
        ss = new Shot(x + 10, y, 0);
        break;
    case 1:
        ss = new Shot(x + 10, y + 30, 1);
        break;
    case 2:
        ss = new Shot(x, y + 10,2);
        break;
    case 3:
        ss = new Shot(x + 30, y + 10,3);
        break;
    }
    //启动子弹线程
    Thread t = new Thread(ss);
    t.start();
}
//坦克向上移动
public void moveup() {
    y= y - speed;
}
//向下移动
public void movedown() {
    y= y + speed;
}
//向左移动
public void moveleft() {
    x= x - speed;
}
//向右移动
public void moveright() {
    x= x + speed;
}

}
//敌方坦克类
class Dtank extends Tank {

public Dtank(int x, int y) {
    super(x,y);
}

}

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值