JAVA之坦克大战(五)实现坦克与子弹的碰撞,然后消失

思路:judge1,judge方法判断敌方坦克友方子弹和友方坦克敌方子弹的碰撞,并且需要放在多线程之中判断

1.Mypanel中的run方法:
public void run()
{
    while (true)
    {
        for(int  i=0;i<enemyTanks.size();i++)
        {
            EnemyTank enemyTank = enemyTanks.get(i);
            for(int j=0;jbulletVector.size();j++)
            {
                judge1(myTank,enemyTank.bulletVector.get(j));
            }
        }

        for(int i=0;i<enemyTanks.size();i++)
        {
            EnemyTank enemyTank = enemyTanks.get(i);
            for(int j=0;j<myTank.bulletVector.size();j++)
            {
                Bullet bullet = myTank.bulletVector.get(j);
                judge(enemyTank,bullet);
            }
        }
        try
        {
            Thread.sleep(50);
        }
        catch (Exception e)
        {

        }
        repaint();
    }
}

2.两个judge方法:

    public void judge(EnemyTank enemyTank,Bullet bullet)//判断子弹与EnemyTank是否碰撞
    {
        switch (enemyTank.getDir())
        {
            case 0:
            case 2:
                if(bullet.getX()>enemyTank.getX()&&bullet.getX()enemyTank.getY()&&bullet.getY()enemyTank.getX()&&bullet.getX()enemyTank.getY()&&bullet.getY()enemyTank.getX()&&bullet.getX()enemyTank.getY()&&bullet.getY()enemyTank.getX()&&bullet.getX()enemyTank.getY()&&bullet.getY()

你可能感兴趣的:(JAVA学习)