关于坦克大战java视频的信息

博主:adminadmin 2022-12-22 09:24:07 73

本篇文章给大家谈谈坦克大战java视频,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

看了马士兵老师的坦克大战java程序,想按照自己的思路动手编一下,结果界面背景设置不了,

java 作图 不一定要继承JFrame或者Frame,但是所有的组件必须放到顶层容器,Java Swing 有三种顶层容器

JFrame:用来设计类似于Windows系统中的窗口形式的应用程序。

·JDialog:和JFrame类似,只不过JDialog是用来设计对话框。

·JApplet:用来设计可以在嵌入在网页中的Java小程序。

而且你的代码 Kosese001既然继承了JFrame 那么他的构造就是个JFrame ,你下面又new JFrame("坦克大战");这样何必继承,直接在构造里面实现操作就可以把Kosese001中,

你的背景色没有显示出来,是由于JFrame 是顶层容器,它默认的布局会设置它的背景色,除非你在前面 加上 jfrmMain.setLayout(null);使得布局变成空布局,下面的jfrmMain.getContentPane().setBackground(Color.green); 才会生效,或者你可以通过设置JFrame上组件的背景色来完成同样的效果 比如 mypaint = new MyPaint();

mypaint.setBackground(Color.green); 还有啥不懂的 可以问我。

java写坦克大战、发出第一个子弹时。敌人坦克全部消失是为什么?

switch(ets.direct)

确定是检测tank的移动方向吗?全消失肯定是检测出问题了,认为全打到了

去调试轻易就能找到问题,看看各变量输出都是啥

看样子你的子弹只能向一个方向飞啊

你的tank是25*19的

还不如先把HitEnemyTank改精简一点,不要switch直接下面就行

if(b.xets.x-19b.xets.x+19b.yets.y-25b.yets.y+25);

{ b.isLive=false;

ets.isLive=false;

}

看这下能正常不(会有误差,但不应一下全没了)

你写代码啊把该加的括号都加上,这也是好的习惯b.x(ets.x-19)...

另外,这个检测应是实时的不断的,为啥不用引擎呐

急!!!Java坦克大战 敌人坦克跑了一会儿就全都消失了

//判断敌人坦克是否死亡

if(this.isLive=false)

{

//让坦克死亡后,退出线程

break;

}

将if(this.isLive=false)改为if(this.isLive == false)或者if(!this.isLive)

JAVA坦克大战,这段代码为什么子弹的坐标在变,却不能repaint,但是按下任意键盘的建却重绘了呢?

Mypanel的  run方法里要调用repaint方法    否则你的repaint方法只会在keyPressed发生的时候才调用

修改一下两个地方

(1)

// 键盘获取事件的函数

    public void keyPressed(KeyEvent arg0) {

        // TODO Auto-generated method stub

        if (arg0.getKeyCode() == KeyEvent.VK_J) {

            if (hero.shot.size() 5) {

                hero.shott();

            }

        }

        if (arg0.getKeyCode() == KeyEvent.VK_W) {

            hero.setSDC(hero.getSpeed(), 0, hero.getColor());

            hero.moveUp();

        } else if (arg0.getKeyCode() == KeyEvent.VK_S) {

            hero.setSDC(hero.getSpeed(), 1, hero.getColor());

            hero.moveDown();

        } else if (arg0.getKeyCode() == KeyEvent.VK_A) {

            hero.setSDC(hero.getSpeed(), 2, hero.getColor());

            hero.moveLeft();

        } else if (arg0.getKeyCode() == KeyEvent.VK_D) {

            hero.setSDC(hero.getSpeed(), 3, hero.getColor());

            hero.moveRight();

        }

        /**

         * 这个repaint注释掉

         */

        //this.repaint();

    }

(2)

// 线程

    /**

     * 一秒钟60帧

     */

    public void run() {

        // TODO Auto-generated method stub

        while(true){

            this.repaint();

            try {

                

                Thread.sleep(1000 / 60);

            } catch (InterruptedException e) {

                // TODO 自动生成的 catch 块

                e.printStackTrace();

            }

        }

        

    }

完整代码如下:

import java.awt.*;

import javax.swing.*;

import java.util.*;

import java.awt.event.*;

public class aaa extends JFrame {

    public static void main(String[] args) {

        aaa a1 = new aaa();

        Thread t1 = new Thread(a1.mp);

        t1.start();

    }

    MyPanel mp = null;

    public aaa() {

        mp = new MyPanel();

        this.add(mp);

        this.addKeyListener(mp);

        this.setSize(500, 500);

        this.setVisible(true);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

class MyPanel extends JPanel implements KeyListener, Runnable {

    MyTank hero = null;

    VectorEmenyTank emeny = new VectorEmenyTank();

    int emsize = 5;

    // 键盘获取事件的函数

    public void keyPressed(KeyEvent arg0) {

        // TODO Auto-generated method stub

        if (arg0.getKeyCode() == KeyEvent.VK_J) {

            if (hero.shot.size()  5) {

                hero.shott();

            }

        }

        if (arg0.getKeyCode() == KeyEvent.VK_W) {

            hero.setSDC(hero.getSpeed(), 0, hero.getColor());

            hero.moveUp();

        } else if (arg0.getKeyCode() == KeyEvent.VK_S) {

            hero.setSDC(hero.getSpeed(), 1, hero.getColor());

            hero.moveDown();

        } else if (arg0.getKeyCode() == KeyEvent.VK_A) {

            hero.setSDC(hero.getSpeed(), 2, hero.getColor());

            hero.moveLeft();

        } else if (arg0.getKeyCode() == KeyEvent.VK_D) {

            hero.setSDC(hero.getSpeed(), 3, hero.getColor());

            hero.moveRight();

        }

        /**

         * 这个repaint注释掉

         */

        //this.repaint();

    }

    public void keyReleased(KeyEvent arg0) {

        // TODO Auto-generated method stub

    }

    public void keyTyped(KeyEvent arg0) {

        // TODO Auto-generated method stub

    }

    // 完毕

    public MyPanel() {

        hero = new MyTank(250, 250);

        hero.setSDC(5, 2, 2);

        for (int i = 0; i  emsize; ++i) {

            EmenyTank em = new EmenyTank((i + 1) * 60, 20);

            em.setSDC(5, 1, 1);

            emeny.add(em);

        }

    }

    // 线程

    /**

     * 一秒钟60帧

     */

    public void run() {

        // TODO Auto-generated method stub

        while(true){

            this.repaint();

            try {

                

                Thread.sleep(1000 / 60);

            } catch (InterruptedException e) {

                // TODO 自动生成的 catch 块

                e.printStackTrace();

            }

        }

        

    }

    public void paint(Graphics g) {

        super.paint(g);

        // 画板,坦克得放在画板后头

        g.fillRect(0, 0, 400, 400);

        // paint敌人坦克

        for (int i = 0; i  emeny.size(); ++i) {

            EmenyTank em = null;

            em = emeny.get(i);

            this.drawTank(em.getX(), em.getY(), g, em.getDirect(),

                    em.getColor());

        }

        // 画我自己的坦克

        this.drawTank(hero.getX(), hero.getY(), g, hero.getDirect(),

                hero.getColor());

        // 画出我的子弹

        for (int i = 0; i  hero.shot.size(); i++) {

            Shot myShot = hero.shot.get(i);

            if (myShot != null  myShot.live == true) {

                g.draw3DRect(myShot.x, myShot.y, 2, 2, false);

            }

            if (myShot.live == false) {

                hero.shot.remove(myShot);

            }

        }

    }

    public void drawTank(int x, int y, Graphics g, int direct, int color) {

        // 判断坦克的颜色(敌我)然后画出坦克

        switch (color) {

        case 0:

            g.setColor(Color.BLUE);

            break;

        case 1:

            g.setColor(Color.YELLOW);

            break;

        case 2:

            g.setColor(Color.GREEN);

            break;

        }

        // 判断坦克的方向然后再画出坦克

        switch (direct) {

        case 0:

            g.fill3DRect(x, y, 10, 30, false);

            g.fill3DRect(x + 26, y, 10, 30, false);

            g.fill3DRect(x + 10, y + 5, 16, 20, false);

            g.drawLine(x + 18, y + 15, x + 18, y);

            break;

        case 1:

            g.fill3DRect(x, y, 10, 30, false);

            g.fill3DRect(x + 26, y, 10, 30, false);

            g.fill3DRect(x + 10, y + 5, 16, 20, false);

            g.drawLine(x + 18, y + 15, x + 18, y + 30);

            break;

        case 2:

            g.fill3DRect(x + 3, y - 3, 30, 10, false);

            g.fill3DRect(x + 3, y + 23, 30, 10, false);

            g.fill3DRect(x + 8, y + 7, 20, 16, false);

            g.drawLine(x + 18, y + 15, x + 3, y + 15);

            break;

        case 3:

            g.fill3DRect(x + 3, y - 3, 30, 10, false);

            g.fill3DRect(x + 3, y + 23, 30, 10, false);

            g.fill3DRect(x + 8, y + 7, 20, 16, false);

            g.drawLine(x + 18, y + 15, x + 33, y + 15);

            break;

        }

    }

}

class EmenyTank extends Tank implements Runnable {

    public EmenyTank(int x, int y) {

        // TODO Auto-generated method stub

        super(x, y);

    }

    public void run() {

    }

}

class Shot implements Runnable {

    protected int x;

    protected int y;

    protected int direct;

    protected int speed = 4;

    protected boolean live = true;

    public void setX(int x) {

        this.x = x;

        this.y = y;

    }

    public int getX() {

        return x;

    }

    public int getY() {

        return y;

    }

    public void setDirect(int direct) {

        this.direct = direct;

    }

    public int getDirect() {

        return direct;

    }

    public void setSpeed(int speed) {

        this.speed = speed;

    }

    public int getSpeed() {

        return speed;

    }

    // 子弹的上下左右以及走的速度

    public void run() {

        // TODO Auto-generated method stub

        while (true) {

            try {

                Thread.sleep(100);

            } catch (Exception e) {

            }

            switch (direct) {

            case 0:

                y -= speed;

                break;

            case 1:

                y += speed;

                break;

            case 2:

                x -= speed;

                break;

            case 3:

                x += speed;

                break;

            }

            if (x  400 || x  0 || y  400 || y  0) {

                this.live = false;

                break;

            }

        }

    }

}

class Tank {

    protected int x;

    protected int y;

    protected int speed = 5;

    protected int direct;

    protected int color;

    boolean live;

    public Tank(int x, int y) {

        this.x = x;

        this.y = y;

    }

    public int getX() {

        return x;

    }

    public int getY() {

        return y;

    }

    public void setSDC(int speed, int direct, int color) {

        this.speed = speed;

        this.direct = direct;

        this.color = color;

    }

    public int getSpeed() {

        return speed;

    }

    public int getDirect() {

        return direct;

    }

    public int getColor() {

        return color;

    }

}

class MyTank extends Tank {

    public MyTank(int x, int y) {

        // TODO Auto-generated method stub

        super(x, y);

    }

    VectorShot shot = new VectorShot();

    Shot shota = null;

    public void shott() {

        switch (this.direct) {

        case 0:

            shota = new Shot();

            shota.x = x + 18;

            shota.y = y;

            shota.direct = 0;

            shot.add(shota);

            break;

        case 1:

            shota = new Shot();

            shota.x = x + 18;

            shota.y = y + 30;

            shota.direct = 1;

            shot.add(shota);

            break;

        case 2:

            shota = new Shot();

            shota.x = x + 3;

            shota.y = y + 15;

            shota.direct = 2;

            shot.add(shota);

            break;

        case 3:

            shota = new Shot();

            shota.x = x + 33;

            shota.y = y + 15;

            shota.direct = 3;

            shot.add(shota);

            break;

        }

        Thread t = new Thread(shota);

        t.start();

    }

    public void moveUp() {

        if (y  0) {

            y -= speed;

        }

    }// 我的坦克得在自己的类里定义怎么移动

    public void moveDown() {

        if (y  367) {

            y += speed;

        }

    }

    public void moveLeft() {

        if (x  0) {

            x -= speed;

        }

    }

    public void moveRight() {

        if (x  365) {

            x += speed;

        }

    }

}

JAVA 坦克大战

import java.awt.*;

import javax.swing.*;

public class Tank extends JFrame {

mypane mp=null;

Obj[] objs=new Obj[0];

public Tank() {

setTitle("坦克大战");

setSize(800,600);

pro();

add(new mypane(objs));

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocationRelativeTo(null);

//在这里添加键盘事件、鼠标事件、让坦克移动,修改objs数组对象让他们移动

setVisible(true);

}

private void pro(){

Obj[] tmp=new Obj[objs.length+1];

System.arraycopy(objs,0,tmp,0,objs.length);

tmp[tmp.length-1]=new Obj(1,1,0,1);

objs=tmp;

int num=(int)(Math.random()*5)+1;

for(int i=0;inum;i++){

int x=(int)(Math.random()*getWidth())+1;

int y=(int)(Math.random()*getHeight())+1;

int dir=(int)(Math.random()*4);

Obj[] dst=new Obj[objs.length+1];

System.arraycopy(objs,0,dst,0,objs.length);

dst[dst.length-1]=new Obj(x,y,1,dir);

objs=dst;

}

}

public static void main(String[] args) {

new Tank();

}

}

class Obj{

int x,y;//坦克坐标

int type;

int dir;

public Obj(int x,int y,int type,int dir){

this.x=x;

this.y=y;

this.type=type;

this.dir=dir;

}

}

class mypane extends JPanel{

Obj[] objs;

public mypane(Obj[] objs){

this.objs=objs;

}

public void paint(Graphics g) {

super.paint(g);

for(int i=0;iobjs.length;i++){

Obj obj=objs[i];

drawtank(obj.x,obj.y, g, obj.type, obj.dir);

}

g.dispose();

}

public void drawtank(int x,int y,Graphics g, int type,int direct) {

/*type 为坦克类型,敌方,我方*/

switch(type) {

case 0://我方坦克,设置为红色

g.setColor(Color.red);

break;

case 1://敌方坦克,设置为蓝色

g.setColor(Color.blue);

break;

}

switch(direct) {

case 0://坦克方向朝上

g.drawRect(0+x, 0+y, 5, 30);

g.drawRect(5+x, 5+y, 10,20);

g.drawRect(15+x,0+y, 5,30);

g.drawLine(10+x, 15+y, 10+10+x, 15+y);

break;

case 1://坦克方向朝右

g.drawRect(0+x, 0+y, 30, 5);

g.drawRect(5+x, 5+y, 20, 10);

g.drawRect(0+x, 15+y, 30, 5);

g.drawLine(15+x, 10+y, 30+15+x, 10+10+y);

break;

case 2://方向向下

g.drawRect(0+x, 0+y, 5, 30);

g.drawRect(5+x, 5+y, 10,20);

g.drawRect(15+x,0+y, 5,30);

g.drawLine(10+x, 15+y, 10+10+x, 30+15+y);

break;

case 3://方向向左

g.drawRect(0+x, 0+y, 30, 5);

g.drawRect(5+x, 5+y, 20, 10);

g.drawRect(0+x, 15+y, 30, 5);

g.drawLine(15+x, 10+y, 15+x, 10+10+y);

break;

}

}

}

坦克大战java视频的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、坦克大战java视频的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-22,除非注明,否则均为首码项目网原创文章,转载请注明出处。