标题:不知道为什么只有一坦克在发子弹
只看楼主
周攀
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-12-11
结帖率:100%
已结贴  问题点数:20 回复次数:4 
不知道为什么只有一坦克在发子弹
package tangke03;
import java.util.*;
class Alltank {
    int x = 0;
    int y = 0;
    int direct;
    int speed;
    int color;

    public Alltank(int x, int y, int direct, int speed, int color) {
        this.x = x;
        this.y = y;
        this.direct = direct;
        this.speed = speed;
        this.color = color;

    }
    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;
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
    }
}

// 敌人的坦克并做成线程类
class Enemytank extends Alltank implements Runnable {
    boolean isLive = true;   
    Shot a;
   
    Vector<Shot> ve =new Vector<Shot>();        
    public Enemytank(int x, int y, int direct, int speed, int color) {
        super(x, y, direct, speed, color);        
        // TODO Auto-generated constructor stub
    }
    @Override
    // 坦克的线程;
    public void run() {
        while (true) {//eeshot();

            switch (this.direct) {
            case 0:
                for (int i = 0; i <= 30; i++) {
                    if (y > 15) {
                        y -= speed;
                    }
                    try {
                        Thread.sleep(70);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                break;
            case 1:
                for (int i = 0; i <= 30; i++) {
                    if (y < 385) {
                        y += speed;
                    }
                    try {
                        Thread.sleep(70);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                break;
            case 2:
                for (int i = 0; i <= 30; i++) {
                    if (x > 15) {
                        x -= speed;
                    }
                    try {
                        Thread.sleep(70);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                break;
            case 3:
                for (int i = 0; i <= 30; i++) {
                    if (x < 485) {
                        x += speed;
                    }
                    try {
                        Thread.sleep(70);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                break;
            }
            if(isLive){
                if(ve.size()<5){
                switch(direct){
                case 0: a=new Shot(x,y-15,0);ve.add(a);break;
                case 1: a=new Shot(x,y+15,1);ve.add(a);break;
                case 2: a=new Shot(x-15,y,2);ve.add(a);break;
                case 3: a=new Shot(x+15,y,3);ve.add(a);break;
                }
                Thread z=new Thread(a);
                z.start();
                }
            }
            // 让round产生一个数(0到1)*4在int
            this.direct = (int) (Math.random() * 4);
            if (this.isLive == false) {
                break;
            }
        }
        // TODO Auto-generated method stub
    }
}

// 我的坦克
class Mytank extends Alltank {
    Vector<Shot> ss = new Vector<Shot>();
    Shot s = null;

    public Mytank(int x, int y, int direct, int speed, int color) {
        super(x, y, direct, speed, color);
    }

    public void shotEnemy() {

        switch (this.direct) {
        case 0:
            s = new Shot(x, y - 16, 0);
            ss.add(s);
            break;
        case 1:
            s = new Shot(x, y + 16, 1);
            ss.add(s);
            break;
        case 2:
            s = new Shot(x - 16, y, 2);
            ss.add(s);
            break;
        case 3:
            s = new Shot(x + 16, y, 3);
            ss.add(s);
            break;

        }
        // 启动线程
        Thread a = new Thread(s);
        a.start();
    }

    public void a() {
        x += speed;
    }

    public void b() {
        x -= speed;
    }

    public void c() {
        y += speed;
    }

    public void d() {
        y -= speed;
    }
}

class Shot implements Runnable {
    int x;
    int y;
    int direct;
    int speed = 1;
    boolean isLive = true;

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

    @Override
    public void run() {
        
        while (true) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            switch (direct) {
            case 0:
                y -= speed;
                break;
            case 1:
                y += speed;
                break;
            case 2:
                x -= speed;
                break;
            case 3:
                x += speed;
                break;
            }
            if (x < 0 || x > 500 || y < 0 || y > 400) {
                isLive = false;
                break;
            }
        }
    }

}

class Bomb {
    int x;
    int y;
    // 炸弹的生命
    boolean islive = true;
    int left = 900;

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

    public void bombleft() {
        if (left > 0) {
            left--;
        } else {
        }
    }
}

package tangke03;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Tank extends JFrame {
    Myjpanel me;
    public static void main(String[] args) {
        Tank tank = new Tank();
    }
    public Tank() {
        me = new Myjpanel();
        Thread t = new Thread(me);
        t.start();
        this.addKeyListener(me);
        this.add(me);
        ImageIcon Icon = new ImageIcon("picture/8.jpg");
        this.setIconImage(Icon.getImage());
        this.setTitle("坦克大战初始版本");
        this.setLocation(200, 200);
        this.setSize(500, 400);
        this.setDefaultCloseOperation(Tank.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
}
class Myjpanel extends JPanel implements KeyListener, Runnable {
    Mytank my;
    Vector<Enemytank> vt;
    Enemytank sx;
    int enemy = 3;
    Vector<Bomb> boobs = null;
    // 三张图片 一颗炸弹
    Image image1 = null;
    Image image2 = null;
    Image image3 = null;
    //Vector<Shot> ss;
    public Myjpanel() {
        boobs = new Vector<Bomb>();
        my = new Mytank(200, 300, 0, 5, 0);
        vt = new Vector<Enemytank>();
        //敌人坦克的赋值
        for (int i = 0; i < enemy; i++) {
            sx = new Enemytank(50 * i+30 , 40, 1, 1, 1);
            vt.add(sx);   
            Thread a=new Thread(sx);
            a.start();
            
            Shot s=new Shot(vt.get(i).x,vt.get(i).y+15,vt.get(i).direct );
            sx.ve.add(s);
            Thread t=new Thread(s);
            t.start();
            }
        image1 = Toolkit.getDefaultToolkit().getImage(
                Panel.class.getResource("/bomb_1.gif"));
        image2 = Toolkit.getDefaultToolkit().getImage(
                Panel.class.getResource("/bomb_2.gif"));
        image3 = Toolkit.getDefaultToolkit().getImage(
                Panel.class.getResource("/bomb_3.gif"));
    }
    // 子弹和坦克相撞的函数
    public void crash(Shot a, Enemytank b) {
        switch (my.direct) {
        case 0:
        case 1:
            if (a.x >= b.x - 10 && a.x <= b.x + 10 && a.y >= b.y - 15
                    && a.y <= b.y + 15) {
                a.isLive = false;
                b.isLive = false;
                Bomb bomb = new Bomb(b.x - 10, b.y - 15);
                boobs.add(bomb);
            }
        case 2:
        case 3:
            if (a.x >= b.x - 15 && a.x <= b.x + 15 && a.y >= b.y - 10
                    && a.y <= b.y + 10) {
                a.isLive = false;
                b.isLive = false;
                Bomb bomb = new Bomb(b.x - 15, b.y - 10);
                boobs.add(bomb);
            }
        }
    }

    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0, 0, 500, 400);
        this.attribute(my.getX(), my.getY(), my.getDirect(), my.getColor(), g);
        // 画敌人坦克
        for (int i = 0; i < vt.size(); i++) {
            Enemytank at = vt.get(i);
            if (at.isLive) {
                this.attribute(at.getX(), at.getY(), at.getDirect(), at
                        .getColor(), g);
                for(int j=0;j<sx.ve.size();j++){
                Shot t=sx.ve.get(j);
                //System.out.println("第 "+i+"坦克的 "+j+"颗子弹x="+t.x);
                g.setColor(Color.green);
                g.draw3DRect(t.x, t.y, 1, 1, false);
                }
            }        
        }
        
        // 画炸弹
        for (int i = 0; i < boobs.size(); i++) {
            Bomb a = boobs.get(i);
            if (a.left >= 600) {
                g.drawImage(image1, a.x, a.y, 30, 30, this);
            }
            if (a.left >= 300 && a.left < 600) {
                g.drawImage(image2, a.x, a.y, 30, 30, this);
            }
            if (a.left >= 0 && a.left < 300) {
                g.drawImage(image3, a.x, a.y, 30, 30, this);
            }
            a.bombleft();
            // 删炸弹
            if (a.left == 0) {
                boobs.remove(a);
            }
        }        
        
        for (int t = 0; t < my.ss.size(); t++) {// 画子弹
            Shot myshot = my.ss.get(t);

            g.setColor(Color.lightGray);
            g.draw3DRect(myshot.x, myshot.y, 1, 1, false);

            // 删子弹
            if (myshot.isLive == false) {
                my.ss.remove(myshot);

            }
        }

    }

    // 画坦克
    public void attribute(int x, int y, int direct, int color, Graphics g) {
        if (color == 0) {
            g.setColor(Color.orange);
        } else if (color == 1) {
            g.setColor(Color.green);
        }
        if (direct == 0) {
            g.drawLine(x, y, x, y - 15);
            g.fill3DRect(x - 5, y - 5, 10, 13, false);
            g.drawOval(x - 3, y - 3, 5, 5);
            g.fill3DRect(x - 10, y - 15, 5, 30, false);
            g.fill3DRect(x + 5, y - 15, 5, 30, false);
        }
        if (direct == 1) {
            g.drawLine(x, y, x, y + 15);
            g.fill3DRect(x - 5, y - 5, 10, 13, false);
            g.drawOval(x - 3, y - 2, 5, 5);
            g.fill3DRect(x - 10, y - 15, 5, 30, false);
            g.fill3DRect(x + 5, y - 15, 5, 30, false);
        }
        if (direct == 2) {
            g.drawLine(x, y, x - 15, y);
            g.fill3DRect(x - 5, y - 5, 13, 10, false);
            g.drawOval(x - 3, y - 3, 5, 5);
            g.fill3DRect(x - 15, y - 10, 30, 5, false);
            g.fill3DRect(x - 15, y + 5, 30, 5, false);
        }
        if (direct == 3) {
            g.drawLine(x, y, x + 15, y);
            g.fill3DRect(x - 5, y - 5, 13, 10, false);
            g.drawOval(x - 3, y - 3, 5, 5);
            g.fill3DRect(x - 15, y - 10, 30, 5, false);
            g.fill3DRect(x - 15, y + 5, 30, 5, false);
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            this.my.setDirect(3);
            this.my.a();
        }
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            this.my.setDirect(2);
            this.my.b();

        }
        if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            this.my.setDirect(1);
            this.my.c();
        }
        if (e.getKeyCode() == KeyEvent.VK_UP) {
            this.my.setDirect(0);
            this.my.d();
        }
        if (e.getKeyCode() == KeyEvent.VK_J) {
            if (this.my.ss.size() < 5) {
                this.my.shotEnemy();
            }
        }
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }

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

    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            this.repaint();
            // 判断是否击中
            for (int i = 0; i < vt.size(); i++) {
                Enemytank b = vt.get(i);// 取出敌人的坦克
                if (b.isLive) {
                    for (int j = 0; j < my.ss.size(); j++) {
                        Shot a = my.ss.get(j);// 取出子弹
                        if (a.isLive) {
                            this.crash(a, b);
                        }
                    }
                }
            }
        }
    }
}
搜索更多相关主题的帖子: package public return direct import 
2015-12-11 16:10
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2276
专家分:10647
注 册:2015-3-19
得分:20 
代码太长

剑栈风樯各苦辛,别时冰雪到时春
2015-12-11 20:56
周攀
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-12-11
得分:0 
额  在电脑上跑下就知道了啊
2015-12-11 21:38
周攀
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-12-11
得分:0 
问题已经解绝  原因取子弹地方错误
2015-12-12 12:36
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2276
专家分:10647
注 册:2015-3-19
得分:0 


剑栈风樯各苦辛,别时冰雪到时春
2015-12-12 20:52



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-460117-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.094392 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved