TheProgrammingZone's front page. All the latest programming and game development news, as well as this week's feature game project!
Visual Basic
Assembly aritcles
Source Code
Visual Basic
Game Projects
 Game projects under development by members of the TheProgrammingZone Community. Show them your support, and check out their games!
Message Board
 A forum to interact with your fellow programmers.
Visual Basic
Assembly Links
TheProgrammingZone's products. Check out some of our in house products that we made.
You want to contact the guy behind TheProgrammingZone? Here's where to do it!


Game Programming in Visual Basic - page 1

pages: prev | 1 | next
Welcome to the second of a series of tutorials about “Game Programming in Visual Basic”. This lesson will get you down into the nitty gritty of the Win32 API. So go ahead and read on and get coding J.

Getting Started
In this lesson, you will learn the techniques of the Win32 API to make a catchy little game for you and your friends to play. All game programming is are techniques that you learn and put them together to make the next Quake 3 Engine! We will start off with good old bitblt. The lesson itself won’t be big, but you can reference my Sample Project of Asteroids in which I made in 3 hours J.

What is BitBlt?

BitBlt is the main graphics drawing function for the Win32 GDI, there are others like StretchBlt, but they aren’t really needed here. So lets take a look at the function

Public Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long

hDestDC – The destination DC(Device Context) example: frmMain.hdc/picGame.hdc

X,Y – The coordinates of where you want the Top/Left part of the graphics being drawn

nWidth, nHeight – The dimensions of the graphic to be drawn.

hSrcDC – The source DC from which the graphic comes from.

xSrc, ySrc – The source coordinates from the hSrcDC you get the image from(nWidth and nHeight determine xSrc2 and ySrc2)

dwRop – The rasterization option. Example: SRCCOPY = Copy as is, SRCINVERT = Inverts the colors, SRCAND = Copies all but the white, SRCPAINT = Copies all but the black.

Tip For Debugging BitBlt
If you haven’t noticed, BitBlt is a function, so it will return a value. If the value returned is less than or equal to zero, then the execution of BitBlt has failed. Below is sample code for debugging BitBlt

[Code Start]

Dim RetVal as long

RetVal = BitBlt(frmMain.hdc,0,0,640,480,picLogo.hdc,0,0,SRCCOPY)

If RetVal = 0 Then

MsgBox “BitBlt has failed!”

Exit Sub/Function

End If

[Code Stop]

Extra BitBlt Stuff

Getting Transparent Blts
Sometimes you will need to get an image by itself(say a character sprite with a green background, you would need a Mask for the graphic. A Mask is just a Black and White picture of the graphic.

You would draw the Mask first using SRCAND, then draw the real graphic EXACTLY AFTER IT, using SRCINVERT. You can get mask creators off PSC, because I don’t have the time to make one.

[Code Start]

BitBlt frmMain.hdc,0,0,640,480,picLogo.hdc,0,0,SRCAND

BitBlt frmMain.hdc,0,0,640,480,picLogoMask.hdc,0,0,SRCINVERT

[Code Stop]

What is GetAsyncKeyState?
This function allows the programmer to access character input throughout the program without the use of the default Form_KeyPress/KeyDown/KeyUp events allowing more versatility I would say.Lets take a look at the function, its VERY VERY VERY simple.

Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

vKey – You insert the key constant here to check its current state, you can use the basic vbKey constants with this.

This API is very simple to use.

[Code Start]

Dim btnDown as Boolean

btnDown = GetAsyncKeyState(vbKeyDown)

If btnDown = True Then ‘//the key is being pressed

‘//code here


‘//code here

End If

[Code Stop]

A cool way to use this API can be looked at modEngine.bas in the Asteroids directory.

What is SndPlaySound?
This function is pretty easy to use as well, but at the same time, it can cause some big problems if the flags given are kinda awkward. So let’s take a look at this function.

Public Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA"
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

LpszSoundName = The filename for the WAVE sound(must be .WAV sound file)

UFlags - Flags for the sound when it’s played.

SND_ASYNC - &H1 lets you play a new wav sound, interrupting another

SND_LOOP - &H8 loops the wav sound

SND_NODEFAULT - &H2 if wav file not there, then make sure NOTHING plays

SND_SYNC - &H0 no control to program til wav is done playing

SND_NOSTOP - &H10 if a wav file is already playing then it wont interrupt

[Code Start]

sndPlaySound App.Path & “\Audio\Sound.wav”, SND_ASYNC or SND_NODEFAULT

[Code Stop]

For some basic subs and functions on using sndPlaySound, refer to the Asteroids example.

What is IntersectRect?
This function takes to RECT types and determines whether they overlap each other. Let’s take a look at this function.

Public Declare Function IntersectRect Lib "user32" (lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT) As Long

lpDestRect – This RECT will receive the area that the 2 RECTs crossed over. You would be able to use this RECT for pixel perfect detection. More on that in a later lesson (maybe…)

lpSrc1Rect – The first source RECT

lpSrc2Rect – The second source RECT

[Code Start]

Dim tmpRECT as RECT
Dim PlayerX as Integer, PlayerY As Integer

Dim CompX As Integer, CompY as Integer

Dim PlayerRect as RECT, CompRect As RECT

‘//We are assuming the dimenions of the player are 50x50 and the comp 50x50

‘//createrect is a helper function I wrote for creating rects.

PlayerRect = CreateRect(PlayerX, PlayerY, PlayerX +50, PlayerY + 50)

CompRect = CreateRect(CompX,CompY,CompX + 50, CompY + 50)

If IntersectRect(tmpRECT,PlayerRect,CompRect) = True Then ‘//there was an overlap between the 2 rects

‘//code here

End If

[Code Stop]

Using IntersectRect can provide a mere decent collision detection like I used in the Asteroids game. Refer to modEngine.bas for my short Collide function for collision detection.

Other APIs Used
I’m well aware of the other 6 or 7 APIs I used in the lesson, but if you go to, there are some good tutorials on all the DC stuff, they are very good and that’s where I learned from. Or you can check out a kick-azz VB community at, with lots of other cool tutorials on such stuff, including some APIs I used.


With these simple techniques, you can effectively create a nice 2d game, better than my Asteroids game I made because I made it in 1 – 2 Hours. You must remember, these are just the techniques NEEDED to create the game, you gotta learn to put them together by yourself, and when you can program a cool game(even a simple one), you can probably say to yourself, you can program anything because games require all the basics of the language like strings(chars in C++ unless in an array), simple math operations, arrays etc… Until next time, see ya later J

If you have any questions, comments, or ideas about this lesson please email me at

pages: prev | 1 | next
TheProgrammingZone 2019
Site Map