Game Programming in Visual Basic - Lesson Two

Submitted on: 1/24/2015 7:03:00 AM
By: Gregory English (from psc cd)  
Level: Beginner
User Rating: By 52 Users
Compatibility: VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 978
     This article is lesson two in my mini-series of "Game Programming in Visual Basic". If you like it or even dislike please tell me what was wrong and what was good. PLEASE EVERYONE VOTE, IT ONLY TAKES A FEW SECONDS!!!!

This article has accompanying files

Game Programming in Visual Basic
<--[if gte mso 9]>
 MR Ronald W. English
 MR Ronald W. English
 English Enterprises Inc,

<--[if gte mso 9]>
<--[if gte mso 9]>

Game Programming in Visual Basic

By Greg English



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


winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net

Other 3 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.