Programming - Games - 2D - using DX9 and C#

This toturial was written for me by Imran Khan (imranahmedkhan82@hotmail.com, iak1982@yahoo.com).
Copyright (c) 2004 is owned by Martin Baker

Background:

In this section we will learn how to show Background. You may think that the mechanism to show background must be same to show a bitmap. Yes it is, but with some changes, because the background scroll with the movement of the character.

First we declare some instances

//for background

public Rectangle backrect;
public Surface backsurface = null;
public int xaxis;

As we define rectangle for a character , here we also define a instance of Rectangle class for background to make frames. So that background will move with the movement of the character.

Next we define a surface for background.

Next we define a integer , we will show the use later.

First we create a method name Background( ).

private void Background()
{
SurfaceDescription sdesc =new SurfaceDescription();
sdesc.SurfaceCaps.OffScreenPlain = true;
backsurface = new Surface("background.bmp", sdesc, dddevice);
backrect.Size = new Size(800, 600);
}

Call the Background method from constructor of form1( "public Form1(): base()" )

, by writing " Background( )" at the last of the method.

First line create a new surfacedescription instance for background surface. Next we declare that the surface is simple not a backbuffer. 3rd line we create new surface as we done before. The only new line is last one.4th line set the size of the rectangle, this assumes the part of bitmap you want to display is 800x600 ( size of the screen ). Means it will show that size of frame every time when character moves.

Now we will see how to show the picture.

 

public void ShowSurface()
{

try
{
ssurface.ColorFill(Color.Red);

backrect.X = xaxis;
backrect.Y = 0;
ssurface.DrawFast(0, 0, backsurface, backrect, DrawFastFlags.DoNotWait);

ssurface.DrawFast(x4act, 300, charsurface, frames[frm4act], DrawFastFlags.SourceColorKey);
psurface.Flip(ssurface, FlipFlags.Wait);
}
catch
{
psurface.Dispose();
Init();
}

}

backrect.X = xaxis , it tells to start drawin from the beginning of the bitmap.We take a variable so that every time the character moves the xaxis value will be changed and the backrect.X value will be change so that we will see the background scrolling.
backrect.Y = 0
, tell to start drawing from the top of the bitmap,This will be constant. If your game contains upward motion then you should make it dynamic as we do the backrect.X, because there is only forward and backward motion in our game.

Now When the user hit the right arrow the character move(we have done this ) and the xaxis value will also change.


private void f_keydown(Object sender,System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode==Keys.Escape)
{
ending();
Application.Exit();
}

if(e.KeyCode==Keys.Right)
{
frm4act = frm4act + 1;
x4act +=5;
xaxis += 10; // we add it so the background will move
if (frm4act > 3)
frm4act = 0;
if (x4act > 500)
x4act -=5;
if (xaxis > 1600)
xaxis = 1600;


ShowSurface();
}

}


we will explain here the newly added lines.

xaxis += 10,whenever user press right key , 10 will be added to this variable so when we call ' ShowSurface( ) ' this value will be copied to

backrect.X = xaxis.

and the image will be shown from that postion.

the next thing we added is :

if (xaxis > 1600)
xaxis = 1600;

it is the limit of the background here, because I have made only 2 scenes so the total lenght of the image will be '1600'.

Now if you execute your program you will see a complete game features ( except sound ... ).

 

next: sound


metadata block
see also:
Correspondence about this page

Book Shop - Further reading.

Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.

cover Game Design for Teens.

Commercial Software Shop

Where I can, I have put links to Amazon for commercial software, not directly related to this site, but related to the subject being discussed, click on the appropriate country flag to get more details of the software or to buy it from them.

cover Visual C# .NET for Microsoft Platforms only.

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2017 Martin John Baker - All rights reserved - privacy policy.