Important alert: (current site time 4/18/2014 6:57:33 AM EDT)
 

article

Game Programming in Visual Basic - Lesson Two

Email
Submitted on: 4/6/2002 8:55:39 PM
By: Gregory English  
Level: Beginner
User Rating: By 53 Users
Compatibility: VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 79540
(About the author)
 
     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
 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				






Game Programming in Visual Basic
<--[if gte mso 9]>
 
 MR Ronald W. English
 MR Ronald W. English
 16
 23
 2002-04-06T20:34:00Z
 2002-04-06T20:56:00Z
 3
 802
 4575
 English Enterprises Inc,
 38
 9
 5618
 9.2720
 


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


Game Programming in Visual Basic

By Greg English

 

Introduction

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.

 

BitBlt

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]

 

GetAsyncKeyState

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

Else

‘//code here

End If

[Code Stop]

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

 

SndPlaySound

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.

 

IntersectRect

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 voodoovb.thenexus.bc.ca, 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 rookscape.com/vbgaming, with lots of other cool tutorials on such stuff, including some APIs I used.

 

Conclusion

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 EnglishM1@aol.com

 

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 including:McAfee.com

 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.


Other 8 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

4/6/2002 10:01:13 PMGreg L. English

Thanks a lot man, i appreciate it. :)
(If this comment was disrespectful, please report it.)

 
4/6/2002 10:12:59 PMJason Hensley

You should not use the SndPlaySound api. Instead you should use the PlaySound api.


Jason
(If this comment was disrespectful, please report it.)

 
4/7/2002 12:31:34 AMJohnny Fisher

I've looked at a couple other tutorials that deal with this and this was by far much better than either came in terms of actually getting my attention and making me really understand it and also taught me how to use it. This is great for a beginner like me. Thank You. 5 Globes!
(If this comment was disrespectful, please report it.)

 
4/7/2002 1:47:21 AMMark Lu

Actually, sndPlaySound is all you really actually need. PlaySound is a little too difficult for some beginners. What's the difference? Not much. Well, this is written professional, nice, neat and clean. 5 globes from me. Even though I am fimilar with these functions. Next time, use <pre> & </pre> for the code as it looks much better.
(If this comment was disrespectful, please report it.)

 
4/7/2002 11:16:54 AMGreg L English

Well Isaac, thats what Im debating for the next lesson, in the next lesson I may go into File I/O with games, and extra features in games like sprite animation instead of just the normal sprites. So I'd be spicing up the asteroids game to make it a lil cooler. I do plan on going into DirectX 7 because of its ease in 2D Graphics, then go into DirectXGraphics in DX8 so we can get some cool graphics, and maybe even some network play with a space game or something not sure yet. All depends on the input people give me.

(If this comment was disrespectful, please report it.)

 
4/7/2002 7:19:48 PMMike

Nice job i'm sure it took a lot of work you got my vote
(If this comment was disrespectful, please report it.)

 
4/9/2002 1:57:32 AM[]

I'll give u my vote even though I have not looked at the example yet.

[] [] [] [] []

from me
(If this comment was disrespectful, please report it.)

 
4/27/2002 2:29:55 PMChill

Cool code, helped me on several things. Nice job, Keep up the good work!!! 5 stars from me.
(If this comment was disrespectful, please report it.)

 
4/30/2002 11:23:28 PMKing Of Code

thanks a lot man, helped out a lot! great job! keep it up!
(If this comment was disrespectful, please report it.)

 
5/28/2002 1:41:53 PMflyzzx

Great article! This is suitable for beginners.5 globes from me.

GetAsyncKeystate function can also reconize more than one pressed key in the same time.

This function is useful for creating hotkeys.

For example:

Private Sub Timer1_Timer()
Dim ret as long

'use Or keyword
ret=GetAsyncKeyState(vbKeyControl Or vbKeyA)
if ret<>0 then
MsgBox "Hi"
End If

End Sub
(If this comment was disrespectful, please report it.)

 
5/28/2002 1:50:06 PMflyzzx

Private Sub Timer1_Timer()
Dim ret as long

'use Or keyword
ret=GetAsyncKeyState(vbKeyContr ol Or vbKeyA)
if ret<>0 then
MsgBox "Hi"
End If

End Sub
(If this comment was disrespectful, please report it.)

 
5/29/2002 1:25:53 PMJ. B.

Nice tutorial but I know of a website with a lot of tutorials involving DirectX 7 and 8. I can't remember the url though but the title was something like DirectX for VB or something. They taught about path finding, lighting effects, and of course they had from novice on up to stuff even more detailed than what I mentioned (I can't remember everything they taught because it's been months since I visited the site but they start out with the basics on making a game, you should be comfortable with VB before going there though). It's a really good site and if I remember the url I'll post the link. It's where I learned DirectX. This is still a good tutorial though and by no means should you stop here. I've been writing a game simply to learn how it all works. It's unfortunate that after I had it nearly complete I had to stop working on it to go to school where I've yet to learn anything new about programming ... but that's another story.
(If this comment was disrespectful, please report it.)

 
5/29/2002 1:57:57 PMJ. B.

Duh! *Smacks himself for being dumb* The url is http://www.directx4vb.com/ ... they are hosted by this site (I think). Anyhow, check them out. They helped me a lot.
(If this comment was disrespectful, please report it.)

 
6/13/2002 3:37:22 AMKhun

Argh, that VoodooVB site isn't working so I can' go check out their game engine thingy. I'd like to know what "CreateCompatibleDC" and "CreateCompatibleBitmap" does, if you're interested in ideas for your next tutorial :D
Great tutorial, that really helped me a lot
(If this comment was disrespectful, please report it.)

 
7/1/2002 3:46:57 PMJeremy Boyd

This is the best Game Toutorial I have ever seen
(If this comment was disrespectful, please report it.)

 
7/3/2002 4:40:35 AMMax Cornett

Well done Tutorial!! (im 12 years old)
O O O O O Globes from me!
(If this comment was disrespectful, please report it.)

 
7/7/2002 2:09:28 AMKhun

Great Tutorial!!! The voodoovb website doesn't work though.
(If this comment was disrespectful, please report it.)

 
8/14/2002 2:55:50 PMJOSHUA STEWARD

Great stuff but how would i yse a 3ds file in a game?
(If this comment was disrespectful, please report it.)

 
8/14/2002 5:25:59 PMrkinasz

One thing i noticed was you had Randomize in a timer..and you had it twice...

You should only have Randomize called ONE time in your program...or it wont really be random.

Just put it in your Form_Load

Great tutorial though...5 globes from me!
(If this comment was disrespectful, please report it.)

 
8/15/2002 2:12:36 AMscorpydude

There is NO doubt about it somone should convince this dude to make a website then email with the url :-)

scorpydude@Hotmail.com

if you dont find some way to teach people you wont be doing what your good at

good work with this mate 5star
(If this comment was disrespectful, please report it.)

 
8/15/2002 7:50:10 PMGreg L English

New Link for voodoovb
www.mwgames.com/voodoovb

Btw, i just started work on another tutorial adding to this game, and its also moving to DirectX 8(screw DX7 hehe)
(If this comment was disrespectful, please report it.)

 
10/13/2002 1:05:34 PM

yo greg fantastic job man :) cya on monday ... i'll kick yer butt in UT ;)

nice tutorial, i know most of the stuff but its a great reference, now on with the programming :)
(If this comment was disrespectful, please report it.)

 
2/20/2004 9:47:16 AMCraig Thomson

Thank you for sharing, n00b game programmers like myelf will find a good use for this,

Cheers Mate!
(If this comment was disrespectful, please report it.)

 
2/23/2004 6:32:59 AM

its really good and helpfull for the beginners.i wish that i may get many more such like this in future also
(If this comment was disrespectful, please report it.)

 
4/25/2005 6:30:05 AMSPY-3

Great tutorial and game sample. Only problem on the game i see is that it starts off on 32(which isnt bad) but as the levels go on it can get to 10 and then its a slow motion game but great example of making a game 5 globes!!!!!
(If this comment was disrespectful, please report it.)

 
1/26/2006 2:28:58 AMbrownhead

Didn't actually look at the tutorial (sorry, got to much junkk piling up on my computer) but it sounds like a hook would be a bit better for a game.
(If this comment was disrespectful, please report it.)

 

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.