当物体在彼此的顶部产生时重新定位

发布于 2025-02-09 22:32:56 字数 781 浏览 1 评论 0原文

如标题中所述,我正在编写一个代码,其中对象彼此产卵。有没有办法防止这种情况?这是我的代码:

// Check if the enemy UFO leaves the screen
if (enemyUfoPos[i].Y > screenHeight)
{
    // Update UFOs position
    enemyUfoPos[i].Y = -200;
    enemyUfoRecs[i].X = rng.Next(1, 1720);

    // Decrease the players score
    playerScores[SHOOTER] += ufoPass;
}

// Assume UFOs collide with eachother
bool isColliding = true;

while (isColliding == true)
{
    enemyUfoRecs[i].X = rng.Next(1, 1720);
    isColliding = false;

    for (int j = 0; j <= enemyUfoRecs.Length - 1; j++)
    {
        if (i != j && enemyUfoRecs[j].Intersects(enemyUfoRecs[i]))
        {
            isColliding = true;
        }
        break;
    }
}

如果有人可以给我一些建议,这将不胜感激。

As stated in the title, I am writing a code where objects are spawning on top of each other. Is there a way to prevent this? Here is my code:

// Check if the enemy UFO leaves the screen
if (enemyUfoPos[i].Y > screenHeight)
{
    // Update UFOs position
    enemyUfoPos[i].Y = -200;
    enemyUfoRecs[i].X = rng.Next(1, 1720);

    // Decrease the players score
    playerScores[SHOOTER] += ufoPass;
}

// Assume UFOs collide with eachother
bool isColliding = true;

while (isColliding == true)
{
    enemyUfoRecs[i].X = rng.Next(1, 1720);
    isColliding = false;

    for (int j = 0; j <= enemyUfoRecs.Length - 1; j++)
    {
        if (i != j && enemyUfoRecs[j].Intersects(enemyUfoRecs[i]))
        {
            isColliding = true;
        }
        break;
    }
}

If anyone could give me some advice that would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骄傲 2025-02-16 22:32:56

我认为这是您要归类的逻辑:

bool isColliding = false;

do
{
    enemyUfoRecs[i].X = rng.Next(1, 1720);
    isColliding = false;

    for (int j = 0; j <= enemyUfoRecs.Length - 1; j++)
    {
        if (i != j && enemyUfoRecs[j].Intersects(enemyUfoRecs[i]))
        {
            isColliding = true;
        }
        break;
    }
}
while (isColliding == true)

区别在do-while循环中,并在do-nile循环时至少遍历代码,然后查看wher> 之后检查是否需要再次重复。

I think this is the logic you want to archieve:

bool isColliding = false;

do
{
    enemyUfoRecs[i].X = rng.Next(1, 1720);
    isColliding = false;

    for (int j = 0; j <= enemyUfoRecs.Length - 1; j++)
    {
        if (i != j && enemyUfoRecs[j].Intersects(enemyUfoRecs[i]))
        {
            isColliding = true;
        }
        break;
    }
}
while (isColliding == true)

The difference is in the do-while loop, with a do-while loop, it goes through the code at least once, and then look at the while check afterwards if it needs to be repeated again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文