代码没啥问题,就是玩一会就会变得很卡,求解答
坦克大战,代码有点长,见谅功能基本没什么问题,不过有个问题,就是每次打了打死第二个坦克的时候会变得非常卡,求高手帮忙找出原因
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;
}
}