如何制作一个随机敌人生成器?

huangapple go评论112阅读模式
英文:

How do I make a random enemy spawner?

问题

这可能有点愚蠢,但我正在制作一个无尽的奔跑游戏,但我无法正确生成我的障碍物...

更具体地说,我有3种不同的障碍物,我无法使它们以随机方式选择,作为下一个要出现的 - 如果这有意义的话!我也无法让每个障碍物之间的距离变得随机。

为了让你了解我正在制作什么样的游戏,我试图使它的布局与 Chrome Dino 相似(https://chromedino.com)

注意:我本可以提供一些代码,但对于这个问题我没有任何东西 - 我只是有点需要一些能够工作的源代码 ;p 我已经花了好几天的时间试图找到一些有效的代码,但到目前为止还没有找到。
哦,我有两个类,一个是我的障碍物生成器,另一个是跳跃功能。
如果需要代码,我很乐意提供!

我研究过的一切通常都是针对 Unity 的,对它我曾经感到非常沮丧,所以我不使用它。

我尝试创建一个可以容纳我的3个障碍物的数组,想法是随机选择一个索引,但是那引发了错误 CS0021 - “无法将[]应用于类型为'ObstacleGenerator'的表达式”。

我还尝试过:

int obstacles = random.Next(0, 3);

然后说:

if (obstacles == 0)
{
    obstacle1.Update(gameTime);
}
else if (obstacles == 1)
{
    obstacle2.Update(gameTime);
}
else if (obstacles == 2)
{
    obstacle3.Update(gameTime);
}

理论上,这应该随机选择一个数字(在0和3之间),无论选择了哪一个,都应该开始移动。问题是,random.Next() 不仅会选择一个随机数字 - 它会不断选择,因此障碍物会移动,但是会出现抽搐。这是random.Next应该做的吗?如果是这样,有没有更好的方法来随机选择敌人?

我正在使用 C#、Visual Studio 以及 MonoGame 模板。任何帮助都将不胜感激! 如何制作一个随机敌人生成器?

英文:

This may be a lil stupid, but I'm making an endless runnner and I can't make my obstacles spawn properly...

To elaborate, I have 3 different obstacles, and I can't get them to be picked at random, as the next to appear - if that makes sense!
I also can't get the distance between each obstacle to be random, either.

For you to get an idea of what kind of game I'm making, I'm trying to make it somewhat of the same layout as Chome Dino (https://chromedino.com)

Note: I would provide some code, but I have nothing for this issue - I just kinda need some source code that will work ;p I've been spending several days trying to find some code that works, but nothing so far.
Oh, I do have 2 classes that are my Obstacle generator and the jumping function.
If code is needed, I'm happy to provide it!

Everything I've researched usually were for Unity, which I've had some insane frustrations with, hence why I'm not using it.

I tried making an array that would hold my 3 obstacles, with the idea of picking an index at random, but that threw the error CS0021 - "Cannot apply indexing with [] to an expression of type 'ObstacleGenerator'.

I also tried having:

int obstacles = random.Next(0, 3);

Then saying:

if (obstacles == 0)
{
    obstacle1.Update(gameTime);
}
else if (obstacles == 1)
{
    obstacle2.Update(gameTime);
}
else if (obstacles == 2)
{
    obstacle3.Update(gameTime);
}

In theory, that should pick a number at random (between 0 and 3), and whichever is picked, that's the obstacle that should start moving. The issue is that the random.Next() doesn't just pick one random number - it's constantly picking, so the obstacles move, but jerkily. Is that what's supposed to happen with random.Next ???
If so, is there a better way to get the enemies to get picked at random?

I'm using C#, Visual Studio with a MonoGame template. Any help is appreciated! 如何制作一个随机敌人生成器?

答案1

得分: 0

以下是要翻译的内容:

障碍物生成的代码从第148行开始:
https://github.com/Justin-Naicker/Unity_EndlessRunner_2D/blob/main/Ground.cs

在这个无尽奔跑游戏中,我有一些掉落的地面。这段代码根据我的地面高度(y轴)移动我的障碍物:
https://github.com/Justin-Naicker/Unity_EndlessRunner_2D/blob/main/GroundFall.cs

这段代码追踪我的障碍物位置,并在其离开屏幕时将其销毁:
https://github.com/Justin-Naicker/Unity_EndlessRunner_2D/blob/main/Obstacle.cs

你可以使用 random.Range() 函数来生成随机数。

英文:

I have some attached code here, hopefully, this can be of help:

Code for obstacle generation starting on Line 148:
https://github.com/Justin-Naicker/Unity_EndlessRunner_2D/blob/main/Ground.cs

I have falling grounds in this endless runner. This code moves my obstacles based on my ground height (y-axis):
https://github.com/Justin-Naicker/Unity_EndlessRunner_2D/blob/main/GroundFall.cs

This code tracks my obstacles position and destroys it when it goes off-screen:
https://github.com/Justin-Naicker/Unity_EndlessRunner_2D/blob/main/Obstacle.cs

You can use the random.Range() function to generate numbers.

答案2

得分: 0

只是一个建议,为什么不在你的障碍物上使用一个 Queue 结构呢?当障碍物在屏幕上生成时,使用 Enqueue 将其添加到队列中,然后在主要的更新循环中,使用 foreach 循环迭代队列,以更新每个障碍物。当障碍物离开屏幕时,使用 Dequeue 将其出队。

队列似乎非常适合,因为它遵循FIFO规则(先进先出),这与障碍物进入和离开游戏场地相对应。

至于何时生成它们,你可以简化一下,暂时在主要的更新循环中使用递减计数器,当它达到零时,生成另一个随机障碍物并将其添加到队列中;同时将计数器重置为一个介于200到400之间的随机数,这样你就可以在未来的随机时间生成另一个障碍物。

英文:

Just a suggestion, why not use a Queue structure for your obstacles. Enqueue an obstacle when it spawns on screen and then in your main Update loop, iterate through the Queue with a foreach loop to update each obstacle. When an obstacle leaves the screen Dequeue it.

A Queue seems perfect because it obeys FIFO rules (first in first out) which corresponds to your obstacles entering and leaving the playing field.

As for when to spawn them you could keep it simple for now, have a decrementing counter in your main Update loop, when it reaches zero, spawn another random obstacle and add it to the Queue; at the same time reset the counter to a random number between say 200 and 400 so you get another obstacle at a random time in the future.

huangapple
  • 本文由 发表于 2023年6月19日 21:41:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定