3.5 randomhelper Class
Let's take a look atRandomhelperClass, which is not used in a single project, but almost any game will need to generate random numbers so that the game content does not seem to have much periodicity, and make the game rich in changes.
In Breakout In the game, you need some random numbers. Here the bricks ( Block ) Is generated randomly. The first level of random probability is 10% , Level 2 is 20% And so on, as the game level continues to increase, the game will become more difficult. Of course, you can use Random Class, call Next Method to generate a new random number, but if you want to generate a random standardization vector, you must use the followingCode:
Random randomgenerator = New Random (( Int ) Datetime. Now. ticks );
Vector3 randomnormalvector = New Vector3 (
( Float ) Randomgenerator. nextdouble () * 2.0f - 1.0f ,
( Float ) Randomgenerator. nextdouble () * 2.0f - 1.0f ,
( Float ) Randomgenerator. nextdouble () * 2.0f - 1.0f );
Randomnormalvector. normalize ();
If you do not want to repeat the code again and againRandomhelperIs helpful. Figure3-8ShownRandomhelperClass internal structure.
Figure 3-8
We can see that the methods of this class are very simple. You can write this class in a few minutes. However, this class is very useful. Random Static instance of the class Globalrandomgenerator , Randomhelper Class creates a new Random The instance is much faster.
Generate random vectors
Below is Randomhelper Class to obtain two-dimensional random vectors: /// <Summary>
/// Get random vector2
/// </Summary>
/// <Param name = "min"> Minimum for each component </Param>
/// <Param name = "Max"> Maximum for each component </Param>
/// <Returns> Vector2 </Returns>
Public Static Vector2 getrandomvector2 ( Float Min, Float Max)
{
Return New Vector2 (
Getrandomfloat (Min, max ),
Getrandomfloat (Min, max ));
} // Getrandomvector2 (Min, max)
test randomhelper methods in the class do not make much sense, because the return values of all methods are random numbers, there is no need to check getrandomvector2 whether the method returns a two-dimensional vector is basically not an error.