XNa advanced programming: Xbox 360 and Windows 3-5

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.