I am having trouble with loading my sprites in random places when the game loads, I have currently got them set in specific positions as shown below:
我在游戏加载时在随机位置加载我的精灵时遇到问题,我现在已将它们设置在特定位置,如下所示:
BlackBallRectangle = new Rectangle(150, 300, 25,25);
BlackBallRectangle2 = new Rectangle(500, 400, 25, 25);
BlueBallRectangle = new Rectangle(500, 150, 25, 25);
GreenBallRectangle = new Rectangle(100, 500, 25, 25);
OrangeBallRectangle = new Rectangle(180, 200, 25, 25);
PinkBallRectangle = new Rectangle(260, 260, 25, 25);
RedBallRectangle = new Rectangle(300, 450, 25, 25);
YellowBallRectangle = new Rectangle(550, 300, 25, 25);
I have created a Random Randome = new Random();
but I am unsure if this is needed. Any help would be great as I need them to be in random places for each level
我创建了一个Random Randome = new Random();但我不确定是否需要这样做。任何帮助都会很棒,因为我需要它们在每个级别的随机位置
1 个解决方案
#1
2
You have to make use of Random
.
你必须使用随机。
Random Randome = new Random();
BlackBallRectangle = new Rectangle(Randome.Next(0, 150), Randome.Next(0, 300), 25, 25);
BlackBallRectangle2 = new Rectangle(Randome.Next(0, 500), Randome.Next(0, 400), 25, 25);
// the same thing for others
This Randome.Next(0, 150)
generate a value between 0
and 150
. You can replace minValue
and maxValue
to match your needs.
此Randome.Next(0,150)生成0到150之间的值。您可以替换minValue和maxValue以满足您的需要。
Note: If you make same instance of the class with the Random
property, I recommend you to mark it as static
, otherwise there are chances to generate for same clases type same set of values.
注意:如果使用Random属性创建类的相同实例,我建议您将其标记为static,否则有可能生成相同的clases类型相同的值集。
#1
2
You have to make use of Random
.
你必须使用随机。
Random Randome = new Random();
BlackBallRectangle = new Rectangle(Randome.Next(0, 150), Randome.Next(0, 300), 25, 25);
BlackBallRectangle2 = new Rectangle(Randome.Next(0, 500), Randome.Next(0, 400), 25, 25);
// the same thing for others
This Randome.Next(0, 150)
generate a value between 0
and 150
. You can replace minValue
and maxValue
to match your needs.
此Randome.Next(0,150)生成0到150之间的值。您可以替换minValue和maxValue以满足您的需要。
Note: If you make same instance of the class with the Random
property, I recommend you to mark it as static
, otherwise there are chances to generate for same clases type same set of values.
注意:如果使用Random属性创建类的相同实例,我建议您将其标记为static,否则有可能生成相同的clases类型相同的值集。