|
YOUR FEEDBACK
|
TOP MICROSOFT .NET LINKS Feature XNA, Game Development for Everyone
Writing games sure isn't what it used to be
By: Berndt Hamboeck
Mar. 27, 2008 05:00 PM
If you try to run the current project you'll see a cornflower-blue window of 800x600 pixels pop up and that's all, but if you set breakpoints for all of the functions I mentioned you'll see that the XNA Framework's logic flow works something like this:
We can clearly see that the game loop exists and that the update and draw methods are the places for our game logic and graphics drawing. If you're not already excited about game programming maybe you will be as soon as you see more than a blue background screen. To get started we need to create or import a few images (see Figure 3):
If you don't want to create these on your own, feel free to download the source code for this article, which includes all of the images used. We have to import these images into the project's content subfolder (another new feature of the XNA Framework 2.0) where all the content files should be. Your Solution Explorer should look like the one in Figure 4. The next step is to show the background and a scoreboard to display the player's name and current and best lap time. To do this we start by defining some instance variables that will hold our textures of the track and scoreboard - where we also need two rectangles that define the size of the scoreboard and its border: Texture2D scoreBoard; The next step is to code the LoadContent function and load the images. We use an image with a black background for the scoreboard and surround it with a gray border to make it look nicer not forgetting our real background, the track itself. We do this simply by telling the 'Content Pipeline' to load our textures (from the Content subdirectory). The XNA content pipeline is used to load 3D objects (that you've exported from Blender or 3DStudio), textures (this is exactly what we want to do), effects (light, bump mapping), or Microsoft Cross-Platform Audio Creation Tool (XACT) projects. This kind of different content can be created with many different tools and saved in many different file formats. Using the content pipeline built to import most of the common file formats, this content is processed automatically into a managed code object that can be loaded and used by XNA Game Studio games on Windows and the Xbox 360 at runtime (all our content files are compiled into .xnb files and our audio files are compiled into .xgs, .xwb, and .xsb files for the actual sound project, wave bank, and sound bank content). // TODO: use this.Content to load your game content here Now before we test our cool game we code the draw function, where we simply draw the track, the border, and the actual scoreboard - in that order - so that everything appears on the screen. This is done in a sprite batch because by using it we send a single draw call to the graphics card. If we did it without using a sprite batch we'd put a load on the gaming machine that would slow things down and maybe result in an unplayable game since all the drawing commands are sent separately to the graphics card every time we draw something to the screen - and that's the last thing we want. // TODO: Add your drawing code here Note that we haven't coded anything in the update function so far because we don't have any game logic yet. If we run the application we see nearly the same thing as in Figure 1 - only the cars are missing and there's no text on our scoreboard. Fine, but what's a racing game without any players? OK, let's create the cars and put them on the starting line. The first object we should create now is a player class. It's always good to have a class with the player-specific values. You might think that's unnecessary, but it's not. The more you work on a game the more features you want to build into the application. For example, in the beginning you might only have a player name and the laps driven, as well as the current position (x, y coordinates and rotation) of the car. Later on you might want to keep the track record and current points if you stage some kind of championship season, or - even simpler - the keyboard should be configurable, and so on. You can see my current player class in Listing 1 (I'm quite sure that this will be far bigger by the time you read this article). YOUR FEEDBACK
MICROSOFT .NET LATEST STORIES
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING NEWS FROM THE WIRES
|
|||||||||||||||||||||||||||||||||||||||