Drawing the background
Depending on the type of game you are building, you may or may not want to use a scrolling background. Typical platform and adventure games make use of a tiled background, where the game world extends beyond the screen edges. In arcade games or simple platform games, you may instead choose to use a static background that changes when the level is competed. A third approach is to use a background that is larger than the screen, but is still limited. In this scenario you would move the background as the character approached the edge of the screen and stop scrolling it when the edge of the world was reached. In this example, we will use a simple fixed background as found in classic games like Pac Man or Frogger.
Firstly, we need to create another surface for our background image. We also need another rectangle object to store the position of our viewport into it (defined as the current window). In the class definition, add the following lines:
//background
public Rectangle rectBg;
public Surface ddSurfaceBg = null;
public int intBgX;
public int intBgY;
Then, in the initialise method, add the following:
//add background
ddSDesc.Clear();
ddSDesc.SurfaceCaps.OffScreenPlain = true;
ddSurfaceBg = new Surface(“bg.bmp”, ddSDesc, ddDevice);
rectBg.Size = new Size(800, 600);
intBgX = 0;
intBgY = 0;
To draw the background, the RenderSurface method needs to be updated thus:
rectBg.X = intBgX;
rectBg.Y = intBgY;
ddSurface2.ColorFill(Color.Black);
ddSurface2.DrawFast(0, 0, ddSurfaceBg, rectBg, DrawFastFlags.DoNotWait);
ddSurface2.DrawFast(intXloc, intYloc, ddSprite1, rectFrames[intFrame], DrawFastFlags.SourceColorKey);
ddSurface1.Flip(ddSurface2, FlipFlags.Wait);
Here we are creating an 800x600 backdrop positioned in the top left corner of the screen (0,0). It is drawn to the back buffer before the sprite to ensure that it is behind the sprite. The screenshot demonstrates what the game looks like at this point in time. Now the transparent sprite has a world to explore!
Adding sound effects
As our sprite moves around the game world, it may complete required tasks or fail them. At these points in time it may be desirable to play sound effects. To play a sound through DirectX, we first need to add a reference to Microsoft.DirectX.AudioVideoPlayback in our project and then add the following line to the source code:
using Microsoft.DirectX.AudioVideoPlayback;
As an example, we can use a simple sound effect whenever the sprite crashes into the bounding walls of the 2D world. We create an Audio object in the class declaration and initialise it in the Initialise() method as shown here.
//sound and video
public Audio soundFx;
...
soundFx = new Audio(“crash.wav”,false);
The second parameter of the Audio constructor is a Boolean indicator as to whether the file should automatically play when loaded. We have set this to false, adding the following lines to the key press handler whenever a collision is detected.
if (soundFx.Playing)
{
soundFx.Stop();
}
soundFx.Play();
Just in case the sound effect is already playing when a collision is detected, we have added a call to the soundFx.Stop() method before attempting to play it again. Without this method call, the sound would not play again. ::

Make sure your project references the DirectX and DirectDraw libraries.

Define the Sprites, Background and Sounds

Our animated sprite bitmap consists of four frames stored in the one image file.

Now the transparent sprite has a world to explore!