VOTE,ONLY,TAKES,FEW,article,lesson,miniseries
Quick Search for:  in language:    
VOTE,ONLY,TAKES,FEW,article,lesson,miniseries
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
RentACoder Stats

 Code:  lines
 Jobs: 0 postings

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for RentACoder.
Wrapping Scrolling Text
By Paranoid_Androi d on 7/2


Create A Dummy File
By AML on 7/2


Click here to see a screenshot of this code!Captionbar manipulation!
By Peter Hebels on 7/2

(Screen Shot)

A Game Of War
By Co0nest on 7/2


Click here to see a screenshot of this code!KeyGen Example
By Bengie|NET on 7/2

(Screen Shot)

Click here to see a screenshot of this code!OpenBrowser v1.9
By Orlando Jerez on 7/2

(Screen Shot)

SendMessageBySt ring() Example
By Jaime Muscatelli on 7/2


Click here to see a screenshot of this code!FirstSunday
By Jan Paul Penning on 7/2

(Screen Shot)

Click here to see a screenshot of this code!Ikonz v1.0
By Gaurav Creations on 7/2

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

Game Programming in Visual Basic - Lesson Two

Print
Email
 

Submitted on: 4/6/2002 8:55:39 PM
By: Greg L English  
Level: Beginner
User Rating: By 34 Users
Compatibility:

Users have accessed this article 5373 times.
 
(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 langauges 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 Winzipto 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.

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 langauges 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 notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
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 9:05:51 PM:Isaac Luxford
Although I'm already familiar with these techniques, you have done a fantastic job and anyone that hasn't had experience with this area should be able to learn a great deal from you. It's good to see people taking the time to explain things to others.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/6/2002 10:01:13 PM:Greg L. English
Thanks a lot man, i appreciate it. :)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/6/2002 10:12:59 PM:Jason Hensley
You should not use the SndPlaySound api. Instead you should use the PlaySound api. Jason
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/7/2002 12:21:25 AM:Isaac Luxford
Greg, I was just wondering where this series of tutorials will be heading in the future? Are you planning to expand towards DirectX?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/7/2002 12:31:34 AM:Johnny 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!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/7/2002 1:47:21 AM:Mark 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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/7/2002 11:16:54 AM:Greg 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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/7/2002 7:19:48 PM:Mike
Nice job i'm sure it took a lot of work you got my vote
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/27/2002 2:29:55 PM:Chill
Cool code, helped me on several things. Nice job, Keep up the good work!!! 5 stars from me.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/30/2002 11:23:28 PM:King Of Code
thanks a lot man, helped out a lot! great job! keep it up!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/28/2002 1:41:53 PM:flyzzx
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(vbKeyContr ol Or vbKeyA) if ret<>0 then MsgBox "Hi" End If End Sub
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/28/2002 1:50:06 PM:flyzzx
Private Sub Timer1_Timer()<br>Dim ret as long<br><br>'use Or keyword <br>ret=GetAsyncKeyState(vbKeyContr ol Or vbKeyA)<br>if ret<>0 then<br> MsgBox "Hi"
End If

End Sub
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/29/2002 1:25:53 PM:J. 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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/29/2002 1:57:57 PM:J. 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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/13/2002 3:37:22 AM:Khun
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/1/2002 3:46:57 PM:Jeremy Boyd
This is the best Game Toutorial I have ever seen
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | RentACoder Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.