| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 606 人关注过本帖
标题:代码没啥问题,就是玩一会就会变得很卡,求解答
只看楼主 加入收藏
hsjjgm
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:106
专家分:189
注 册:2013-4-27
结帖率:88.89%
收藏
已结贴  问题点数:40 回复次数:3 
代码没啥问题,就是玩一会就会变得很卡,求解答
坦克大战,代码有点长,见谅
功能基本没什么问题,不过有个问题,就是每次打了打死第二个坦克的时候会变得非常卡,求高手帮忙找出原因
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import *;

public class MyTankGame extends JFrame
{
    //声明
    MyPanel mp;
    public static void main(String[] args)
    {
        MyTankGame mytankgame = new MyTankGame();
    }
    public MyTankGame()
    {
        mp = new MyPanel();
        Thread t2 = new  Thread(mp);
        t2.start();
        mp.setBackground(Color.black);
        this.add(mp);
        this.addKeyListener(mp);
        this.setVisible(true);
        this.setBounds(300,300,400,300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

//定义个MyPanel来画图
class MyPanel extends JPanel implements KeyListener,Runnable
{
    //定义个敌人坦克集合类
    Vector<Enermy> ee = new Vector<Enermy>();
    Hero hero;
    Enermy enermy;
    BufferedReader br;
   
    public MyPanel()
    {
        //初始化英雄
        hero = new Hero(10,10);
        System.out.println("请输入敌人坦克的数目");
        //初始化输入流
        br = new BufferedReader(new InputStreamReader(System.in));
        //定义接受类型
        String str = null;
        try
        {
            str = br.readLine();
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for(int i = 0; i < Integer.parseInt(str); i++)
        {
            enermy = new Enermy((i+1)*50,50);
                enermy.setSpeed(1);
                enermy.setType(2);
                ee.add(enermy);
                Thread t3 = new Thread(enermy);
                t3.start();
        }
    }
    //定义paint()函数并调用画笔
    public void paint(Graphics g)
    {
        //利用父类paint函数初始化画笔
        super.paint(g);
        if(hero.isalive == true)
        {
            hero.setSpeed(4);
            hero.setType(1);
            this.paintTank(hero.x,hero.y,hero.direct,hero.type,g);
        }
        else
            System.exit(0);
        
        //画敌人坦克
        for(int i =0; i < ee.size(); i++)
        {
            Enermy enerm = ee.get(i);
            if(enerm.isalive == true)
            {
                this.paintTank(enerm.x,enerm.y,enerm.direct,enerm.type,g);
            }
            else
                ee.remove(ee.get(i));
        }
        
            for(int j = 0; j < hero.bull.size();j++)
            {
                Bullet bul = hero.bull.get(j);
                if(bul != null&&bul.isalive == true)
                {
                    g.setColor(Color.YELLOW);
                    g.fillOval(bul.x,bul.y,2,2);
                }
                else
                    hero.bull.remove(bul);
            }
        }
   
    //画坦克
    public void paintTank(int x, int y, int direct, int type, Graphics g)
    {
        if(type == 1)
        {
            g.setColor(Color.YELLOW);
            switch(direct)
            {
            //向上
            case 0:
                g.fill3DRect(x, y,5,30,false);
                g.fill3DRect(x+5,y+5,10,20,false);
                g.fill3DRect(x+15,y,5,30,false);
                g.fillOval(x+4,y+10,10,10);
                g.drawLine(x+9,y+15,x+9,y-4);
                break;
            //向右
            case 1:
                g.fill3DRect(x, y,30,5,false);
                g.fill3DRect(x+5,y+5,20,10,false);
                g.fill3DRect(x,y+15,30,5,false);
                g.fillOval(x+10,y+5,10,10);
                g.drawLine(x+15,y+10,x+34,y+10);
                break;
            //向下
            case 2:
                g.fill3DRect(x, y,5,30,false);
                g.fill3DRect(x+5,y+5,10,20,false);
                g.fill3DRect(x+15,y,5,30,false);
                g.fillOval(x+4,y+10,10,10);
                g.drawLine(x+9,y+15,x+9,y+34);
                break;
            //向左
            case 3:
                g.fill3DRect(x, y,30,5,false);
                g.fill3DRect(x+5,y+5,20,10,false);
                g.fill3DRect(x,y+15,30,5,false);
                g.fillOval(x+10,y+5,10,10);
                g.drawLine(x+15,y+10,x-4,y+10);
                break;        
            }            
        }
        else if(type == 2)
        {
            g.setColor(Color.RED);
            switch(direct)
            {
            //向上
            case 0:
                g.fill3DRect(x, y,5,30,false);
                g.fill3DRect(x+5,y+5,10,20,false);
                g.fill3DRect(x+15,y,5,30,false);
                g.fillOval(x+4,y+10,10,10);
                g.drawLine(x+9,y+15,x+9,y-4);
                break;
            //向右
            case 1:
                g.fill3DRect(x, y,30,5,false);
                g.fill3DRect(x+5,y+5,20,10,false);
                g.fill3DRect(x,y+15,30,5,false);
                g.fillOval(x+10,y+5,10,10);
                g.drawLine(x+15,y+10,x+34,y+10);
                break;
            //向下
            case 2:
                g.fill3DRect(x, y,5,30,false);
                g.fill3DRect(x+5,y+5,10,20,false);
                g.fill3DRect(x+15,y,5,30,false);
                g.fillOval(x+4,y+10,10,10);
                g.drawLine(x+9,y+15,x+9,y+34);
                break;
            //向左
            case 3:
                g.fill3DRect(x, y,30,5,false);
                g.fill3DRect(x+5,y+5,20,10,false);
                g.fill3DRect(x,y+15,30,5,false);
                g.fillOval(x+10,y+5,10,10);
                g.drawLine(x+15,y+10,x-4,y+10);
                break;
        }
    }
}

@Override
    public void keyTyped(KeyEvent e)
    {
    }
    //设置WSAD
    @Override
    public void keyPressed(KeyEvent e)
    {
        if(e.getKeyCode() == KeyEvent.VK_W)
        {
                hero.setDirect(0);
                hero.moveUp();
        }
        if(e.getKeyCode() == KeyEvent.VK_D)
        {
                hero.setDirect(1);
                hero.moveRight();
//            }
        }
        else if(e.getKeyCode() == KeyEvent.VK_S)
        {
                hero.setDirect(2);
                hero.moveDown();
        }
        else if(e.getKeyCode() == KeyEvent.VK_A)
        {
                hero.setDirect(3);
                hero.moveLeft();
        }   
        if(e.getKeyCode() == KeyEvent.VK_J)
        {
            hero.fired();
        }
        this.repaint();
    }
    @Override
    public void keyReleased(KeyEvent e)
    {
    }
    @Override
    public void run()
    {
        while(true)
        {
            try
            {
                Thread.sleep(100);
            } catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            for(int i = 0; i < hero.bull.size();i++)
            {
                Bullet bul = hero.bull.get(i);
                for(int j = 0; j < ee.size(); j++)
                {
                    Enermy enermy = ee.get(j);
                    this.hit(bul,enermy);
                }
            }
            this.repaint();
        }
    }
   
    //判断是否碰撞
    public void hit(Bullet a, Enermy b)
    {
        switch(b.direct)
        {
            case 0:
            case 2:
                if(a.x>b.x&&a.x<b.x+20&a.y>b.y&a.y<b.y+30)
                {
                    a.isalive = false;
                    b.isalive = false;
                }
                break;
            case 1:
            case 3:
                if(a.x<b.x+30&&a.x>b.x&&a.y>b.y&&a.y<b.y+20)
                {
                    a.isalive = false;
                    b.isalive = false;
                }
                break;
        }
    }
}

//定义一个坦克类
class Tank
{
    int x, y,direct=1,type=1,speed=1;
    boolean isalive = true;
   
    public Tank(int x, int y)
    {
        this.x = x;
        this.y = 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 getType()
    {
        return type;
    }

    public void setType(int type)
    {
        this.type = type;
    }

    public int getSpeed()
    {
        return speed;
    }

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

    public boolean isIsalive()
    {
        return isalive;
    }

    public void setIsalive(boolean isalive)
    {
        this.isalive = isalive;
    }
}

class Hero extends Tank
{
    //定义子弹为其成员
    Vector<Bullet> bull = new Vector<Bullet>();
    Bullet bu;
    //boolean isalive = true;
    public Hero(int x, int y)
    {
        super(x, y);
    }
   
    //坦克移动
    public void moveUp()
    {
        y-=this.speed;
    }
    public void moveRight()
    {
        x+=this.speed;
    }
    public void moveDown()
    {
        y+=this.speed;
    }
    public void moveLeft()
    {
        x-=this.speed;
    }
   
    //开火
    public void fired()
    {
        switch(this.direct)
        {
            case 0:
                bu = new Bullet(x+10,y,0);
                break;
            case 1:
                bu = new Bullet(x+30,y+10,1);
                break;
            case 2:
                bu = new Bullet(x+10,y+30,2);
                break;
            case 3:
                bu = new Bullet(x,y+10,3);
                break;
        }
        bu.setSpeed(4);
        bull.add(bu);
        Thread t1 = new Thread(bu);
        t1.start();
    }
}

class Enermy extends Tank implements Runnable
{
//    Vector<Bullet> bull = new Vector<Bullet>();
    boolean isalive =true;
    public Enermy(int x, int y)
    {
        super(x,y);
    }

    @Override
    public void run()
    {
        while(true)
        {
            try
            {
                Thread.sleep(50);
            } catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            switch(this.direct)
            {
                case 0:
                    for(int i = 0; i < 30; i++)
                    {
                        if(y>0)
                        {
                            y-=this.speed;
                        }
                        try
                        {
                            Thread.sleep(50);
                        } catch (InterruptedException e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    break;
                case 1:
                    for(int i = 0; i < 30; i++)
                    {
                        if(x<400)
                        {
                            x+=this.speed;
                        }
                        try
                        {
                            Thread.sleep(50);
                        } catch (InterruptedException e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    break;
                case 2:
                    for(int i = 0; i < 30; i++)
                    {
                        if(y<300)
                        {
                            y+=this.speed;
                        }
                        try
                        {
                            Thread.sleep(50);
                        } catch (InterruptedException e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    break;
                case 3:
                    for(int i = 0; i < 30; i++)
                    {
                        if(x>0)
                        {
                            x-=this.speed;
                        }
                        try
                        {
                            Thread.sleep(50);
                        } catch (InterruptedException e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    break;
            }
            //让坦克有新方向
            this.direct = (int)(Math.random()*4);
            //清除僵尸坦克
            if(this.isalive == false) //坑爹啊
            {
                break;
            }
        }
    }
   
}

class Bullet implements Runnable
{
    //定义属性
    int x, y,direct =1,speed = 1;
    boolean isalive = true;
    //初始化
    public Bullet(int x, int y, int direct)
    {
        this.x = x;
        this.y = y;
        this.direct = direct;
    }
    @Override
    public void run()
    {
        while(true)
        {
            if(this.isalive == true)
            {
                try
                {
                    Thread.sleep(50);
                } catch (InterruptedException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                switch(this.direct)
                {
                    case 0:
                        y-=this.speed;
                        break;
                    case 1:
                        x+=this.speed;
                        break;
                    case 2:
                        y+=this.speed;
                        break;
                    case 3:
                        x-=this.speed;
                        break;
                }
                if(x<0||x>396||y<0||y>296)
                {
                    this.isalive = false;
                    break;
                }
            }
        }
    }
   
    public int getSpeed()
    {
        return speed;
    }
    public void setSpeed(int speed)
    {
        this.speed = speed;
    }
   
}
搜索更多相关主题的帖子: import public static 坦克大战 
2013-05-29 14:20
Kingbox_tang
Rank: 7Rank: 7Rank: 7
来 自:天津师范大学
等 级:黑侠
威 望:3
帖 子:146
专家分:677
注 册:2012-11-27
收藏
得分:40 
你看你这个t3.start();是开始一个线程,但是你没有一个.close();也就是说你没关闭线程,所以运行下去会卡。
不知道我这样说对不对。

旨在提高编程水平,学有所用,学有所成,学有所为。
2013-05-29 21:54
hsjjgm
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:106
专家分:189
注 册:2013-4-27
收藏
得分:0 
线程也要关闭吗?我平时发发子弹一点都不卡啊,就是打死敌人坦克才会变卡,才打两个就不行了
2013-05-29 23:31
hsjjgm
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:106
专家分:189
注 册:2013-4-27
收藏
得分:0 
没人指点么
2013-05-31 10:24
快速回复:代码没啥问题,就是玩一会就会变得很卡,求解答
数据加载中...
 
   



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

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