Programming - Games - 2D - using DX9 and visual basic

This toturial was written for me by Imran Khan (,
Copyright (c) 2004 is owned by Martin Baker


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

First we declare some instances

Private backrect As Rectangle
Private backsurface As Surface = Nothing

Private xaxis as Integer

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 Sub Background()
Dim sdesc As New SurfaceDescription()
sdesc.SurfaceCaps.OffScreenPlain = True
backsurface = New Surface("background.bmp", sdesc, dddevice)
backrect.Size = New Size(800, 600)
End Sub

Call the Background method from " Init( ) ", by writing " Background( )" at the last of the Init( ) 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 Sub ShowSurface()

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

ssurface.DrawFast(x4act, 300, charsurface, frames(frm4act), DrawFastFlags.SourceColorKey)

Catch ex As SurfaceLostException
End Try

psurface.Flip(ssurface, FlipFlags.Wait)
End Sub

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, Coz 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 Sub form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

If e.KeyCode = Keys.Right Then

frm4act = frm4act + 1


x4act += 5 ' we add it so that the character will move forward.

xaxis += 10 ' we add it so the background will move

if frm4act > 3 then
frm4act = 0

End If

if x4act > 300 then ' Boudries for the character.

x4act -=5

End If

If xaxis > 1600 Then
xaxis = 1600
End If

ShowSurface( )

End if

If e.KeyCode = Keys.Escape Then



End If

End Sub

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 Then
xaxis = 1600
End If

it is the limit of the background here, coz 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 ( excepy sound ... ).


next: sound

metadata block
see also:
Correspondence about this page

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

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