Visual Basic Tutorial
In the tutorial we will use Visual Basic 6 to create a class module for game writing and
a simple demo program to show it's use.
To start create a new standard exe project and add a class module called 'Gamer'.
Our 'Gamer' class will do flickerless graphics and play multimedia files (waves, midi, etc.).
Add the following code to the Gamer class module.
Option Explicit
'Add multimedia capability
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Private mpicGame As PictureBox 'Points to the picture control on Game form.
Private mpicBuf As PictureBox 'Graphics buffer
Public Enum OpCode 'Orig Sprite Dest
srcAnd = vbSrcAnd '00 FF 00
srcCopy = vbSrcCopy '00 FF FF
srcErase = vbSrcErase 'FF FF 00
srcInvert = vbSrcInvert '??
srcPaint = vbSrcPaint '7F 80 FF
End Enum
Public Enum NumPads 'For Form KeyDown Event
gUpLeft = 103
gUp = 104
gUpRight = 105
gRight = 102
gDownRight = 99
gDown = 98
gDownLeft = 97
gLeft = 100
gMiddle = 101
gZero = 96
gPeriod = 110
gSlash = 111
gAsterisk = 106
gMinus = 109
gPlus = 107
gEnter = 13
gEscape = 27
End Enum
We declared the mciSendString API function to play waves etc. It uses a command string of the form
"play soundtrack.mid" or "play gunshot.wav".
We also declared two PictureBox control variables. The 'mpicGame' PictureBox variable will be a pointer
to the real PictureBox on the calling form (the one that the user sees). The second PictureBox 'mpicBuf'
will be the graphics buffer that we will draw on before displaying (flickerlessly). OpCode is an
enumeration of the different codes for copying one picture over another. 'srcAnd' would logically AND the
pixel, 'srcCopy' would just ignore the original and copy the seconf image etc.
Next we will create an initialize function that will be passed the visible PictureBox and create the
invisible buffer. It is important to set the AutoRedraw property of each PictureBox to True to
make the PaintPicture methods used later work.
Public Sub Init(picGame As PictureBox) 'Add a pictureBox control to form and set props
Dim frmParent As Form
Set mpicGame = picGame
Set frmParent = picGame.Parent
Set mpicBuf = frmParent.Controls.Add("vb.picturebox", "mpicBuf")
mpicBuf.ScaleMode = 3
mpicBuf.Visible = False
mpicBuf.Width = mpicGame.Width
mpicBuf.Height = mpicGame.Height
mpicBuf.AutoRedraw = True
End Sub
Next we will create a couple functions to clear the screen to a solid color or an image.
'If no background picture then clear and set color
Public Sub BackColor(lColor As Long)
mpicBuf.Cls
mpicBuf.BackColor = lColor
End Sub
'Copy background picture, x and y can be negative to create a moving background
Public Sub BackGround(nX As Integer, nY As Integer, pic As StdPicture)
mpicBuf.PaintPicture pic, nX, nY
End Sub
Sprites are small images that are copied over the background to create animated characters
like Mario etc. The user will store these images in Image Controls and copy them to the screen
with the following function.
'Copy sprite over background with different effects (OpCode) for masked sprites
Public Sub Sprite(nX As Integer, nY As Integer, pic As StdPicture, lOp As OpCode)
mpicBuf.PaintPicture pic, nX, nY, , , , , , , lOp
End Sub
There is usually a need to display text on the screen so we will add a couple of functions for that.
One to set the font and another to display text at an x,y position.
'Set the font stype for graphics buffer
Public Sub Font(strFontName As String, nSize As Integer, lColor As Long, bBold As Boolean, bItalic As Boolean)
mpicBuf.FontName = strFontName
mpicBuf.FontSize = nSize
mpicBuf.FontBold = bBold
mpicBuf.FontItalic = bItalic
mpicBuf.ForeColor = lColor
End Sub
'Write text at x,y
Public Sub Text(nX As Integer, nY As Integer, strText As String)
mpicBuf.CurrentX = nX
mpicBuf.CurrentY = nY
mpicBuf.Print strText
End Sub
We need a simple function to copy the buffer to the screen.
'Draws buffer to screen
Public Sub ReDraw()
mpicGame.PaintPicture mpicBuf.Image, 0, 0
End Sub
We also have encapsulated the mciSendString function with one of our own.
'Executes a multimedia command like "play boom.wav"
Public Sub SendMCI(strCommand As String)
Dim rep As String * 255
Dim l As Long
l = mciSendString(strCommand, rep, 0, 0)
End Sub
That's it for the 'Gamer' class at least for now. Let's move on to a simple demo form.
In the demo a plane will fly over a terrain. Here are the pics:


If a form wasn't created with the project create one now and call it 'Demo'. Add the
following code:
Option Explicit 'Always declare your variables!
Private gamTest As Gamer 'Add an instance of our class.
Private nSkyX As Integer 'Keep track of the Sky going by.
Private nPlaneY As Integer 'Keep track of the Plane going up and down.
Private nDeltaY As Integer 'Keep track of the planes direction up or down.
Private nTimesAround As Integer 'Keep track of the number of times around the "track".
Private bQuit as Boolean 'Set to true on escape or collision.
Add a PictureBox Control called Picture1, set the AutoRedraw property to True and
the Enabled property to false.
Add two Image Controls to the form off the visible screen and call them 'imgBackGround' and
'imgPlane'.
When the form loads we want to initialize our class and load up the images we will use
for the background and our one sprite. (The class can handle any number of sprites.)
Private Sub Form_Load()
Me.Show
Set gamTest = New Gamer
imgBackground.Picture = LoadPicture("c:/allen/land.gif")
imgPlane.Picture = LoadPicture("c:/allen/plane.gif")
gamTest.Init Picture1
gamTest.Font "Arial", 14, RGB(255, 0, 0), False, False
nDeltaY = -1: nPlaneY = 50
End Sub
In the forms KeyDown event add code to make the plane switch directions up and down
using the nDeltaY variable.
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case gUp
nDeltaY = -1
Case gMiddle
nDeltaY = 0
Case gDown
nDeltaY = 1
Case gEscape
bQuit = True
End Select
End Sub
For animation you need a timer so add a Timer Control to the form named 'timAni'.
Set the Timer Controls Interval to 100ms. Every 100ms we will want to:
1. Draw the background (or back color).
2. Draw the Sprites at their new location.
3. Draw any required text.
4. Draw the buffer to the screen.
5. Recalulate the Sprite positions.
Code it's Timer Event with the following:
Private Sub timAni_Timer()
gamTest.BackGround -nSkyX, 0, imgBackground.Picture
gamTest.Sprite 75, nPlaneY, imgPlane.Picture, srcAnd
gamTest.ReDraw
If Picture1.Point(75, nPlaneY) <> RGB(255, 255, 255) Then
bQuit = True 'If background is not white then we hit something!
End If
nSkyX = nSkyX + 1: nPlaneY = nPlaneY + nDeltaY
If (nPlaneY < 1) Then nPlaneY = 1
If (nPlaneY > 75) Then nPlaneY = 75
If (nSkyX = 400) Or (bQuit) Then
nTimesAround = nTimesAround + 1
nSkyX = 0
If (nTimesAround = 4) Or bQuit Then
timAni.Enabled = False
gamTest.BackGround -nSkyX, 0, imgBackground.Picture
gamTest.Sprite 75, nPlaneY, imgPlane.Picture, srcAnd
gamTest.Text 50, 50, "GAME OVER"
gamTest.ReDraw
End If
End If
End Sub
That's it! Run the program and see if there are errors. If not try adding some more sprites, check for a collision with blue and add a score, check for a collision with red and subtract from the score. (Or make a better game all together!)
If you don't like typing download the source.