Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Introducing XNA Game Studio Express : Page 4

Microsoft's XNA Game Studio Express gives you powerful game tools, and the ability to create games for both Windows and Xbox 360.


advertisement
Add the Hazard Image
With the road scrolling and the car successfully changing lanes, you can add an obstacle that the car must avoid. Start this next stage in the development of your game by adding the hazard image (see Figure 6) to the project. Follow the same technique as before.

 
Figure 6: Adding the hazard image to the game project.

After you've added the image, add a Texture2D object to store it in. Write the following code in the beginning of the Game1.cs class:

private Texture2D mHazard; private Rectangle mHazardPosition;

The Rectangle object will hold the size and current position of the hazard on the screen.

You need to initialize the hazard position, so add the following code to the StartGame method to set the initial size and starting position for the hazard:

mHazardPosition = new Rectangle(275, 0, (int)(mHazard.Width * 0.4f), (int)(mHazard.Height * 0.4f));

Load the image from disk and into the Texture2D object in the the LoadGraphicsContent method:



mHazard = content.Load<Texture2D>("Hazard");

Finally, you need to draw the hazard image to the screen. Add the following code to the Draw method.

mSpriteBatch.Draw(mHazard, mHazardPosition, Color.White);

 
Figure 7: Drawing the hazard image to the screen.
Place this code before the SpriteBatch.End() line, but after the road and car have been drawn. Remember that the order in which you draw objects is important.

Now build the project and verify that your code draws the hazard image to the screen properly (see Figure 7).

The image should appear in the upper left lane. It probably looks a little funny because it's not moving as the road scrolls. You'll fix that next.

Move the Hazard
Hazards should scroll down through the screen on the road. As they exit the bottom of the screen they should reappear randomly in the right or left lane, repeating the process. To introduce this randomness, you first need to define a Random object at the top of the Game1.cs class:

private Random mRandom = new Random();

It's generally best to make only one Random object, and then use it throughout the entire game. This helps ensure that random number generation is as random as possible. Initializing multiple random objects often leads to the same numbers being generated, especially if the random objects were created on similar ticks.

Seeing hazards jump into view is disorienting; it would be better if they glide into the screen more smoothly. To do that, you need to change the initial starting position of the hazard so it starts just off-screen, and then scrolls down into the screen.

Change the following line of code in the StartGame method to look like this.

mHazardPosition = new Rectangle(275, -mHazard.Height, (int)(mHazard.Width * 0.4f), (int)(mHazard.Height * 0.4f));

Now, create a method to move the hazard down through the screen. The technique is similar to scrolling the road.

Add the following UpdateHazard method to your Game1.cs class.

private void UpdateHazard(GameTime theTime) { mHazardPosition.Y += (int)(mVelocityY * theTime.ElapsedGameTime.TotalMilliseconds); if (mHazardPosition.Y > this.Window.ClientBounds.Height) { mHazardPosition.X = 275; if (mRandom.Next(1, 3) == 2) { mHazardPosition.X = 440; } mHazardPosition.Y = -mHazard.Height; } }

This code moves the hazard down the screen. It then checks to make sure the hazard has not scrolled off the screen entirely. If the hazard has moved past the screen boundary, then players can no longer see the hazard. When that occurs, the code uses the Random object to randomly pick a lane for the hazard to appear in and then changes the hazard's position to be just off the top of the screen in that lane.

You also need to add a call to UpdateHazard in the Update method. Add the following line of code to Update just after the ScrollRoad method call.

UpdateHazard(gameTime);

Now build the project again to make sure you've done everything correctly. When you run the game, you should see the hazards scrolling down the screen one after the other, appearing randomly in one lane or another.

You might think you're done at this point. Unfortunately, your car can currently drive right through the hazard. You'll correct that in just a bit. The game also needs to keep score. This game simply tracks the number of hazards that the car has passed successfully.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date