太空侵略者 - 奇怪的运动与外星人运动算法 [英] Space Invaders - strange movement with alien movement algorithm

查看:139
本文介绍了太空侵略者 - 奇怪的运动与外星人运动算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经工作了一段时间的太空侵略者的克隆。我至今所面临的最大问题是让外国人来移动。很多的努力后,我已经开发ñ算法来移动它们。该算法的行为与预期不同的是当外星人已经完成移动到左/右,然后向下移动,向左或最右边的外国人开始收敛旁边两个重叠的精灵。监测与其X坐标,我发现他们移动10个像素他们下移左/右每次。我想不通他们为什么这样做,我希望你能帮助我。这是我的运动算法(外星人排成2排10和该方法被称为每十帧 movestate 规定了哪些方向上的外星人正在朝着。):

 公共无效alienMove()
    {
        布尔下移= FALSE;
        如果(movestate)
        {
            的for(int i = 0; I< aliens.length;我++)
            {
                如果(外星人[I] .getX()== 950)
                {
                    movestate = FALSE;
                    下移= TRUE;
                }
                其他
                {
                    外星人[I] .setX(外星人[I] .getX()+ 10);
                }
            }
            如果(下移)
            {
                对于(INT J = 0; J< aliens.length; J ++)
                {
                    外星人[J] .setY(外星人[J] .getY()+ 20);
                }
                下移= FALSE;
            }
        }
        其他
        {
            的for(int i = 0; I< aliens.length;我++)
            {
                如果(外星人[I] .getX()== 50)
                {
                    movestate = TRUE;
                    下移= TRUE;
                }
                其他
                {
                    外星人[I] .setX(外星人[I] .getX() -  10);
                }
            }
            如果(下移)
            {
                对于(INT J = 0; J< aliens.length; J ++)
                {
                    外星人[J] .setY(外星人[J] .getY()+ 20);
                }
                下移= FALSE;
            }
        }
    }
 

解决方案

假设外星人即将击中右边缘。第9(读左到右)没有X = 950,那么移动10个像素的权利。然后最右边确实有X = 950,这样的的向右移动;相反,它触发的方向变化和降档。

同样,在左边,第一个外星人不动,而是触发(后下)降档和方向逆转,那么其他九外星人举动,10个像素离开了。

您需要把所有的外国人,以同样的方式。无论是移动边缘外星人一样好,甚至当到达边缘不动任何外星人。

I've been working for a while on a clone of space invaders. The biggest problem I've faced by far is getting the aliens to move. After a lot of effort, I've developed n algorithm to move them. The algorithm behaves as expected, except that when the aliens have finished moving to the left/right and then move down, the left or rightmost aliens begin to converge on the two next to them, overlapping their sprites. Monitoring their X coordinates, I discovered that they move 10 pixels to the left/right every time they move down. I can't figure out why they do this, and am hoping you can help me. Here is my movement algorithm (the aliens are arranged in 2 rows of 10 and this method is called every ten frames. movestate dictates which direction the aliens are moving in.):

public void alienMove()
    {
        boolean movedown = false;
        if(movestate)
        {
            for(int i = 0;i<aliens.length;i++)
            {
                if(aliens[i].getX()==950)
                {
                    movestate = false;
                    movedown = true;
                }
                else
                {
                    aliens[i].setX(aliens[i].getX()+10);
                }
            }
            if(movedown)
            {
                for(int j = 0;j<aliens.length;j++)
                {
                    aliens[j].setY(aliens[j].getY()+20);
                }
                movedown = false;
            }
        }
        else
        {
            for(int i = 0;i<aliens.length;i++)
            {
                if(aliens[i].getX()==50)
                {
                    movestate = true;
                    movedown = true;
                }
                else
                {
                    aliens[i].setX(aliens[i].getX()-10);
                }
            }
            if(movedown)
            {
                for(int j = 0;j<aliens.length;j++)
                {
                    aliens[j].setY(aliens[j].getY()+20);
                }
                movedown = false;
            }
        }
    }

解决方案

Suppose the aliens are about to hit the right edge. The first 9 (reading left-to-right) don't have X = 950, so move 10 pixels right. Then the rightmost does have X=950, so doesn't move right; instead, it triggers the direction change and downshift.

Similarly, at the left edge, the first alien doesn't move, but triggers the (later) downshift and direction reversal, then the other nine aliens move left by 10 pixels.

You need to treat all the aliens the same way. Either move the edge alien as well, or don't move any aliens when the edge is reached.

这篇关于太空侵略者 - 奇怪的运动与外星人运动算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆