Sie sind auf Seite 1von 33

FrmDog - 1

'*****************************************************************
'
'SAVE THE DOG - THE GAME
'
'Feel free to re-use any of this code that you find useful. The
'game is not complete, you will notice that the artwork is missing
'or unfinished in most cases, but the playability is intact and the
'coding has been finished. This serves as an example for the use
'of basic DirectDraw, DirectInput, and DirectSound functions.
'Mouse support is provided via the windows API.
'
'Lucky
'theluckyleper@home.com
'http://members.home.net/theluckyleper
'
'*****************************************************************

Private Sub Form_Load()

DDraw.Initialize FrmDog.hWnd 'Initialize DirectDraw


DInput.Initialize FrmDog.hWnd 'Initialize DirectInput
DSound.Initialize FrmDog.hWnd 'Initialize DirectSound
Cursor.Initialize 'Initialize (remove) the Cursor
Me.Show 'Show the form
MainLoop 'Run the main loop

End Sub

Private Sub MainLoop()

On Error GoTo ErrOut

'Set up the control booleans


EntryRunning = True
InfoRunning = False
GameRunning = False
EntryInitialized = False
InfoInitialized = False
GameInitialized = False

Do While True
If EntryRunning Then
'Run the entry screen
If EntryInitialized = False Then EntryScreen.Initialize
EntryScreen.Physics 'Handle animations and physics
EntryScreen.Display 'Display the appropriate frame of the sprites
DDraw.Flip 'Flip the backbuffer to the screen
DDraw.ClearBuffer 'Erase the backbuffer so that it can be drawn on again
DoEvents 'Give windows its chance to do things
If Not EntryRunning Then EntryScreen.Terminate
ElseIf InfoRunning Then
'Run the info screen
If InfoInitialized = False Then InfoScreen.Initialize
InfoScreen.Display 'Display the appropriate frame of the sprites
DDraw.Flip 'Flip the backbuffer to the screen
DDraw.ClearBuffer 'Erase the backbuffer so that it can be drawn on again
DoEvents 'Give windows its chance to do things
If Not InfoRunning Then InfoScreen.Terminate
ElseIf GameRunning Then
'Run the game portion
If GameInitialized = False Then GameScreen.Initialize
GameScreen.HandleKeys 'Check the DirectInput data for significant keypresses
GameScreen.Physics 'Handle animations and physics
GameScreen.Display 'Display the appropriate frame of the sprites
DDraw.Flip 'Flip the backbuffer to the screen
DDraw.ClearBuffer 'Erase the backbuffer so that it can be drawn on again
DoEvents 'Give windows its chance to do things
If Not GameRunning Then GameScreen.Terminate
End If
Loop

ErrOut:

ExitProgram 'If an error occurs, leave the program

End Sub
FrmDog - 2

Private Sub Form_Click()

'Handle mouse clicks


If InfoRunning = True Then InfoScreen.Click
If GameRunning = True Then GameScreen.Click

End Sub

Public Sub ExitProgram()

If EntryRunning = True Then EntryScreen.Terminate 'Unload game objects


If GameRunning = True Then GameScreen.Terminate 'Unload game objects
DDraw.Terminate (FrmDog.hWnd) 'Unload the DirectDraw variables
DInput.Terminate 'Unload the DirectInput variables
DSound.Terminate 'Unload the DirectSound variables
Cursor.Terminate 'Redisplay the cursor
End 'End the program

End Sub
FrmDog - 1

VERSION 5.00
Begin VB.Form FrmDog
BackColor = &H00000000&
BorderStyle = 0 'None
Caption = "Dog"
ClientHeight = 3195
ClientLeft = 0
ClientTop = 0
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 4680
ShowInTaskbar = 0 'False
StartUpPosition = 3 'Windows Default
End
Cursor - 1

'***************
'
'This module con
'restore the mou
'to call the xPo
'know the precis
'
'
'Lucky
'
Cursor - 2

ShowCursor CURVISIBLE

End Sub
DDraw - 1

'Major DX Objects
Dim dx As New DirectX7
Dim dd As DirectDraw7

Dim Primary As DirectDrawSurface7 'Primary surface


Dim BackBuffer As DirectDrawSurface7 'Backbuffer surface
Dim ddsdPrimary As DDSURFACEDESC2 'Primary surface description
Dim ddsdBackBuffer As DDSURFACEDESC2 'Backbuffer surface description

Global FrameTime As Long 'System time that last frame was flipped at
Const FrameRate = 100 'How many MS per frame?

Public Sub Initialize(WindowHandle As Long)

'This routine initializes the display mode, and the primary/backbuffer complex

'Handles errors
On Local Error GoTo ErrOut

'Creates the directdraw object


Set dd = dx.DirectDrawCreate("")

'Set the cooperative level and displaymode...


Call dd.SetCooperativeLevel(FrmDog.hWnd, DDSCL_FULLSCREEN Or DDSCL_EXCLUSIVE Or DDSCL_ALLOWREBO
OT)
Call dd.SetDisplayMode(800, 600, 8, 0, DDSDM_DEFAULT)

'Create the primary complex surface with one backbuffer


ddsdPrimary.lFlags = DDSD_CAPS Or DDSD_BACKBUFFERCOUNT
ddsdPrimary.ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE Or DDSCAPS_FLIP Or DDSCAPS_COMPLEX
ddsdPrimary.lBackBufferCount = 1
Set Primary = dd.CreateSurface(ddsdPrimary)

'Get the backbuffer from the primary surface


Dim caps As DDSCAPS2
caps.lCaps = DDSCAPS_BACKBUFFER
Set BackBuffer = Primary.GetAttachedSurface(caps)

'Clears the buffer


ClearBuffer

Exit Sub

ErrOut:
FrmDog.ExitProgram 'If there's an error, exit the program

End Sub

Public Sub ClearBuffer()

Dim DestRect As RECT

'Clears the backbuffer


With DestRect
.Bottom = 600
.Left = 0
.Right = 800
.Top = 0
End With
BackBuffer.BltColorFill DestRect, 0

End Sub

Public Function LoadSurface(Path As String, ddsd As DDSURFACEDESC2) As DirectDrawSurface7

'Loads the surface using the given path and description


Set LoadSurface = dd.CreateSurfaceFromFile(Path, ddsd)

End Function

Public Sub DisplaySprite(DestRect As RECT, Sprite As DirectDrawSurface7, SrcRect As RECT, Optional


Fast As Boolean, Optional xCoord As Long, Optional yCoord As Long)

If Fast = True Then


'Blit the sprite fast to the backbuffer
BackBuffer.BltFast xCoord, yCoord, Sprite, SrcRect, DDBLTFAST_WAIT
Else
DDraw - 2

'Clip the sprite if any part will be off screen


With DestRect
If .Bottom > 600 Then
SrcRect.Bottom = SrcRect.Bottom - (.Bottom - 600)
.Bottom = 600
End If
If .Left < 0 Then
SrcRect.Left = SrcRect.Left - .Left
.Left = 0
End If
If .Right > 800 Then
SrcRect.Right = SrcRect.Right - (.Right - 800)
.Right = 800
End If
If .Top < 0 Then
SrcRect.Top = SrcRect.Top - .Top
.Top = 0
End If
End With
'Blit the sprite with colour key to the backbuffer
BackBuffer.Blt DestRect, Sprite, SrcRect, DDBLT_KEYSRC Or DDBLT_WAIT
End If

End Sub

Public Sub Flip()

'Slow the frame rate down


Do While Timer.Time - FrameTime < FrameRate
DoEvents
Loop

'Flip the attached surface (the backbuffer) to the screen


Primary.Flip Nothing, DDFLIP_WAIT

'Record the time of the flip


FrameTime = Timer.Time

End Sub

Public Sub TextOut(OutText As String, x As Integer, y As Integer, Colour As Long)

'This function writes text to the backbuffer


BackBuffer.SetForeColor Colour
Call BackBuffer.DrawText(x, y, OutText, False)

End Sub

Public Sub Terminate(WindowHandle As Long)

Dim i As Integer

'This routine must destroy all surfaces and restore display mode
Set Primary = Nothing
Set BackBuffer = Nothing
Set BackGround = Nothing

Call dd.RestoreDisplayMode
Call dd.SetCooperativeLevel(WindowHandle, DDSCL_NORMAL)

End Sub
DInput - 1

'dX Variables
Dim dx As New DirectX7
Dim di As DirectInput
Dim diDEV As DirectInputDevice
Dim diState As DIKEYBOARDSTATE

'Loop counter
Dim i As Integer

'Public array showing which keys are active


Public aKeys(211) As Boolean

'Keycode constants
Global Const DIK_ESCAPE = 1
Global Const DIK_1 = 2
Global Const DIK_2 = 3
Global Const DIK_3 = 4
Global Const DIK_4 = 5
Global Const DIK_5 = 6
Global Const DIK_6 = 7
Global Const DIK_7 = 8
Global Const DIK_8 = 9
Global Const DIK_9 = 10
Global Const DIK_0 = 11
Global Const DIK_MINUS = 12
Global Const DIK_EQUALS = 13
Global Const DIK_BACKSPACE = 14
Global Const DIK_TAB = 15
Global Const DIK_Q = 16
Global Const DIK_W = 17
Global Const DIK_E = 18
Global Const DIK_R = 19
Global Const DIK_T = 20
Global Const DIK_Y = 21
Global Const DIK_U = 22
Global Const DIK_I = 23
Global Const DIK_O = 24
Global Const DIK_P = 25
Global Const DIK_LBRACKET = 26
Global Const DIK_RBRACKET = 27
Global Const DIK_RETURN = 28
Global Const DIK_LCONTROL = 29
Global Const DIK_A = 30
Global Const DIK_S = 31
Global Const DIK_D = 32
Global Const DIK_F = 33
Global Const DIK_G = 34
Global Const DIK_H = 35
Global Const DIK_J = 36
Global Const DIK_K = 37
Global Const DIK_L = 38
Global Const DIK_SEMICOLON = 39
Global Const DIK_APOSTROPHE = 40
Global Const DIK_GRAVE = 41
Global Const DIK_LSHIFT = 42
Global Const DIK_BACKSLASH = 43
Global Const DIK_Z = 44
Global Const DIK_X = 45
Global Const DIK_C = 46
Global Const DIK_V = 47
Global Const DIK_B = 48
Global Const DIK_N = 49
Global Const DIK_M = 50
Global Const DIK_COMMA = 51
Global Const DIK_PERIOD = 52
Global Const DIK_SLASH = 53
Global Const DIK_RSHIFT = 54
Global Const DIK_MULTIPLY = 55
Global Const DIK_LALT = 56
Global Const DIK_SPACE = 57
Global Const DIK_CAPSLOCK = 58
Global Const DIK_F1 = 59
Global Const DIK_F2 = 60
Global Const DIK_F3 = 61
Global Const DIK_F4 = 62
Global Const DIK_F5 = 63
Global Const DIK_F6 = 64
DInput - 2

Global Const DIK_F7 = 65


Global Const DIK_F8 = 66
Global Const DIK_F9 = 67
Global Const DIK_F10 = 68
Global Const DIK_NUMLOCK = 69
Global Const DIK_SCROLL = 70
Global Const DIK_NUMPAD7 = 71
Global Const DIK_NUMPAD8 = 72
Global Const DIK_NUMPAD9 = 73
Global Const DIK_SUBTRACT = 74
Global Const DIK_NUMPAD4 = 75
Global Const DIK_NUMPAD5 = 76
Global Const DIK_NUMPAD6 = 77
Global Const DIK_ADD = 78
Global Const DIK_NUMPAD1 = 79
Global Const DIK_NUMPAD2 = 80
Global Const DIK_NUMPAD3 = 81
Global Const DIK_NUMPAD0 = 82
Global Const DIK_DECIMAL = 83
Global Const DIK_F11 = 87
Global Const DIK_F12 = 88
Global Const DIK_NUMPADENTER = 156
Global Const DIK_RCONTROL = 157
Global Const DIK_DIVIDE = 181
Global Const DIK_RALT = 184
Global Const DIK_HOME = 199
Global Const DIK_UP = 200
Global Const DIK_PAGEUP = 201
Global Const DIK_LEFT = 203
Global Const DIK_RIGHT = 205
Global Const DIK_END = 207
Global Const DIK_DOWN = 208
Global Const DIK_PAGEDOWN = 209
Global Const DIK_INSERT = 210
Global Const DIK_DELETE = 211

Public Sub Initialize(WindowHandle As Long)

'Create the direct input object


Set di = dx.DirectInputCreate()

'Aquire the keyboard as the device


Set diDEV = di.CreateDevice("GUID_SysKeyboard")

'Get input nonexclusively, only when in foreground mode


diDEV.SetCommonDataFormat DIFORMAT_KEYBOARD
diDEV.SetCooperativeLevel WindowHandle, DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE
diDEV.Acquire

End Sub

Public Sub CheckKeys()

'Get the current state of the keyboard


diDEV.GetDeviceStateKeyboard diState

'Scan through all the keys to check which are depressed


For i = 1 To 211
If diState.Key(i) <> 0 Then
aKeys(i) = True 'If the key is pressed, set the appropriate array index to
true
Else
aKeys(i) = False 'If the key is not pressed, set the appropriate array index
to false
End If
Next

End Sub

Public Sub Terminate()

'Unaquire the keyboard when we quit


diDEV.Unacquire

End Sub
DSound - 1

'DirectX Variables
Dim ds As DirectSound
Dim dx As New DirectX7

'User defined type to determine a buffer's capabilities


Private Type BufferCaps
Volume As Boolean 'Can this buffer's volume be changed?
Frequency As Boolean 'Can the frequency be altered?
Pan As Boolean 'Can we pan the sound from left to right?
Loop As Boolean 'Is this sound looping?
Delete As Boolean 'Should this sound be deleted after playing?
End Type

'User defined type to contain sound data


Private Type SoundArray
DSBuffer As DirectSoundBuffer 'The buffer that contains the sound
DSState As String 'Describes the current state of the buffer (ie. "Playing", "Sto
pped")
DSNotification As Long 'Contains the event reference returned by the DirectX7 object
DSCaps As BufferCaps 'Describes the buffer's capabilities
DSSourceName As String 'The name of the source file
DSFile As Boolean 'Is the source in a seperate file?
DSResource As Boolean 'Or is it in a resource?
DSEmpty As Boolean 'Is this SoundArray index empty?
End Type
Dim Sound() As SoundArray 'Contains all the data needed for sound manipulation

'Constant that contains the path inside the app.path in which the sounds are stored
Const DataLocation = "\data\"

'Constants that describe the contents of the sound buffers


Global Const SndBoyShoot = 0
Global Const SndClick = 1
Global Const SndDogDead = 2
Global Const SndDogHit = 3
Global Const SndDogYap = 4
Global Const SndIntro = 5
Global Const SndManHit = 6
Global Const SndYouLose = 7
Global Const SndYouWin = 8

'Array to contain the ID's for the sounds


Global SoundID(8) As Integer

'Wave Format Setting Contants


Const NumChannels = 2 'How many channels will we be playing on?
Const SamplesPerSecond = 22050 'How many cycles per second (hertz)?
Const BitsPerSample = 16 'What bit-depth will we use?

Public Sub Initialize(ByRef Handle As Long)

'If we can't initialize properly, trap the error


On Local Error GoTo ErrOut

'Make the DirectSound object


Set ds = dx.DirectSoundCreate("")

'Set the DirectSound object's cooperative level (Priority gives us sole control)
ds.SetCooperativeLevel Handle, DSSCL_PRIORITY

'Initialize our Sound array to zero


ReDim Sound(0)
Sound(0).DSEmpty = True
Sound(0).DSState = "empty"

'Load all of the sounds into buffers


SoundID(SndBoyShoot) = LoadSound("boyshoot.wav", True, False, False, False, False, False, False
, FrmDog)
SoundID(SndClick) = LoadSound("click.wav", True, False, False, False, False, False, False, FrmD
og)
SoundID(SndDogDead) = LoadSound("dogdead.wav", True, False, False, False, False, False, False,
FrmDog)
SoundID(SndDogHit) = LoadSound("doghit.wav", True, False, False, False, False, False, False, Fr
mDog)
SoundID(SndDogYap) = LoadSound("dogyap.wav", True, False, False, False, False, False, False, Fr
mDog)
SoundID(SndIntro) = LoadSound("intro.wav", True, False, False, False, False, False, False, FrmD
DSound - 2

og)
SoundID(SndManHit) = LoadSound("manhit.wav", True, False, False, False, False, False, False, Fr
mDog)
SoundID(SndYouLose) = LoadSound("youlose.wav", True, False, False, False, False, False, False,
FrmDog)
SoundID(SndYouWin) = LoadSound("youwin.wav", True, False, False, False, False, False, False, Fr
mDog)

'Exit sub before the error code


Exit Sub

ErrOut:
'Display an error message and exit if initialization failed
MsgBox "Unable to initialize DirectSound."
End

End Sub

Public Function LoadSound(SourceName As String, IsFile As Boolean, IsResource As Boolean, IsDelete


As Boolean, IsFrequency As Boolean, IsPan As Boolean, IsVolume As Boolean, IsLoop As Boolean, FormO
bject As Form) As Integer

Dim i As Integer
Dim Index As Integer
Dim DSBufferDescription As DSBUFFERDESC
Dim DSFormat As WAVEFORMATEX
Dim DSPosition(0) As DSBPOSITIONNOTIFY

'Search the sound array for any empty spaces


Index = -1
For i = 0 To UBound(Sound)
If Sound(i).DSEmpty = True Then 'If there is an empty space, us it
Index = i
Exit For
End If
Next
If Index = -1 Then 'If there's no empty space, make a new spot
ReDim Preserve Sound(UBound(Sound) + 1)
Index = UBound(Sound)
End If
LoadSound = Index 'Set the return value of the function

'Load the Sound array with the data given


With Sound(Index)
.DSEmpty = False 'This Sound(index) is now occupied with data
.DSFile = IsFile 'Is this sound to be loaded from a file?
.DSResource = IsResource 'Or is it to be loaded from a resource?
.DSSourceName = SourceName 'What is the name of the source?
.DSState = "Stopped" 'Set the current state to "Stopped"
.DSCaps.Delete = IsDelete 'Is this sound to be deleted after it is played?
.DSCaps.Frequency = IsFrequency 'Is this sound to have frequency altering capabilities?
.DSCaps.Loop = IsLoop 'Is this sound to be looped?
.DSCaps.Pan = IsPan 'Is this sound to have Left and Right panning capabilities?
.DSCaps.Volume = IsVolume 'Is this sound capable of altered volume settings?
End With

'Set the buffer description according to the data provided


With DSBufferDescription
If Sound(Index).DSCaps.Delete = True Then .lFlags = .lFlags Or DSBCAPS_CTRLPOSITIONNOTIFY
If Sound(Index).DSCaps.Frequency = True Then .lFlags = .lFlags Or DSBCAPS_CTRLFREQUENCY
If Sound(Index).DSCaps.Pan = True Then .lFlags = .lFlags Or DSBCAPS_CTRLPAN
If Sound(Index).DSCaps.Volume = True Then .lFlags = .lFlags Or DSBCAPS_CTRLVOLUME
End With

'Set the Wave Format


With DSFormat
.nFormatTag = WAVE_FORMAT_PCM
.nChannels = NumChannels
.lSamplesPerSec = SamplesPerSecond
.nBitsPerSample = BitsPerSample
.nBlockAlign = .nBitsPerSample / 8 * .nChannels
.lAvgBytesPerSec = .lSamplesPerSec * .nBlockAlign
End With

'Load the sound into the buffer


If Sound(Index).DSFile = True Then 'If it's in a file...
Set Sound(Index).DSBuffer = ds.CreateSoundBufferFromFile(App.Path & DataLocation & Sound(In
DSound - 3

dex).DSSourceName, DSBufferDescription, DSFormat)


ElseIf Sound(Index).DSResource = True Then 'If it's in a resource...
Set Sound(Index).DSBuffer = ds.CreateSoundBufferFromResource("", Sound(Index).DSSourceName,
DSBufferDescription, DSFormat)
End If

'If the sound is to be deleted after it plays, we must create an event for it
If Sound(Index).DSCaps.Delete = True Then
Sound(Index).DSNotification = dx.CreateEvent(FormObject) 'Make the event (has to be
created in a Form Object) and get its handle
DSPosition(0).hEventNotify = Sound(Index).DSNotification 'Place this event handle in
an DSBPOSITIONNOTIFY variable
DSPosition(0).lOffset = DSBPN_OFFSETSTOP 'Define the position within
the wave file at which you would like the event to be triggered
Sound(Index).DSBuffer.SetNotificationPositions 1, DSPosition() 'Set the "notification posi
tion" by passing the DSBPOSITIONNOTIFY variable
End If

End Function

Public Sub RemoveSound(Index As Integer)

'Destroy the event associated with the ending of this sound, if there was one
If Sound(Index).DSCaps.Delete = True And Sound(Index).DSNotification <> 0 Then dx.DestroyEvent
Sound(Index).DSNotification

'Reset all the variables in the sound array


With Sound(Index)
Set .DSBuffer = Nothing
.DSCaps.Delete = False
.DSCaps.Frequency = False
.DSCaps.Loop = False
.DSCaps.Pan = False
.DSCaps.Volume = False
.DSEmpty = True
.DSFile = False
.DSNotification = 0
.DSResource = False
.DSSourceName = ""
.DSState = "empty"
End With

End Sub

Public Sub PlaySound(Index As Integer)

'Check to make sure there is a sound loaded in the specified buffer


If Sound(Index).DSEmpty Then Exit Sub

'If the sound is not "paused" then reset it's position to the beginning
If Sound(Index).DSState <> "paused" Then Sound(Index).DSBuffer.SetCurrentPosition 0

'Play looped or singly, as appropriate


If Sound(Index).DSCaps.Loop = False Then Sound(Index).DSBuffer.Play DSBPLAY_DEFAULT
If Sound(Index).DSCaps.Loop = True Then Sound(Index).DSBuffer.Play DSBPLAY_LOOPING

'Set the state to "playing"


Sound(Index).DSState = "playing"

End Sub

Public Sub StopSound(Index As Integer)

'Check to make sure there is a sound loaded in the specified buffer


If Sound(Index).DSEmpty Then Exit Sub

'Stop the buffer and reset to the beginning


Sound(Index).DSBuffer.Stop
Sound(Index).DSBuffer.SetCurrentPosition 0
Sound(Index).DSState = "stopped"

End Sub

Public Sub PauseSound(Index As Integer)

'Check to make sure there is a sound loaded in the specified buffer


If Sound(Index).DSEmpty Then Exit Sub
DSound - 4

'Stop the buffer


Sound(Index).DSBuffer.Stop
Sound(Index).DSState = "paused"

End Sub

Public Sub SetFrequency(Index As Integer, Freq As Long)

'Check to make sure there is a sound loaded in the specified buffer


If Sound(Index).DSEmpty Then Exit Sub

'Check to make sure that the buffer has the capability of altering its frequency
If Sound(Index).DSCaps.Frequency = False Then Exit Sub

'Alter the frequency according to the Freq provided


Sound(Index).DSBuffer.SetFrequency Freq

End Sub

Public Sub SetVolume(Index As Integer, Vol As Long)

'Check to make sure there is a sound loaded in the specified buffer


If Sound(Index).DSEmpty Then Exit Sub

'Check to make sure that the buffer has the capability of altering its volume
If Sound(Index).DSCaps.Volume = False Then Exit Sub

'Alter the volume according to the Vol provided


Sound(Index).DSBuffer.SetVolume Vol

End Sub

Public Sub SetPan(Index As Integer, Pan As Long)

'Check to make sure there is a sound loaded in the specified buffer


If Sound(Index).DSEmpty Then Exit Sub

'Check to make sure that the buffer has the capability of altering its pan
If Sound(Index).DSCaps.Pan = False Then Exit Sub

'Alter the pan according to the Pan provided


Sound(Index).DSBuffer.SetPan Pan

End Sub

Public Function GetFrequency(Index As Integer) As Long

'Check to make sure there is a sound loaded in the specified buffer


If Sound(Index).DSEmpty Then Exit Function

'Check to make sure that the buffer has the capability of altering its frequency
If Sound(Index).DSCaps.Frequency = False Then Exit Function

'Return the frequency value


GetFrequency = Sound(Index).DSBuffer.GetFrequency()

End Function

Public Function GetVolume(Index As Integer) As Long

'Check to make sure there is a sound loaded in the specified buffer


If Sound(Index).DSEmpty Then Exit Function

'Check to make sure that the buffer has the capability of altering its volume
If Sound(Index).DSCaps.Volume = False Then Exit Function

'Return the volume value


GetVolume = Sound(Index).DSBuffer.GetVolume()

End Function

Public Function GetPan(Index As Integer) As Long

'Check to make sure there is a sound loaded in the specified buffer


If Sound(Index).DSEmpty Then Exit Function
DSound - 5

'Check to make sure that the buffer has the capability of altering its pan
If Sound(Index).DSCaps.Pan = False Then Exit Function

'Return the pan value


GetPan = Sound(Index).DSBuffer.GetPan()

End Function

Public Function GetState(Index As Integer) As String

'Returns the current state of the given sound


GetState = Sound(Index).DSState

End Function

Public Function DXCallback(ByVal eventid As Long) As Integer

Dim i As Integer

'Find the sound that caused this event to be triggered


For i = 0 To UBound(Sound)
If Sound(i).DSNotification = eventid Then
Exit For
End If
Next

'Return the ID
DXCallback = i

End Function

Public Sub Terminate()

Dim i As Integer

'Delete all of the sounds created


For i = 0 To UBound(Sound)
RemoveSound i
Next

End Sub
EntryScreen - 1

Dim BackGround As DirectDrawSurface7 'Contains the "BackGround" bitmap


Dim DogJump(4) As DirectDrawSurface7 'Contains the "DogJump" bitmaps
Dim Shovel(11) As DirectDrawSurface7 'Contains the "Shovel" bitmaps
Dim Title(10) As DirectDrawSurface7 'Contains the "Title" bitmaps

Global EntryRunning As Boolean 'Determines if the entry screen is running or not


Global EntryInitialized As Boolean 'Determines if the Screen has been initialized

'Sprite Size Constants


Const BackgroundWidth = 800
Const BackgroundHeight = 600
Const DogJumpWidth = 150
Const DogJumpHeight = 250
Dim ShovelWidth(11) As Integer 'Will contain the constant widths of all 12 shovels
Dim ShovelHeight(11) As Integer 'Will contain the constant heights of all 12 shovels
Dim TitleWidth(10) As Integer 'Will contain the constant widths of all 11 title bitmaps
Dim TitleHeight(10) As Integer 'Will contain the constant heights of all 11 title bitmaps

'Sprite Location/Condition Constants


Const DogJumpX = 325
Const DogJumpY = 200
Const ShovelSpeed = 10
Const TimeBeforeTitleBmp = 32
Const DurationOfTitleScreen = -40

'Sprite Location/Condition Variables


Dim DogJumpAnim As Integer 'Which frame of DogJump animation are we at?
Dim ShovelX(11) 'X Coordinates of the shovels
Dim ShovelY(11) 'Y Coordinates of the shovels
Dim TitleDelay As Integer 'Delay before the title bitmaps show up
Dim TitleAnim As Integer 'Which frame of the title bitmap are we showing?

Public Sub Initialize()

'Initialize the variables


EntryRunning = True
DogJumpAnim = 0
ShovelWidth(0) = 100
ShovelHeight(0) = 300
ShovelWidth(1) = 237
ShovelHeight(1) = 310
ShovelWidth(2) = 310
ShovelHeight(2) = 237
ShovelWidth(3) = 300
ShovelHeight(3) = 100
ShovelWidth(4) = 310
ShovelHeight(4) = 237
ShovelWidth(5) = 237
ShovelHeight(5) = 310
ShovelWidth(6) = 100
ShovelHeight(6) = 300
ShovelWidth(7) = 237
ShovelHeight(7) = 310
ShovelWidth(8) = 310
ShovelHeight(8) = 237
ShovelWidth(9) = 300
ShovelHeight(9) = 100
ShovelWidth(10) = 310
ShovelHeight(10) = 237
ShovelWidth(11) = 237
ShovelHeight(11) = 310
ShovelX(0) = 350
ShovelY(0) = -400
ShovelX(1) = 804
ShovelY(1) = -400
ShovelX(2) = 890
ShovelY(2) = 17
ShovelX(3) = 900
ShovelY(3) = 250
ShovelX(4) = 890
ShovelY(4) = 346
ShovelX(5) = 804
ShovelY(5) = 690
ShovelX(6) = 350
ShovelY(6) = 700
ShovelX(7) = -241
ShovelY(7) = 690
EntryScreen - 2

ShovelX(8) = -400
ShovelY(8) = 349
ShovelX(9) = -400
ShovelY(9) = 250
ShovelX(10) = -400
ShovelY(10) = 17
ShovelX(11) = -241
ShovelY(11) = -400
TitleWidth(0) = 40
TitleHeight(0) = 30
TitleWidth(1) = 80
TitleHeight(1) = 60
TitleWidth(2) = 100
TitleHeight(2) = 75
TitleWidth(3) = 200
TitleHeight(3) = 150
TitleWidth(4) = 240
TitleHeight(4) = 180
TitleWidth(5) = 280
TitleHeight(5) = 210
TitleWidth(6) = 300
TitleHeight(6) = 225
TitleWidth(7) = 400
TitleHeight(7) = 300
TitleWidth(8) = 600
TitleHeight(8) = 450
TitleWidth(9) = 640
TitleHeight(9) = 480
TitleWidth(10) = 720
TitleHeight(10) = 540
TitleDelay = TimeBeforeTitleBmp
TitleAnim = -1
EntryInitialized = True

'Turn the mouse off


Cursor.CursorToggle

'Load the sprites


LoadSurfaces

'Play the intro sound


DSound.PlaySound SoundID(SndIntro)

End Sub

Private Sub LoadSurfaces()

Dim i As Integer
Dim CKey As DDCOLORKEY
Dim ddsdNewSprite As DDSURFACEDESC2

'This routine loads all of the surfaces we're going to be using

'Set up those components of the surface description that will stay the same
ddsdNewSprite.lFlags = DDSD_CAPS Or DDSD_WIDTH Or DDSD_HEIGHT
ddsdNewSprite.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN

'Set up those components of the colour key that will stay the same
CKey.low = 0
CKey.high = 0

'Load the background


ddsdNewSprite.lWidth = BackgroundWidth
ddsdNewSprite.lHeight = BackgroundHeight
Set BackGround = DDraw.LoadSurface(App.Path & "\data\background_entry.bmp", ddsdNewSprite)

'Load the "DogJump" bitmaps


ddsdNewSprite.lWidth = DogJumpWidth
ddsdNewSprite.lHeight = DogJumpHeight
For i = 0 To 4
Set DogJump(i) = DDraw.LoadSurface(App.Path & "\data\dogjump" & i + 1 & ".bmp", ddsdNewSpri
te)
DogJump(i).SetColorKey DDCKEY_SRCBLT, CKey
Next

'Load the "Shovel" bitmaps


For i = 0 To 11
EntryScreen - 3

ddsdNewSprite.lWidth = ShovelWidth(i)
ddsdNewSprite.lHeight = ShovelHeight(i)
Set Shovel(i) = DDraw.LoadSurface(App.Path & "\data\shovel" & i + 1 & ".bmp", ddsdNewSprite
)
Shovel(i).SetColorKey DDCKEY_SRCBLT, CKey
Next

'Load the "Title" bitmaps


For i = 0 To 10
ddsdNewSprite.lWidth = TitleWidth(i)
ddsdNewSprite.lHeight = TitleHeight(i)
Set Title(i) = DDraw.LoadSurface(App.Path & "\data\title" & i + 1 & ".bmp", ddsdNewSprite)
Title(i).SetColorKey DDCKEY_SRCBLT, CKey
Next

End Sub

Public Sub Display()

Dim i As Integer
Dim SrcRect As RECT
Dim DestRect As RECT

'Fill the backbuffer with the background bitmap


With SrcRect
.Bottom = BackgroundHeight
.Left = 0
.Right = BackgroundWidth
.Top = 0
End With
DDraw.DisplaySprite DestRect, BackGround, SrcRect, True, 0, 0

'Display the dog


With SrcRect
.Bottom = DogJumpHeight
.Left = 0
.Right = DogJumpWidth
.Top = 0
End With
With DestRect
.Bottom = DogJumpHeight + DogJumpY
.Left = DogJumpX
.Right = DogJumpWidth + DogJumpX
.Top = DogJumpY
End With
DDraw.DisplaySprite DestRect, DogJump(DogJumpAnim), SrcRect

'Display the shovels


For i = 0 To 11
With SrcRect
.Bottom = ShovelHeight(i)
.Left = 0
.Right = ShovelWidth(i)
.Top = 0
End With
With DestRect
.Bottom = ShovelY(i) + ShovelHeight(i)
.Left = ShovelX(i)
.Right = ShovelX(i) + ShovelWidth(i)
.Top = ShovelY(i)
End With
DDraw.DisplaySprite DestRect, Shovel(i), SrcRect
Next

'Display the title bitmap


If TitleAnim >= 0 Then
With SrcRect
.Bottom = TitleHeight(TitleAnim)
.Left = 0
.Right = TitleWidth(TitleAnim)
.Top = 0
End With
With DestRect
.Bottom = 300 + TitleHeight(TitleAnim) / 2
.Left = 400 - TitleWidth(TitleAnim) / 2
.Right = 400 + TitleWidth(TitleAnim) / 2
.Top = 300 - TitleHeight(TitleAnim) / 2
EntryScreen - 4

End With
DDraw.DisplaySprite DestRect, Title(TitleAnim), SrcRect
End If

End Sub

Public Sub Physics()

Dim i As Integer

'Animate the jumping dog


If DogJumpAnim = 4 Then
DogJumpAnim = 0
Else
DogJumpAnim = DogJumpAnim + 1
End If

'Move the shovels inward


For i = 0 To 11
If Sqr((ShovelX(i) + ShovelWidth(i) / 2 - 400) ^ 2 + (ShovelY(i) + ShovelHeight(i) / 2 - 30
0) ^ 2) > 220 Then
If ShovelX(i) + ShovelWidth(i) / 2 > 550 Then
ShovelX(i) = ShovelX(i) - ShovelSpeed
ElseIf ShovelX(i) + ShovelWidth(i) / 2 < 250 Then
ShovelX(i) = ShovelX(i) + ShovelSpeed
End If
If ShovelY(i) + ShovelHeight(i) / 2 > 450 Then
ShovelY(i) = ShovelY(i) - ShovelSpeed
ElseIf ShovelY(i) + ShovelHeight(i) / 2 < 150 Then
ShovelY(i) = ShovelY(i) + ShovelSpeed
End If
End If
Next

'Handle title animation


TitleDelay = TitleDelay - 1
If TitleDelay <= 0 And TitleAnim < UBound(Title) Then TitleAnim = TitleAnim + 1

'End the entry scene after the appropriate time


If TitleDelay <= DurationOfTitleScreen Then
EntryRunning = False
InfoRunning = True
End If

End Sub

Public Sub Terminate()

Dim i As Integer

'Reset the running variable


EntryRunning = False
EntryInitialized = False

'Destroy the objects


Set BackGround = Nothing
For i = 0 To 4
Set DogJump(i) = Nothing
Next
For i = 0 To 11
Set Shovel(i) = Nothing
Next
For i = 0 To 9
Set Title(i) = Nothing
Next

End Sub
GameScreen - 1

Dim Man(7) As DirectDrawSurface7 'Contains the "Man" bitmaps


Dim ManHit As DirectDrawSurface7 'Contains the "Man Hit" bitmap
Dim Boy(2) As DirectDrawSurface7 'Contains the "Boy" bitmaps
Dim BoyFire(2) As DirectDrawSurface7 'Contains the "Boy Firing" bitmaps
Dim Dog(4) As DirectDrawSurface7 'Contains the "Dog" bitmaps
Dim Rock As DirectDrawSurface7 'Contains the "Rock" bitmap
Dim BackGround As DirectDrawSurface7 'Contains the "BackGround" bitmap
Dim Button(2) As DirectDrawSurface7 'Contains the "Button" bitmaps
Dim Pause As DirectDrawSurface7 'Contains the "Pause" bitmap
Dim ManWin(9) As DirectDrawSurface7 'Contains the "ManWin" bitmaps
Dim ManFall(4) As DirectDrawSurface7 'Contains the "ManFall" bitmaps
Dim BoyHappy(9) As DirectDrawSurface7 'Contains the "BoyHappy" bitmaps
Dim BoySad(4) As DirectDrawSurface7 'Contains the "BoySad" bitmaps
Dim YouWin As DirectDrawSurface7 'Contains the "YouWin" bitmap
Dim YouLose As DirectDrawSurface7 'Contains the "YouLose" bitmap

Global GameRunning As Boolean 'Determines if the game portion of the program should still
be running
Global GameInitialized As Boolean 'Determines if the Screen has been initialized

'Sprite Size Constants


Const BackgroundWidth = 800
Const BackgroundHeight = 600
Const ManWidth = 302
Const ManHeight = 328
Const BoyWidth = 195
Const BoyHeight = 203
Const DogWidth = 72
Const DogHeight = 64
Const RockWidth = 10
Const RockHeight = 10
Const ButtonWidth = 200
Const ButtonHeight = 200
Const PauseWidth = 800
Const PauseHeight = 600
Const ManWinWidth = 600
Const ManWinHeight = 340
Const ManFallWidth = 302
Const ManFallHeight = 328
Const BoyHappyWidth = 250
Const BoyHappyHeight = 203
Const BoySadWidth = 250
Const BoySadHeight = 203
Const YouWinWidth = 800
Const YouWinHeight = 600
Const YouLoseWidth = 800
Const YouLoseHeight = 600

'Sprite Location Constants


Const BaseLine = 480 'Where's the baseline for sprites on the background bitmap?
Const DogYapFreq = 200 'How many frames must pass before the do yaps?
Const ManY = 125 'What is the man's Y coordinate?
Const BoyX = 560 'What is the boy's X coordinate?
Const BoyY = 260 'What is the boy's Y coordinate?
Const DogX = 510 'What is the dog's X coordinate?
Const DogY = 390 'What is the dog's Y coordinate?
Const Gravity = 10 'How strong is the gravity on the rock
Const RockStart1X = 600 'Rock's starting X-coordinates
Const RockStart1Y = 315 'Rock's starting Y-coordinates
Const RockStart2X = 600 'Rock's starting X-coordinates
Const RockStart2Y = 300 'Rock's starting Y-coordinates
Const RockStart3X = 605 'Rock's starting X-coordinates
Const RockStart3Y = 280 'Rock's starting Y-coordinates

'Sprite Animation/Physics Constants


Const ManSpeed = 1 'How many frames does it take for the man to animate?
Const ManMoveSpeed = 1 'How many frames does it take for the man to move?
Const ManMoveStop = 280 'How far can the man move before he has to stop?
Const ManSwingStart = 250 'How far along will the man start to swing?
Const DogSpeed = 1 'How many frames does it take for the dog to animate?
Const DogYapSpeed = 24 'How many frames does it take for the dog to yap?
Const Grav = 1 'How strong is gravity?
Const FinalAnimationSpeed = 2 'How fast do we display the final animation sequence?

'Sprite Location/Condition Variables


Dim GameWon As Boolean 'Has the game been won?
Dim GameWonTime As Integer 'How much time has passed since game was won?
GameScreen - 2

Dim GameLost As Boolean 'Has the game been lost?


Dim GameLostTime As Integer 'How much time has passed since game was lost?
Dim IsManHit As Boolean 'Is the man currently hit?
Dim ManHitLength As Integer 'How long should the "manhit" bitmap be displayed?
Dim ManAnim As Integer 'Which frame of man animation are we at?
Dim ManX As Integer 'What is the man's X coordinate?
Dim BoyAnim As Integer 'Which frame of boy animation are we at?
Dim IsBoyFire As Boolean 'Is the boy firing or not?
Dim BoyFireLength As Integer 'How long should the boy be firing?
Dim DogAnim As Integer 'Which Frame of dog animation are we at?
Dim IsDogYap As Boolean 'How long has it been since the last yap?
Dim DogHits As Integer 'How many times has the dog been hit?
Dim IsDogHit As Boolean 'Is the dog hit?
Dim DogHitLength As Integer 'How long should the dog be hit for?
Dim IsDogDead As Boolean 'Is the dog dead?
Dim IsButtonDepressed(2) As Boolean 'Are any of the buttons pressed?
Dim IsPaused As Boolean 'Is the game paused?
Dim FinalAnimationRate As Integer 'How long have we been waiting since the last anim screen?
Dim SlingHold As Integer 'How long did the boy hold the slingshot?
Private Type RockType 'A special type to contain rock data
xCoord As Integer 'Rock's x coord
yCoord As Integer 'Rock's y coord
xSpeed As Integer 'Rocks horizontal speed
ySpeed As Integer 'Rocks vertical speed
Exists As Boolean
End Type
Dim Rocks() As RockType

Public Sub Initialize()

'Initialize the variables


GameRunning = True
GameWon = False
GameLost = False
GameWonTime = 0
GameLostTime = 0
ReDim Rocks(0)
Rocks(0).Exists = False
SlingHold = 0
IsButtonDepressed(0) = False
IsButtonDepressed(1) = False
IsButtonDepressed(2) = False
IsManHit = False
ManHitLength = 0
ManAnim = 4
ManX = 60
BoyAnim = 0
IsBoyFire = False
BoyFireLength = 0
DogAnim = 0
IsDogYap = False
DogHits = 0
IsDogHit = False
DogHitLength = 0
IsDogDead = False
GameInitialized = True
IsPaused = False

'Load the sprites


LoadSurfaces

End Sub

Private Sub LoadSurfaces()

Dim i As Integer
Dim CKey As DDCOLORKEY
Dim ddsdNewSprite As DDSURFACEDESC2

'This routine loads all of the surfaces we're going to be using

'Set up those components of the surface description that will stay the same
ddsdNewSprite.lFlags = DDSD_CAPS Or DDSD_WIDTH Or DDSD_HEIGHT
ddsdNewSprite.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN

'Set up those components of the colour key that will stay the same
CKey.low = 0
GameScreen - 3

CKey.high = 0

'Load the background


ddsdNewSprite.lWidth = BackgroundWidth
ddsdNewSprite.lHeight = BackgroundHeight
Set BackGround = DDraw.LoadSurface(App.Path & "\data\background_game.bmp", ddsdNewSprite)

'Load the "Man" bitmaps


ddsdNewSprite.lWidth = ManWidth
ddsdNewSprite.lHeight = ManHeight
For i = 0 To 7
Set Man(i) = DDraw.LoadSurface(App.Path & "\data\man" & i + 1 & ".bmp", ddsdNewSprite)
Man(i).SetColorKey DDCKEY_SRCBLT, CKey
Next
Set ManHit = DDraw.LoadSurface(App.Path & "\data\manhit.bmp", ddsdNewSprite)
ManHit.SetColorKey DDCKEY_SRCBLT, CKey

'Load the "Boy" bitmaps


ddsdNewSprite.lWidth = BoyWidth
ddsdNewSprite.lHeight = BoyHeight
Set Boy(0) = DDraw.LoadSurface(App.Path & "\data\boyaim1.bmp", ddsdNewSprite)
Boy(0).SetColorKey DDCKEY_SRCBLT, CKey
Set Boy(1) = DDraw.LoadSurface(App.Path & "\data\boyaim3.bmp", ddsdNewSprite)
Boy(1).SetColorKey DDCKEY_SRCBLT, CKey
Set Boy(2) = DDraw.LoadSurface(App.Path & "\data\boyaim5.bmp", ddsdNewSprite)
Boy(2).SetColorKey DDCKEY_SRCBLT, CKey
Set BoyFire(0) = DDraw.LoadSurface(App.Path & "\data\boyfire1.bmp", ddsdNewSprite)
BoyFire(0).SetColorKey DDCKEY_SRCBLT, CKey
Set BoyFire(1) = DDraw.LoadSurface(App.Path & "\data\boyfire3.bmp", ddsdNewSprite)
BoyFire(1).SetColorKey DDCKEY_SRCBLT, CKey
Set BoyFire(2) = DDraw.LoadSurface(App.Path & "\data\boyfire5.bmp", ddsdNewSprite)
BoyFire(2).SetColorKey DDCKEY_SRCBLT, CKey

'Load the "Dog" bitmaps


ddsdNewSprite.lWidth = DogWidth
ddsdNewSprite.lHeight = DogHeight
Set Dog(0) = DDraw.LoadSurface(App.Path & "\data\dog1.bmp", ddsdNewSprite)
Dog(0).SetColorKey DDCKEY_SRCBLT, CKey
Set Dog(1) = DDraw.LoadSurface(App.Path & "\data\dog2.bmp", ddsdNewSprite)
Dog(1).SetColorKey DDCKEY_SRCBLT, CKey
Set Dog(2) = DDraw.LoadSurface(App.Path & "\data\dogyap.bmp", ddsdNewSprite)
Dog(2).SetColorKey DDCKEY_SRCBLT, CKey
Set Dog(3) = DDraw.LoadSurface(App.Path & "\data\doghit.bmp", ddsdNewSprite)
Dog(3).SetColorKey DDCKEY_SRCBLT, CKey
Set Dog(4) = DDraw.LoadSurface(App.Path & "\data\dogdead.bmp", ddsdNewSprite)
Dog(4).SetColorKey DDCKEY_SRCBLT, CKey

'Load the "Rock" bitmap


ddsdNewSprite.lWidth = RockWidth
ddsdNewSprite.lHeight = RockHeight
Set Rock = DDraw.LoadSurface(App.Path & "\data\rock.bmp", ddsdNewSprite)
Rock.SetColorKey DDCKEY_SRCBLT, CKey

'Load the "Button" bitmaps


ddsdNewSprite.lWidth = ButtonWidth
ddsdNewSprite.lHeight = ButtonHeight
For i = 0 To 2
Set Button(i) = DDraw.LoadSurface(App.Path & "\data\depress_button" & i + 2 & ".bmp", ddsdN
ewSprite)
Button(i).SetColorKey DDCKEY_SRCBLT, CKey
Next

'Load the "Pause" bitmap


ddsdNewSprite.lWidth = PauseWidth
ddsdNewSprite.lHeight = PauseHeight
Set Pause = DDraw.LoadSurface(App.Path & "\data\pause.bmp", ddsdNewSprite)
Pause.SetColorKey DDCKEY_SRCBLT, CKey

'Load the "YouWin" bitmap


ddsdNewSprite.lWidth = YouWinWidth
ddsdNewSprite.lHeight = YouWinHeight
Set YouWin = DDraw.LoadSurface(App.Path & "\data\youwin.bmp", ddsdNewSprite)
YouWin.SetColorKey DDCKEY_SRCBLT, CKey

'Load the "YouLose" bitmap


ddsdNewSprite.lWidth = YouLoseWidth
ddsdNewSprite.lHeight = YouLoseHeight
GameScreen - 4

Set YouLose = DDraw.LoadSurface(App.Path & "\data\youlose.bmp", ddsdNewSprite)


YouLose.SetColorKey DDCKEY_SRCBLT, CKey

'Load the "ManWin" bitmaps


ddsdNewSprite.lWidth = ManWinWidth
ddsdNewSprite.lHeight = ManWinHeight
For i = 0 To 9
Set ManWin(i) = DDraw.LoadSurface(App.Path & "\data\manwin" & i + 1 & ".bmp", ddsdNewSprite
)
ManWin(i).SetColorKey DDCKEY_SRCBLT, CKey
Next

'Load the "ManFall" bitmaps


ddsdNewSprite.lWidth = ManFallWidth
ddsdNewSprite.lHeight = ManFallHeight
For i = 0 To 4
Set ManFall(i) = DDraw.LoadSurface(App.Path & "\data\manfall" & i + 1 & ".bmp", ddsdNewSpri
te)
ManFall(i).SetColorKey DDCKEY_SRCBLT, CKey
Next

'Load the "BoyHappy" bitmaps


ddsdNewSprite.lWidth = BoyHappyWidth
ddsdNewSprite.lHeight = BoyHappyHeight
For i = 0 To 9
Set BoyHappy(i) = DDraw.LoadSurface(App.Path & "\data\boyhappy" & i + 1 & ".bmp", ddsdNewSp
rite)
BoyHappy(i).SetColorKey DDCKEY_SRCBLT, CKey
Next

'Load the "BoySad" bitmaps


ddsdNewSprite.lWidth = BoySadWidth
ddsdNewSprite.lHeight = BoySadHeight
For i = 0 To 4
Set BoySad(i) = DDraw.LoadSurface(App.Path & "\data\boysad" & i + 1 & ".bmp", ddsdNewSprite
)
BoySad(i).SetColorKey DDCKEY_SRCBLT, CKey
Next

End Sub

Public Sub HandleKeys()

'Get the current state of the keyboard


DInput.CheckKeys

'Has the escape key been pressed?


If DInput.aKeys(DIK_ESCAPE) Then FrmDog.ExitProgram

'Determine if the player wishes to play again after winning or losing


If (GameWon Or GameLost) And DInput.aKeys(DIK_Y) Then
Terminate
Initialize
End If

'Determine if the player wishes to quit after winning or losing


If (GameWon Or GameLost) And DInput.aKeys(DIK_N) Then
FrmDog.ExitProgram
End If

'If the game has been won or lost, don't check for other keystrokes
If GameWon Or GameLost Then Exit Sub

'Check for unpausing


If DInput.aKeys(DIK_F1) Then
IsPaused = False
IsButtonDepressed(2) = False
End If

'If the game is paused, don't check other keystrokes


If IsPaused Then Exit Sub

'Move the boys arms up if user presses up


Static UpKey As Boolean
If DInput.aKeys(DIK_UP) And UpKey = False Then
UpKey = True
If BoyAnim < 2 Then BoyAnim = BoyAnim + 1
GameScreen - 5

ElseIf Not (DInput.aKeys(DIK_UP)) Then


UpKey = False
End If

'Move the boys arms down if user presses up


Static DownKey As Boolean
If DInput.aKeys(DIK_DOWN) And DownKey = False Then
DownKey = True
If BoyAnim > 0 Then BoyAnim = BoyAnim - 1
ElseIf Not (DInput.aKeys(DIK_DOWN)) Then
DownKey = False
End If

'Determine how fast to shoot the bullet (spacebar)


Static SpaceKey As Boolean
If DInput.aKeys(DIK_SPACE) And SpaceKey = False Then
SpaceKey = True
ElseIf Not (DInput.aKeys(DIK_SPACE)) And SpaceKey = True Then
SpaceKey = False
FireRock
End If
If SpaceKey = True Then SlingHold = SlingHold + 1

End Sub

Public Sub Physics()

Dim i As Integer

'Animate and step the physics functions

'If the game is won or lost then skip the physics


If GameWon Or GameLost Then Exit Sub

'If the game is paused, skip the physics


If IsPaused Then Exit Sub

'Animate the man


Static ManRate As Integer
If ManRate >= ManSpeed And ManX >= ManSwingStart Then
ManRate = 0
If ManAnim < 7 Then ManAnim = ManAnim + 1
If ManAnim = 7 Then ManAnim = 0
Else
ManRate = ManRate + 1
End If

'Move the man


Static ManMoveRate As Integer
If ManMoveRate = ManMoveSpeed Then
ManMoveRate = 0
If ManX <= ManMoveStop - 18 Then ManX = ManX + 18
If ManX > ManMoveStop - 18 Then ManX = ManMoveStop
Else
ManMoveRate = ManMoveRate + 1
End If

'Animate the dog


Static DogRate As Integer
If DogRate = DogSpeed Then
DogRate = 0
IsDogYap = False
If DogAnim = 0 Then
DogAnim = 1
Else
DogAnim = 0
End If
Else
DogRate = DogRate + 1
End If

'Is the dog yapping?


Static DogYapRate As Integer
If DogYapRate = DogYapSpeed Then
DogYapRate = 0
IsDogYap = True
If IsDogDead = False And IsDogHit = False Then DSound.PlaySound SoundID(SndDogYap)
GameScreen - 6

Else
DogYapRate = DogYapRate + 1
End If

'Has the dog been hit?


If ManX >= ManMoveStop And ManAnim = 0 Then
DSound.PlaySound SoundID(SndDogHit)
DogHitLength = 0
DogHits = DogHits + 1
IsDogHit = True
End If

'Is the dog dead?


If ManX >= ManMoveStop And ManAnim = 0 And DogHits >= 6 Then
DSound.PlaySound SoundID(SndDogDead)
GameLost = True
GameLostTime = 0
IsDogDead = True
End If

'Is the man over the edge?


If ManX <= 0 Then
GameWon = True
GameWonTime = 0
End If

'Animate the rocks, remove appropriate ones


For i = 0 To UBound(Rocks)
If Rocks(i).Exists Then
Rocks(i).xCoord = Rocks(i).xCoord - Rocks(i).xSpeed
Rocks(i).ySpeed = Rocks(i).ySpeed - Grav
Rocks(i).yCoord = Rocks(i).yCoord - Rocks(i).ySpeed
'Check if its gone off screen
If Rocks(i).yCoord >= 575 Then RemoveRock i
If Rocks(i).xCoord <= 25 Then RemoveRock i
'Check for collisions
If Rocks(i).xCoord <= ManX + ManWidth * 6 / 10 And Rocks(i).yCoord <= ManY + ManHeight
And Rocks(i).yCoord >= ManY Then
RemoveRock i
DSound.PlaySound SoundID(SndManHit)
IsManHit = True
ManHitLength = 0
ManX = ManX - 80
End If
End If
Next

End Sub

Public Sub Display()

Dim SrcRect As RECT


Dim DestRect As RECT

'Fill the backbuffer with the background bitmap


With SrcRect
.Bottom = BackgroundHeight
.Left = 0
.Right = BackgroundWidth
.Top = 0
End With
DDraw.DisplaySprite DestRect, BackGround, SrcRect, True, 0, 0

'Display the boy


With SrcRect
.Bottom = BoyHeight
.Left = 0
.Right = BoyWidth
.Top = 0
End With
With DestRect
.Bottom = BoyHeight + BoyY
.Left = BoyX
.Right = BoyWidth + BoyX
.Top = BoyY
End With
If Not (GameLost Or GameWon) Then
GameScreen - 7

If IsBoyFire = True Then


If BoyFireLength < 3 Then
BoyFireLength = BoyFireLength + 1
DDraw.DisplaySprite DestRect, BoyFire(BoyAnim), SrcRect
Else
IsBoyFire = False
End If
End If
If IsBoyFire = False Then DDraw.DisplaySprite DestRect, Boy(BoyAnim), SrcRect
End If

'Display the dog


With SrcRect
.Bottom = DogHeight
.Left = 0
.Right = DogWidth
.Top = 0
End With
With DestRect
.Bottom = DogHeight + DogY
.Left = DogX
.Right = DogWidth + DogX
.Top = DogY
End With
If Not (GameLost) Then
If IsDogHit = True And IsDogDead = False Then
If DogHitLength < 3 Then
DogHitLength = DogHitLength + 1
DDraw.DisplaySprite DestRect, Dog(3), SrcRect
Else
IsDogHit = False
End If
End If
If IsDogYap = False And IsDogHit = False And IsDogDead = False Then DDraw.DisplaySprite Des
tRect, Dog(DogAnim), SrcRect
If IsDogYap And IsDogHit = False And IsDogDead = False Then DDraw.DisplaySprite DestRect, D
og(2), SrcRect
If IsDogDead Then DDraw.DisplaySprite DestRect, Dog(4), SrcRect
End If

'Display the man


With SrcRect
.Bottom = ManHeight
.Left = 0
.Right = ManWidth
.Top = 0
End With
With DestRect
.Bottom = ManHeight + ManY
.Left = ManX
.Right = ManWidth + ManX
.Top = ManY
End With
If Not (GameWon Or GameLost) Then
If IsManHit = False And ManX >= ManSwingStart Then
DDraw.DisplaySprite DestRect, Man(ManAnim), SrcRect
ElseIf IsManHit = False Then
DDraw.DisplaySprite DestRect, Man(4), SrcRect
End If
If IsManHit = True And ManHitLength < 3 Then
ManHitLength = ManHitLength + 1
DDraw.DisplaySprite DestRect, ManHit, SrcRect
Else
IsManHit = False
End If
End If

'Display the buttons if depressed


With SrcRect
.Bottom = ButtonHeight
.Left = 0
.Right = ButtonWidth
.Top = 0
End With
With DestRect
.Bottom = 600
.Left = 0
GameScreen - 8

.Right = 200
.Top = 400
End With
'If the left-hand button is depressed, display the bitmap and exit program
If IsButtonDepressed(0) Then
DDraw.DisplaySprite DestRect, Button(0), SrcRect
DSound.PlaySound SoundID(SndClick) 'Play the click sound
DDraw.Flip 'Ensure that the depressed button can be se
en
DoEvents 'Stall a bit
FrmDog.ExitProgram 'Exit the program
End If
With DestRect
.Bottom = 600
.Left = 600
.Right = 800
.Top = 400
End With
'If the reset button is pressed, start the game over
If IsButtonDepressed(1) Then
DDraw.DisplaySprite DestRect, Button(1), SrcRect
DSound.PlaySound SoundID(SndClick) 'Play the click sound
GameRunning = False 'Ensure the game screen is unloaded
EntryRunning = True 'Show the first screen again
End If
'If the pause button is pressed, set the pause variable
If IsButtonDepressed(2) Then
IsPaused = True
DDraw.DisplaySprite DestRect, Button(2), SrcRect
End If

'Display the pause screen


If IsPaused Then
With SrcRect
.Bottom = 600
.Left = 0
.Right = 800
.Top = 0
End With
With DestRect
.Bottom = 600
.Left = 0
.Right = 800
.Top = 0
End With
DDraw.DisplaySprite DestRect, Pause, SrcRect
End If

'Display the rocks


With SrcRect
.Bottom = RockHeight
.Left = 0
.Right = RockWidth
.Top = 0
End With
For i = 0 To UBound(Rocks)
If Rocks(i).Exists = True Then
With DestRect
.Bottom = Rocks(i).yCoord + RockHeight
.Left = Rocks(i).xCoord
.Right = Rocks(i).xCoord + RockWidth
.Top = Rocks(i).yCoord
End With
DDraw.DisplaySprite DestRect, Rock, SrcRect
End If
Next

'If the game is lost or won, display the appropriate animations


If GameLost Then GameLostDisplay
If GameWon Then GameWonDisplay

End Sub

Public Sub Click()

'If the game is paused, lost, or won, ignore the mouse


If GameWon Or GameLost Or IsPaused Then Exit Sub
GameScreen - 9

'Check to see if the mouse is over a button


If Sqr((Cursor.xPos - 95) ^ 2 + (Cursor.yPos - 517) ^ 2) < 20 Then IsButtonDepressed(0) = True
If Sqr((Cursor.xPos - 699) ^ 2 + (Cursor.yPos - 505) ^ 2) < 15 Then IsButtonDepressed(1) = True
If Sqr((Cursor.xPos - 712) ^ 2 + (Cursor.yPos - 525) ^ 2) < 15 Then
IsButtonDepressed(2) = True
DSound.PlaySound SoundID(SndClick) 'Play the click sound
End If

End Sub

Private Sub FireRock()

Dim i As Integer
Dim FreeRock As Integer

'This routine creates a new rock and set it in motion

'Set the boy anim to "fire"


IsBoyFire = True
BoyFireLength = 0

'Play the rock sound


DSound.PlaySound SoundID(SndBoyShoot)

'Check if there's a free spot in the rocks array


FreeRock = -1
For i = 0 To UBound(Rocks)
If Rocks(i).Exists = False Then
FreeRock = i
Exit For
End If
Next

'If there's no free spot, then make a new one


If FreeRock = -1 Then
ReDim Preserve Rocks(UBound(Rocks) + 1)
FreeRock = UBound(Rocks)
End If

'Set this rock as "existing"


Rocks(FreeRock).Exists = True

'Set the rock's horizontal speed


Rocks(FreeRock).xSpeed = SlingHold * 5

'Set the rock's vertical speed


If BoyAnim = 0 Then Rocks(FreeRock).ySpeed = 0
If BoyAnim = 1 Then Rocks(FreeRock).ySpeed = 5
If BoyAnim = 2 Then Rocks(FreeRock).ySpeed = 10

'Set the rock's initial X and Y coords


If BoyAnim = 0 Then
Rocks(FreeRock).xCoord = RockStart1X
Rocks(FreeRock).yCoord = RockStart1Y
ElseIf BoyAnim = 1 Then
Rocks(FreeRock).xCoord = RockStart2X
Rocks(FreeRock).yCoord = RockStart2Y
ElseIf BoyAnim = 2 Then
Rocks(FreeRock).xCoord = RockStart3X
Rocks(FreeRock).yCoord = RockStart3Y
End If

'Reset the sling strength value


SlingHold = 0

End Sub

Private Sub RemoveRock(Index As Integer)

'Remove a rock from the rock array


Rocks(Index).Exists = False

End Sub

Private Sub GameLostDisplay()


GameScreen - 10

Dim DestRect As RECT


Dim SrcRect As RECT

GameLostTime = GameLostTime + 1

'Display the man animation


With SrcRect
.Bottom = ManWinHeight
.Left = 0
.Right = ManWinWidth
.Top = 0
End With
With DestRect
.Bottom = ManY + ManWinHeight
.Left = ManX - 300
.Right = ManX - 300 + ManWinWidth
.Top = ManY
End With
If GameLostTime < 10 * FinalAnimationSpeed Then
DDraw.DisplaySprite DestRect, ManWin(GameLostTime \ FinalAnimationSpeed), SrcRect
Else
DDraw.DisplaySprite DestRect, ManWin(9), SrcRect
End If

'Display the boy animation


With SrcRect
.Bottom = BoySadHeight
.Left = 0
.Right = BoySadWidth
.Top = 0
End With
With DestRect
.Bottom = BoyY + BoySadHeight
.Left = BoyX
.Right = BoyX + BoySadWidth
.Top = BoyY
End With
If GameLostTime < 5 * FinalAnimationSpeed Then
DDraw.DisplaySprite DestRect, BoySad(GameLostTime \ FinalAnimationSpeed), SrcRect
Else
DDraw.DisplaySprite DestRect, BoySad(4), SrcRect
End If

'Display the text box


With SrcRect
.Bottom = YouLoseHeight
.Left = 0
.Right = YouLoseWidth
.Top = 0
End With
If GameLostTime > 10 * FinalAnimationSpeed Then DDraw.DisplaySprite SrcRect, YouLose, SrcRect

End Sub

Private Sub GameWonDisplay()

Dim DestRect As RECT


Dim SrcRect As RECT

GameWonTime = GameWonTime + 1

'Display the boy animation


With SrcRect
.Bottom = BoyHappyHeight
.Left = 0
.Right = BoyHappyWidth
.Top = 0
End With
With DestRect
.Bottom = BoyY + BoyHappyHeight
.Left = BoyX
.Right = BoyX + BoyHappyWidth
.Top = BoyY
End With
If GameWonTime < 10 * FinalAnimationSpeed Then
DDraw.DisplaySprite DestRect, BoyHappy(GameWonTime \ FinalAnimationSpeed), SrcRect
Else
GameScreen - 11

DDraw.DisplaySprite DestRect, BoyHappy(9), SrcRect


End If

'Display the boy animation


With SrcRect
.Bottom = ManFallHeight
.Left = 0
.Right = ManFallWidth
.Top = 0
End With
With DestRect
.Bottom = ManY + ManFallHeight
.Left = ManX
.Right = ManX + ManFallWidth
.Top = ManY
End With
If GameWonTime < 5 Then
DDraw.DisplaySprite DestRect, ManFall(GameWonTime \ FinalAnimationSpeed), SrcRect
Else
DDraw.DisplaySprite DestRect, ManFall(4), SrcRect
End If

'Display the text box


With SrcRect
.Bottom = YouWinHeight
.Left = 0
.Right = YouWinWidth
.Top = 0
End With
If GameWonTime > 10 * FinalAnimationSpeed Then DDraw.DisplaySprite SrcRect, YouWin, SrcRect

End Sub

Public Sub Terminate()

Dim i As Integer

'Reset the running variables


GameRunning = False
GameInitialized = False

'Destroys all of the game objects


For i = 0 To 7
Set Man(i) = Nothing
Set ManHit = Nothing
Next
For i = 0 To 2
Set Boy(i) = Nothing
Set BoyFire(i) = Nothing
Next
For i = 0 To 4
Set Dog(i) = Nothing
Next
Set BackGround = Nothing
Set Rock = Nothing

End Sub
InfoScreen - 1

Dim BackGround As DirectDrawSurface7 'Contains the "BackGround" bitmap


Dim DepressButton As DirectDrawSurface7 'Contains the "Depress_Button1" bitmap

Global InfoRunning As Boolean 'Determines if the entry screen is running or not


Global InfoInitialized As Boolean 'Determines if the Screen has been initialized

'Sprite Size Constants


Const BackgroundWidth = 800
Const BackgroundHeight = 600
Const DepressButtonWidth = 200
Const DepressButtonHeight = 200

'Sprite Location Constants


Const DepressButtonX = 600
Const DepressButtonY = 400

'Sprite Location/Condition Variables


Dim IsButtonDepressed As Boolean

Public Sub Initialize()

'Redisplay the mouse cursor


Cursor.CursorToggle

'Initialize the globals


InfoRunning = True
IsButtonDepressed = False
InfoInitialized = True

'Load the sprites


LoadSurfaces

End Sub

Private Sub LoadSurfaces()

Dim i As Integer
Dim CKey As DDCOLORKEY
Dim ddsdNewSprite As DDSURFACEDESC2

'This routine loads all of the surfaces we're going to be using

'Set up those components of the surface description that will stay the same
ddsdNewSprite.lFlags = DDSD_CAPS Or DDSD_WIDTH Or DDSD_HEIGHT
ddsdNewSprite.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN

'Set up those components of the colour key that will stay the same
CKey.low = 0
CKey.high = 0

'Load the background


ddsdNewSprite.lWidth = BackgroundWidth
ddsdNewSprite.lHeight = BackgroundHeight
Set BackGround = DDraw.LoadSurface(App.Path & "\data\background_info.bmp", ddsdNewSprite)

'Load the background


ddsdNewSprite.lWidth = DepressButtonWidth
ddsdNewSprite.lHeight = DepressButtonHeight
Set DepressButton = DDraw.LoadSurface(App.Path & "\data\depress_button1.bmp", ddsdNewSprite)
DepressButton.SetColorKey DDCKEY_SRCBLT, CKey

End Sub

Public Sub Display()

Dim i As Integer
Dim SrcRect As RECT
Dim DestRect As RECT

'Fill the backbuffer with the background bitmap


With SrcRect
.Bottom = BackgroundHeight
.Left = 0
.Right = BackgroundWidth
.Top = 0
End With
DDraw.DisplaySprite DestRect, BackGround, SrcRect, True, 0, 0
InfoScreen - 2

'Display the depressed button if clicked


If IsButtonDepressed Then
With SrcRect
.Bottom = DepressButtonHeight
.Left = 0
.Right = DepressButtonWidth
.Top = 0
End With
With DestRect
.Bottom = DepressButtonY + DepressButtonHeight
.Left = DepressButtonX
.Right = DepressButtonX + DepressButtonWidth
.Top = DepressButtonY
End With
DDraw.DisplaySprite DestRect, DepressButton, SrcRect 'Show the depressed button
DSound.PlaySound SoundID(SndClick) 'Play the click sound
InfoRunning = False 'Remove the info screen, start the
game!
GameRunning = True 'Start the game screen running
End If

End Sub

Public Sub Click()

'Check to see if the mouse is over the button


If Sqr((Cursor.xPos - 640) ^ 2 + (Cursor.yPos - 455) ^ 2) < 40 Then IsButtonDepressed = True

End Sub

Public Sub Terminate()

'Reset the running variable


InfoRunning = False
InfoInitialized = False

'Destroy the objects


Set BackGround = Nothing
Set DepressButton = Nothing

End Sub
Timer - 1

Private Declare Function GetTickCount Lib "kernel32" () As Long

Public Function Ticker(Optional Reset As Boolean) As Long

Static oldtime As Long

If Reset Then
'reset timer and return zero
oldtime = GetTickCount()
Ticker = 0
Else
'return difference between oldtime and current time
Ticker = GetTickCount() - oldtime
End If

End Function

Public Function Time() As Long

'Simply returns the system tickcount


Time = GetTickCount()

End Function

Das könnte Ihnen auch gefallen