Visual Basic


Welcome to OOP (Object Oriented Programming)! You've succesfully mastered QBASIC and LibertyBasic, and here's your reward.
A complete suite of Windows programming lies at your disposal. VB is the most used Windows programming langauge.
We'll be covering the 5.0 version of VB. Visual Basic was first created in 1989 for Windows 1.0. Ah, the bad old days :-).
Now VB 5.0 was designed with Windows 95 in mind. However I recomend using Windows 98 at least for this version of VB.

The lesson things are:

The VB Shell

The VB shell looks like this:


The VB main screen
Now to explain:
VB is a strictly graphical langauge. What you is sorta like a WISIWIG editor (whith coding capability). The icons in the tab on the left are called controls.
A control is a emmbeded mini-application that helps your program run. Although it's possible to create a good game without controls, it's very difficult.
Controls are similar to Java applets. Another name for these controls is ActiveX. You'll need to know that name because you'll be hearing it a lot.
These controls have little variables called properties which we'll discuse later. If you double-click a control or the main form a code window will pop up:


VB code window
VB code is generaly simalar to QBASIC, with a few key differences.

Setting it up

When you start VB you'll see the following screen:


The VB start-up screen
You'll most likely double-click 'Standard EXE', which will start VB for a regular program. Standard EXE is the only option in the version I'm giving you *smirk*.
Other options if you buy a version are:

The MsgBox statment

Ever a dialog box? The answer is fairly obvious. Yes, if you use Windows® you've seen a dialog box! You get those things too!
To get a message box (dialoge box) use the MsgBox statment. MsgBox takes the following syntax:

MsgBox "Hello, World!", VbInformation, "Hi!"

Here's what that looks like:

The MsgBox subroutine (sub) puts up the mesage box. The "Hello, World!" tells VB what message you want (The message is the only required text).
The VbInformation tells it what type of icon you want. Here's a list of the icons (and number values you can use):

IconWordsNumbers
CriticalVbCritical8
QuestionVbQuestion16
ExclamationVbExclamation32
InformationVbInformation64

The "Hi!" tells it what title you want.

Removed Support

Several QBASIC features have been removed or replaced in VB 5.0. First off is the SOUND command. Sound has been removed from VB.
However you can still use DirectSound (which I'll teach you later). The BEEP statment has been kept though, only now it plays through the external speakers.
The INPUT statment has been removed and replaced with InputBox. Here's an example of InputBox:

name = InputBox ("What is your name?")

You can fool around with that code. VB still supports Lprint.

Dim

A new feature of VB is that you must declare your variables. To declare a variable use the Dim statment. Dim has the following syntax:

Dim name As String

There a several types of variables. String is the same as having a $ sign in QBASIC. An integer is a number value. Variant is the default, and will hold anything.

DirectSound

Before you use this, you'll need to install DirectX. You most likely have when you install a game. Just follow this code (I'll explain later):

Option Explicit

'We ALWAYS need the root DirectX object
'Note the use of 'New' in this line - this is important.
Dim Dx As New DirectX7
'All things DirectSound come from this next object.
'The Directsound object is based on the DirectX object.
Dim Ds As DirectSound

'This is a buffer for holding the sound data.
'You can have as many of these as you like, and/or you
'can have them in an array. Although, try to keep the number
'of buffers used to a minimum - for good practise. It should be
'fairly obvious to you that you shouldn't use any more of anything
'than you actually need...
Dim DsBuffer As DirectSoundBuffer

'These two describe the settings for the
'Buffer. See the Initialise procedure for more details.
Dim DsDesc As DSBUFFERDESC
Dim DsWave As WAVEFORMATEX
Sub Initialise()
Set Ds = Dx.DirectSoundCreate("")

'It is best to check for errors before continuing
If Err.Number <> 0 Then
    MsgBox "Unable to Continue, Error creating Directsound object."
    Exit Sub
End If

'This tells DX how much we want directsound. If it
'is set to normal DX will allow other programs to use the soundcard
'If it is set to Exclusive ONLY we get to use the soundcard. Which
'is what you want when writing a game...
Ds.SetCooperativeLevel Form1.hWnd, DSSCL_NORMAL


'These settings are quite important; what you set here reflects
'in the quality that the sound plays.
'8bit + 1 channel = Poor quality, but less processing time and less memory
'16bit + 2 channels = Good quality, more memory and more processing time
'The comments about processing time and memory will only affect you
'when you start putting a heavy load on the sound card; and/or it is
'playing/storing lots of sound files.
DsDesc.lFlags = DSBCAPS_CTRLFREQUENCY Or DSBCAPS_CTRLPAN Or DSBCAPS_CTRLVOLUME Or DSBCAPS_STATIC
DsWave.nFormatTag = WAVE_FORMAT_PCM 'Sound Must be PCM otherwise we get errors
DsWave.nChannels = 2    '1= Mono, 2 = Stereo
DsWave.lSamplesPerSec = 22050
DsWave.nBitsPerSample = 16 '16 =16bit, 8=8bit
DsWave.nBlockAlign = DsWave.nBitsPerSample / 8 * DsWave.nChannels
DsWave.lAvgBytesPerSec = DsWave.lSamplesPerSec * DsWave.nBlockAlign

'This line creates a sound buffer based on the information you have just
'put in the two structures
Set DsBuffer = Ds.CreateSoundBufferFromFile(App.Path & "\Tester.Wav", DsDesc, DsWave)

End Sub


Sub PlaySound()

'Set the next lines flag to DSBPLAY_DEFAULT if you
'only want it to play once.
DsBuffer.Play DSBPLAY_LOOPING

'One thing to note, the program will continue operating whilst the
'sound is still playing. This is called Asyncronous processing.
'If you are used to the API sound functions, you may know that
'by default you're program stops running whilst the sound plays

'Because it works like this you can play multiple sounds at once.
'But, if you want you're program to wait for the sound to finish you will
'have to write in a little loop that keeps going until
'the sound has reached the end.
End Sub


Sub StopSound()
'Stopping it effectively pauses it
DsBuffer.Stop
'Only when you set the current position to 0 will
'it go back to the beginning
DsBuffer.SetCurrentPosition 0
'You can use the SetCurrentPosition to jump around
'a sound file as you like; which can be useful
End Sub


Private Sub Form_Load()
'Just sets up the interface
'Make sure the form is visible before we load.
'Otherwise the user won't see anything until the program has
'finished loading the DirectX stuff - which can take several seconds
Me.Show
'YOU MUST INITIALISE THE PROGRAM BEFORE YOU TRY
'AND PLAY ANY SOUNDS!!!
Call Initialise
'This will currently loop the sound until we close the program.
Call PlaySound

End Sub


Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Call StopSound
End Sub

There's a few things you'll need to know when putting this in your game (just copy and paste). The 'Form1.hwnd' must be set to the name
of your form plus .hwnd. The 'Tester.wav' must be set to the .wav file you want to play. To use this code in VB follow the following instructions:
Click the 'Project' menu. Click 'References'. Scoll down and click 'DirectX for Visual Basic type library'. Click OK.
Thanks to Jack Hoxley for writing this code.

Properties

Properties are variables that hold info about an control or form. The most common properties you'll be using in VB are Top, Left, and Visible.
Top and Left are effectively the X and Y coordinate. Visible is a True or False value that determines whether it's visible.
An Image Control is one you'll be using most often in VB gaming. The image control has a property called picture that determines the picture.

Strategy

VB is an awesome game programming langauge. Look other places to learn more.

Download VB 5.0

You must download the .ZIP file and extract the files into the same directory.

VB5.zip

If you don't have WinZip, Click Here.

Samples

Here's some sample VB games (without code):


Nighthawk and Flyswatter are copyright © 2004 AbsoluteZer0 Computer Software. Code Red is copyright © 2003 Taicade Inc. Domination is copyright © 2004 Jmrs Development.