Contemporary Games Can Take Up To Three Years To Develop

Game Developer on Ulitzer

Subscribe to Game Developer on Ulitzer: eMailAlertsEmail Alerts newslettersWeekly Newsletters
Get Game Developer on Ulitzer: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Game Developer Authors: Shelly Palmer, William Schmarzo, Steve Mordue, Qamar Qrsh, David Balaban

Related Topics: Artificial Intelligence Journal, Game Developer

Artificial Intelligence: Article

XNA, Game Development for Everyone

Writing games sure isn't what it used to be

Another class we need is the LevelHandler class (see Listing 2) that holds configuration options for different players (the default keyboard layout for each player) and for each of our levels (even if we implement only one track this time) the starting positions of the cars and their initial rotation - if you compare my car image and Figure 1, you'll see that the car is facing right, but we want to drive clockwise, so the car should face left - and we'll need different color for each car.

We have to add additional instance variables to our game class so we can bring the car up on the screen. The car has height and width like a real car, but in our world we can easily rescale our car - for example, if the image of your car is way too big. And last but not least we'd like to have four players (or cars) in the game who we hold in an array (we could also use a collection or list, but the garbage collector on the Xbox 360 works differently than Windows - it uses the compact framework here - and the foreach loop could create additional overhead that we don't want and don't need - on top of which we don't add or remove players dynamically so an array is perfect).

int carHeight;
int carWidth;
double carScale = 1;
Player[] players = new Player[4];

Using the LevelHandler class we initialize our players in the LoadContent function. Each player becomes an index needed to map the player to a joystick. We load the texture for the cars and define the initial starting position and rotation of the cars. Our cars should have different colors and maybe the keyboard should be used, so we need keys for accelerating, braking, and steering. See Listing 3.

Note the code in the comment. If you use it, you'd be able to control all the cars all at once with the cursor keys.

Now, when the players are initialized, the only piece missing is to actually draw the cars. And I bet you've figured it out already, the next code snippet goes into the draw method (just before the call to the spriteBatch.End() method):

foreach (Player player in players)
{
spriteBatch.Draw(player.car,
new Rectangle((int)player.carPosition.X,
(int)player.carPosition.Y, carWidth, carHeight),
new Rectangle(0, 0, player.car.Width, player.car.Height),
player.color, player.carRotation,
new Vector2(player.car.Width / 2, player.car.Height / 2),
SpriteEffects.None, 0);
}

If you run the game now you'll see the cars appear next to the start line. Perfect? Well, not really, the cars aren't moving and don't react to any keyboard or joystick input. This means we have to change the position of the cars depending on the elapsed time and rotate accordingly to the keyboard input from the player. To get our hands on the keyboard or game pad state is a single function call for each. But of course we have to do it for every player. For now the cars always move the same distance. There's no acceleration or braking yet. See Listing 4.

NOW it's really cool when we start up the application. The screen pops up, the track is there, four different cars appear and they start moving and moving and.....whoops! They're off the track and move off the screen (one good thing is that if you use the cursor keys (or joystick) the gold car reacts to your input). Damn, and we've done such a good job up to now, but here's the problem. We need to detect when the cars are leaving the track or when one car is crashing into another car - which means some kind of collision detection. This will be the facility we're going to add to our racing game next month, I hope you like what we did up to now. (See Figure) But wait, one thing we still want to do together is fill in the scoreboard with some useful information. We need to create a new spritefont by right-clicking on the Content folder and choosing New Item under the Add menu. We give it the name Courier New.spritefont. This adds an XML file to our project describing how to build a texture map for the new Courier font (it's a good idea to change the size to 10 point type or it won't fit into the scoreboard rectangle) and as with all content this file will also be compiled into a xnb file. To use the font we need another instance variable.

SpriteFont font;

And again, since it's content, the content pipeline should hold the information about the font, and we need to load it and fill our instance variable in the LoadContent function:

font = Content.Load<SpriteFont>("Courier New");

Last but not least we have to draw the text, in other words the player's name as well as his current and best lap time and the number of laps he's driven. First we need some coordinates for where the strings should be drawn, so we add either instance or local variables to the draw function:

int x1ScoreBoard = 55;
int y1ScoreBoard = 450;
int nextLineScoreBoard = 15;

Finally the actual information has to be printed out, so we add the code within the draw function within the foreach loop for the players as shown in Listing 5.

Now hit F5 and, voilà, we see the time running, but as with programming in general it's the same here. Finish one thing and another problem pops up or did we already implement how a car moves across the start or finish line? Again, this is something for next month.

Suggested Reading
Well, this really depends on whether you're completely new to C# and game development. If you're looking for more information on game development in general the book series Game Development Gems is great. If you want to dive deeper into the XNA Framework in general a very good place to start - besides the usual MSDN help content, which has very interesting tutorials - is the creators.xna.com web site where you'll find a lot of samples, starter kits, and, even better, a forum where the pros are watching and helping people out.

Conclusion
When I was watching the XNA Tour here in Europe (Vienna), one guy in the audience said to a friend, "Imagine what great games we would have built if we had these tools when we were young." Well, this guy was as old as me and he clearly grew up with a Commodore C64 and an Amiga like me (so the time he was talking about was close to the Stone Age - at least that's what my younger readers will say) and the great times he was talking about include titles like Summer/Winter Olympics, Ghost and Goblins (damn, it really was a cool time). But, back to XNA, I don't want to give you the wrong idea. I agree that game development is a lot easier today than it was 10 or 15 years ago, but one now has to be more of a graphics designer than a programmer. You see that in this article. It's easy to paint a track and cars on the screen (the content pipeline is great and the Draw method the place to code) and even making them move isn't a problem (the Update method is the place to code that), but think about it, how much time does it take to draw the different tracks? And this is the real challenge today. One needs a good idea and super-sexy graphics. If this isn't available you're sure to fail (I know that this isn't always true, but it is most of the time). Anyway, I hope you like what we built here and next month we'll continue with our racing game and you'll be able to have a real race with your friends. See you then.

More Stories By Berndt Hamboeck

Berndt Hamboeck is a senior consultant for BHITCON (www.bhitcon.net). He's a CSI, SCAPC8, EASAC, SCJP2, and started his Sybase development using PB5. You can reach him under [email protected]

Comments (4) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
DavidM 05/06/08 09:41:19 PM EDT

How can I contact the author?

What the heck

DavidM 05/06/08 09:40:33 PM EDT

When did .NETDJ go digital?

This site is so confusing.

Where the heck do I go to download the source for this article?

SYSCON needs to re-do this junk and provide the readers with an easy way to navigate the site, find the magazine in question, and find the code that goes with it!

Where is the second article to this? I see the next month appeared but no mention of XNA??

tori 04/03/08 10:31:31 PM EDT

This is very confusing to me, I just started using XNA and thought that this would be helpful and so far this article has only confused me. There is no downloadable source code and there is not enough description of the steps. This looked interesting, but in the end was disappointing.

Oscar Peli 03/28/08 12:26:15 PM EDT

Where can I download the source code for this article?
Thanks