Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Ads by Lake Quincy Media
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » XNA » Introduction to XNA: The Microsoft 2D and 3D Game Development Platform

Introduction to XNA: The Microsoft 2D and 3D Game Development Platform

This article introduces you to the XNA Framework that makes game development easier than it ever was before. This sample features a star trek ship which you can move around the screen and shoot at the enemy Klingon Ship. The demo comes complete with authentic sound.

Author Rank:
Total page views :  44843
Total downloads :  824
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
StarTrek.ZIP
 
Become a Sponsor

 

Figure 1 - The Star Trek Enterprise on a Mission

Introduction

Although I couldn't name all the episodes, I think I watched every original Star Trek episode that was produced.  My fascination with Star Trek didn't end with the original, but continued into the Next Generation.  After that, I kinda lost track...Anyway, this month, Microsoft came out with an article in MSDN on the XNA framework, written by Charles Cox  and Michael Klucher.   I became intrigued and decided to pursue exploring the XNA Framework.  In doing so, I created this simple example.  Most of the inspiration for this article didn't come from the MSDN article, but from a game called elves revenge on a website called Errolian.  By examining this code, I was able to piece together this demonstration of XNA.

Getting Started with XNA

The XNA Framework was created so that any developer could easily put together a game that runs on either Windows or on XBox.  The XNA Framework only works on Visual Studio Express, so even if you have Visual Studio Enterprise Team Suite, you'll still need to download Visual Studio Express. The good news is Visual Studio Express  is FREE!! XNA is FREE!!  XBox is...well...at least you can develop your app for the pc as well.

What Does XNA Stand For?  As far as I know XNA is not acronymic, but if I ventured a guess, it stands for  (DirectX  Next Generation Architecture)

When you install XNA and Visual Studio Express, I think you'll find that if you are already a VS.NET developer, your development experience is almost exactly the same, minus a few cool features.  You can begin developing your game right away by choosing  File-->New Project and selecting  Windows Game.  If you want to start with some functionality in your project, you can choose SpaceWar Windows Starter Kit.

Figure 2 - Picking an XNA Template Project 

This Template generates the most important class for your XNA Game development, the Game1 class.  This class inherits from Microsoft.Xna.Framework.Game, which is essentially the Game Engine that hides some of the messy stuff involved in game development under the hood.  In fact, for most of the game development, the only two methods you need to override in this class are the Update method and the Draw Method.  The Update method is called every time the game needs to update its game data.  And the Draw method...yeah, you guessed it...is called every time the game needs to paint to the screen.

So by now I bet a few dozen questions may have popped into your head.

How do I load my sprite from a bitmap?

How do I move my sprite? Rotate my sprite?

How do I create sound in XNA?

How do I accept input from the keyboard?

How do I detect a collision between sprites?

All these questions, and more will be answered in the paragraphs to follow:

Design

The UML Design of my Star Trek XNA game uses some of the pieces of the framework generated by the XNA wizard, but adds a Sprite class and a SpriteAnimation class.  These two classes are the heart of the game design, and allow us to move shapes around the screen.  In essence all game components including the ship, the enemy, the weapon, and even the background are all sprites.  The EnemyExplosion class inherits from SpriteAnimation.  This class plays a frame of the animation after each new game screen update.

Figure 3 - UML Design of Star Trek Demo Reverse Engineered using WithClass 

Loading the Images

The images in this game are of type Texture2D.  If you want to make a 3D game, you would use Texture3D.  Images can be conveniently loaded in an override of the LoadGraphicsContent method of the Game class provided for you in the template.  Also provided for you is a ContentManager object in which you can use to load your content.  You may also want to create an object called a SpriteBatch, that allows you to later draw your sprites as a batch.  The following call allows us to load our graphic file into our sprite:

_photon = new Sprite(_spriteBatch, content.Load<Texture2D>(@"Content\Textures\photon"));

If we had a 3D Mesh we wanted to load, then we just change the template to Texture3D:

_photon = new Sprite(_spriteBatch, content.Load<Texture3D>(@"Content\Textures\photon"));

Note, that in order to load your Content, the bitmaps in the content must be part of the project.  I found that even if I had the image in the Content\Textures folder, it wouldn't load the image unless I actually had it as part of the project.  This is because, XNA converts all images (and sounds) to its own format.  Textures are made into .xnb files and sounds are stuck in a wavebank.

Keyboard Input

Keyboard Input is handled inside the Update method.  If we want to respond to the space bar being pressed, we simply get all the keys pressed since the last Update and see if one of them is a space bar.  Listing 1 shows the code inside update that checks for a space bar press:

Listing 1 - Checking if the Space Bar is Pressed

protected override void Update(GameTime gameTime)
{

KeyboardState state = Keyboard.GetState();
Keys[] keys = state.GetPressedKeys();
if (keys.Length > 0)
{
 
foreach (Keys nextKey in keys)
  {
   
if (nextKey == Keys.Space)
     {
             if (_weaponState == WeaponState.IDLE)
               {
                  _photon.Position = new Vector2(_ship.Position.X, _ship.Position.Y);
                  _photon.Angle = _ship.Angle;
                   Sound
.Play("photon_torpedo");
                }
     }
  }
}

Moving a Sprite

In order to move a sprite on the screen, we need to update its position or angle.  Translation and rotation are handled inside the Update method.  The results of the transformation are later shown using the overriden Draw method.   Looking at listing 1, we see that we are moving a photon torpedo.  We set the initial photon torpedo to be at the coordinates of the Enterprise ship and angle  the torpedo in the direction the ship is moving.  Our homebrewed sprite class handles the translation of the missle through it's own Update method.  The Update method of the Sprite class takes the delta change of position coordinates and the change in the angle and adds it to the sprite's current coordinates:

Listing 2 - Updating your Sprites Position and Angle

public virtual void Update(int dx, int dy, float diff_angle)
{
  _position.X += ((
float)dx) * (float)Math.Cos(_angle);
   _position.Y += ((
float)dx) * (float)Math.Sin(_angle);
   _angle += diff_angle;
  
if (_position.X < 0) _position.X = 0;
  
if (_position.X > 1500) _position.X = 1500;
  
if (_position.Y < 0) _position.Y = 0;
  
if (_position.Y > 1500) _position.Y = 1500;
  
if (_angle > 6.28) _angle = 0;
}

Drawing a Sprite

The sprites are drawn inside the Draw method of the Game object.  Drawing is batched through the SpriteBatch object.  Listing 3 shows the code needed to draw each of our game objects.  Note that we draw different sprites depending upon the state of objects in the game.

Listing 3 - Drawing Our Game Objects

/// <summary>
///
This is called when the game should draw itself.
/// </summary>
///
<param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{

   // Make the background black

  graphics.GraphicsDevice.Clear(Color.Black);

//   Draw a starry space background
  DrawBackground();

// Draw all the sprites in the sprite collection

foreach
(Sprite sprite in _sprites)
{
   sprite.Draw();
}

// draw the photon torpedo if it's launched
if (_weaponState == WeaponState.PHOTON_MOVING || _weaponState == WeaponState.PHOTON_START)
 {
   _photon.Draw();
}

//  if the enemy is hit, draw one of the explosion animation frames
if
(_enemyState == EnemyState.HIT)
  {
     _explosion.Draw();
   }
else if (_enemyState == EnemyState.IDLE)
  {
    _klingon.Draw(); 
// otherwise draw the enemy in one piece
 }

// don't forget to call the base class draw routine

base.Draw(gameTime);

}

Sprites Draw themselves by batching through the SpriteBatch object.  The SpriteBatch class has a method called Draw that allows us to draw our sprite, translated, rotated, and scaled giving us great flexibility in transforming our sprite.  Listing 4 illustrates how the sprite is drawn through the SpriteBatch object.

Listing 4 - Drawing the Sprite with Transformations

public virtual void Draw()
{
  _batch.Begin();
   _batch.Draw(_texture,
new Vector2(_position.X, _position.Y), new Rectangle(0, 0, _texture.Width, _texture.Height), Color.White, _angle, new Vector2(0, 0), _scale, SpriteEffects.None, .5f);
   _batch.End();
}

Creating a Sound

Creating Sounds in XNA turns out to be a little bit more than loading a file.  In XNA, you need to use a tool called XACT.exe which is located in your \Program Files\Microsoft XNA\XNA Game Studio Express\v1.0\Tools folder.  Xact allows you to drag wave files from explorer into what is called a wave bank.  You then need to drag files from the wave bank into a sound bank.  Finally you need to drag files from the sound bank into the Cue.  It is inside the Cue that you can retrieve sounds and play them through your computer's sound card.  This whole process of dragging and dropping sounds three times strikes me as a bit silly, but currently that is the situation.  The sound classes in XNA are a bit finicky so make sure that eventually your sounds end up in the Cue or they won't play. 

Figure 4 - XACT Showing the Wave Bank, Sound Bank, and Cue Windows 

In this project, we have wrapped the Sound functionality in a Sound class to make things a bit easier.


Figure 5 - Sound Class Wrapping XNA Sound Functions

 However, you still need to make sure that you get all the names of the audio engine file, the sound bank file and wave bank file correct in the Initialize function of the Sound class shown in listing 5.  Also make sure you have included sound.xap file you are using inside your project.  This file will be automatically generated by XACT.exe.

Listing 5 - Initializing the Sound class

public static void Initialize()
{
  engine =
new AudioEngine(@"Content/SoundFX/sounds.xgs");
   wavebank =
new WaveBank(engine, @"Content/SoundFX/Wave Bank.xwb");
   soundbank =
new SoundBank(engine, @"Content/SoundFX/Sound Bank.xsb");
}

If you've done everything correctly,  then when you call Sound.Play with the name of the key in your Cue, then you will hear the specified sound.  Sounds play asynchronously.  If you want to play a sound synchronously, then you need to wait for it to finish and then stop it.  Listing 6 shows the code for playing two sound synchronously (one after another):

Listing 6 - Playing the Red Alert Twice, Synchronously

Cue alert = Sound.Play("redalert");
while (Sound.Playing(alert)) ;
Sound.Stop(alert);

Cue alert1 = Sound.Play("redalert");
while (Sound.Playing(alert1)) ;
Sound.Stop(alert1);

Detecting a Collision Between Sprites

The last XNA challenge we are going to talk about is how to detect a collision between sprites.  You would think that this aspect of game development would be more obvious, but it actually requires a few steps.  Collision detection is done using an object in XNA called a BoundingBox.   The BoundingBox has an Intersects method you can use to test for collision.  Because BoundingBox's require 3D Vectors to construct them, we need to create 3D vectors for each of the objects we are testing for collision.  The first set of 3D vectors describes the minimum and maximum extent of the first object and the second set of 3D vectors describes the minimum and maximum extent of the second object:  Each set of 3D vectors is used to create one BoundingBox.  As you can see, there are a lot of hoops you need to jump through to test a collision.  Since collision testing is a pretty common scenario in game development, it would be nice if there was an easier way to do this for 2D game development.

Listing 7 - Testing for Collision between Sprites

internal bool Intersects(Sprite sprite)
{
 
// create a set of 3D Vectors for the tested sprite object
 
Vector3 min1 = new Vector3( sprite.Position.X, sprite.Position.Y, 0 ); // Vector3 min1 = mimium co-ords of Sprite 1
 
Vector3 max1 = new Vector3( ( sprite.Position.X + sprite.Texture.Width ), ( sprite.Position.Y + sprite.Texture.Height), 0 ); // Vector3 max1 = maximum co-ords of Sprite 1

 // create a set of 3D Vectors for this sprite object
 
Vector3 min2 = new Vector3( Position.X, Position.Y, 0 ); // Vector3 min1 = mimium co-ords of Sprite 1
 
Vector3 max2 = new Vector3( ( Position.X + Texture.Width ), ( Position.Y + Texture.Height), 0 ); // Vector3 max1 = maximum co-ords of Sprite 1

// create a bounding box for the tested sprite
BoundingBox box1 = new BoundingBox(min1, max1 * new Vector3(0.99f, 0.99f, 0)); // Multiply BoundingBox of Sprite1 by 0.99 to provide 1% leniency

// create a bounding box for this sprite
BoundingBox box2 = new BoundingBox(min2, max2 * new Vector3(0.99f, 0.99f, 0)); // Multiply BoundingBox of Sprite2 by 0.99 to provide 1% leniency

// test to see if they have collided
return box1.Intersects(box2);
}

The Game

The sprites finally come to life once we have hammered out the details.  In the sample,  The left and right arrow moves  the enterprise ship and the R key rotates it.  Hitting the SpaceBar fires a photon torpedo at the Klingon.  If you hit the Klingon,  the Klingon Explodes using the ExplosionAnimation.  I used Paint.NET to draw most of the sprites you see here as well as the Explosion Sequence.  Paint.NET is a free painting utility that is actually quite advanced.  Once you get the hang of it, you can draw some pretty cool game objects.

Figure 6 - Star Trek Game in Action

Conclusion

Game programming has never been more accessible to the developer as it has now with XNA and Visual Studio Express.  With VS.NET Express, You can quickly throw together a 2D or 3D game with high quality sounds and graphics.  If you wish to develop games for the XBox using XNA, you need to join the XNA Creators Club, a club which also provides resources for game developers.  Anyway, if you are game for some fun development that can provide fun for others, I invite you to look into the XNA framework.  It will certainly make your life easier with the help of C# and .NET.

Note:  In order to lighten the download, I have cut out a lot of the files that get generated by the project.  You'll need to build the project to get the game to work.


Login to add your contents and source code to this article
 About the author
 
Mike Gold
Michael Gold is President of Microgold Software Inc., makers of the WithClass UML Tool. His company is a Microsoft VBA Partner and Borland Partner. Mike is a Microsoft MVP and founding member of C# Corner. He has a BSEE and MEng EE from Cornell University and has consulted for Chase Manhattan Bank, JP Morgan, Merrill Lynch, and Charles Schwab. Currently he is a senior developer at Finisar Corp. He has been involved in several .NET book projects, and is currently working on a book for using .NET with embedded systems. He can be reached at mike@c-sharpcorner.com
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010 Professional
Microsoft Visual Studio 2010 Professional will launch on April 12, but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of $549 (US). Pre-order now.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
StarTrek.ZIP
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Click Here for 6 Months Free! Powerful ASP.NET Hosting at your Fingertips!
Become a Sponsor
 Comments
Download Link ? by Ian On April 23, 2007
I'm either very tired and blind, or is the download link missing ?
Reply | Email | Delete | Modify | 
Re: Download Link ? by Mike On April 24, 2007
I don't see it either, so your neither tired or blind.  I'll contact the site admin.  It must have gotten dropped accidently
Reply | Email | Delete | Modify | 
Missing Content? by Jeff On May 23, 2007
Downloaded the zip and tried to build, got messages for missing .wav files.... warp.wav, accel.wav, autodestruct.wav, handphaser.wav, stanby.wav, tas_sound_spock_suluabout.wav, tas_sound_spock_that_would_beillogical.wav...
Reply | Email | Delete | Modify | 
Re: Missing Content? by Mike On May 24, 2007
you don't need these to build, so just remove them from the project for now.
Reply | Email | Delete | Modify | 
Re: Re: Missing Content? by J On September 23, 2009
It is a really interesting technology, I'm quite advance programmer and new in this of XNA, nor in .NET framework. I have downloaded 2008 Express edition and there are some errors trying to adapt the .solution... Any idea?

Thanks in advance ;)
Reply | Email | Delete | Modify | 
XBox 360 and PC by Chris On July 17, 2007
How would you create an xbox 360 and pc game in one solution?
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.