[英]SFML how to make various shapes move in random directions?
I am having troubles in making shapes moving in random directions, with the code that i currently have they all move in the same direction and then change to another direction randomly.我在使形状沿随机方向移动时遇到了麻烦,我目前使用的代码使它们都沿同一方向移动,然后随机更改为另一个方向。
Here is the code这是代码
for(int i = 0; i < 50; i++)
{
int randomMoveX = rand() % 2 + (-1);
int randomMoveY = rand() % 2 + (-1);
circleObjectArray[i].move(randomMoveX,randomMoveY);
while(randomMoveX == 0)
{
randomMoveX = rand() % 2 + (-1);
}
while(randomMoveY == 0)
{
randomMoveY = rand() % 2 + (-1);
}
cout << "randomMoveX: " << randomMoveX << endl;
cout << "randomMoveY: " << randomMoveY << endl;
}
How can i change my code to be able to do move them in random directions individually?如何更改我的代码以便能够单独在随机方向上移动它们?
Thank you for your time :D谢谢你的时间:D
When you run your app, a seed is set for random as srand(1)
.当你运行你的应用程序时,一个随机种子被设置为srand(1)
。
To use different value in rand you need to change the seed of random.要在 rand 中使用不同的值,您需要更改 random 的种子。 Then use the function srand and save it in a unsigned int
to use different rand for different shape.然后使用函数srand并将其保存在unsigned int
以便为不同的形状使用不同的 rand。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.