JAVA坦克大战教程

2025-10-29 08:21:51

1、这里我直接给大家代码吧!//菜单选项    JMenuItem ji1=null;    JMenuItem ji2=null;    JMenuItem ji3=null;    //构造函数初始化程序的界面    MyTankGame()    {        jf=new JFrame("坦克大战");        sp=new StartPanel();        jf.setBounds(300, 200, 800, 700);       

JAVA坦克大战教程

2、 //菜单条        jmb=new JMenuBar();        //菜单        jm=new JMenu("游戏");        //开始游戏菜单选项        ji1=new JMenuItem("开始游戏");        ji1.setActionCommand("newgame");        ji1.addActionListener(this);        //存盘退出  

JAVA坦克大战教程

3、点击存盘退出时的方法        {            Recorder.saveExit();            System.exit(0);        }    } }//设置一个开始时的面板,并且实现字体闪动效果class StartPanel extends JPanel implements Runnable {    int times=0;    public void paint(Graphics g)    {        super.paint(g);        g.fillRect(0, 0, 500, 400);     

JAVA坦克大战教程

4、 //将传进来的敌方坦克的集合里的坦克的坐标方向都存在文件里        try        {        bw=new BufferedWriter(new FileWriter("myRecorder.txt"));        for(int i=0;i<v.size();i++)        {            if(v.get(i).isLive)            {            bw.write(allen+""+"\r\n");            String recorder=v.get(i).x+" "+v.get(i).y+" "+v.get(i).direct+"\r\n";            bw.write(recorder);            }        }        }catch (Exception e) {            // TODO: handle exception        }finally        {            try            {            bw.close();            }catch (Exception e) 

JAVA坦克大战教程

5、 //坦克的横纵坐标    int x=0;    int y=0;    //坦克的方向    int direct=0;    int speed=1;    boolean isLive=true;    public int getSpeed() {        return speed;    }    public void setSpeed(int speed) {        this.speed = speed;    }    public int getDirect() {        return direct;    }    public void setDirect(int direct) {        this.direct = direct;    }    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;    }    Tank(int x,int y)    {        this.x=x;        this.y=y;    } }

JAVA坦克大战教程

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢