Beruflich Dokumente
Kultur Dokumente
'*****************************************************************
'
'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
'
'*****************************************************************
End Sub
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:
End Sub
FrmDog - 2
End Sub
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
Global FrameTime As Long 'System time that last frame was flipped at
Const FrameRate = 100 'How many MS per frame?
'This routine initializes the display mode, and the primary/backbuffer complex
'Handles errors
On Local Error GoTo ErrOut
Exit Sub
ErrOut:
FrmDog.ExitProgram 'If there's an error, exit the program
End Sub
End Sub
End Function
End Sub
End Sub
End Sub
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
'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
End Sub
End Sub
End Sub
DSound - 1
'DirectX Variables
Dim ds As DirectSound
Dim dx As New DirectX7
'Constant that contains the path inside the app.path in which the sounds are stored
Const DataLocation = "\data\"
'Set the DirectSound object's cooperative level (Priority gives us sole control)
ds.SetCooperativeLevel Handle, DSSCL_PRIORITY
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)
ErrOut:
'Display an error message and exit if initialization failed
MsgBox "Unable to initialize DirectSound."
End
End Sub
Dim i As Integer
Dim Index As Integer
Dim DSBufferDescription As DSBUFFERDESC
Dim DSFormat As WAVEFORMATEX
Dim DSPosition(0) As DSBPOSITIONNOTIFY
'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
'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
End 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
End Sub
End Sub
End Sub
'Check to make sure that the buffer has the capability of altering its frequency
If Sound(Index).DSCaps.Frequency = False Then Exit Sub
End Sub
'Check to make sure that the buffer has the capability of altering its volume
If Sound(Index).DSCaps.Volume = False Then Exit Sub
End Sub
'Check to make sure that the buffer has the capability of altering its pan
If Sound(Index).DSCaps.Pan = False Then Exit Sub
End Sub
'Check to make sure that the buffer has the capability of altering its frequency
If Sound(Index).DSCaps.Frequency = False Then Exit Function
End Function
'Check to make sure that the buffer has the capability of altering its volume
If Sound(Index).DSCaps.Volume = False Then Exit Function
End Function
'Check to make sure that the buffer has the capability of altering its pan
If Sound(Index).DSCaps.Pan = False Then Exit Function
End Function
End Function
Dim i As Integer
'Return the ID
DXCallback = i
End Function
Dim i As Integer
End Sub
EntryScreen - 1
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
End Sub
Dim i As Integer
Dim CKey As DDCOLORKEY
Dim ddsdNewSprite As DDSURFACEDESC2
'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
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
End Sub
Dim i As Integer
Dim SrcRect As RECT
Dim DestRect As RECT
End With
DDraw.DisplaySprite DestRect, Title(TitleAnim), SrcRect
End If
End Sub
Dim i As Integer
End Sub
Dim i As Integer
End Sub
GameScreen - 1
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
End Sub
Dim i As Integer
Dim CKey As DDCOLORKEY
Dim ddsdNewSprite As DDSURFACEDESC2
'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
End Sub
'If the game has been won or lost, don't check for other keystrokes
If GameWon Or GameLost Then Exit Sub
End Sub
Dim i As Integer
Else
DogYapRate = DogYapRate + 1
End If
End Sub
.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
End Sub
End Sub
Dim i As Integer
Dim FreeRock As Integer
End Sub
End Sub
GameLostTime = GameLostTime + 1
End Sub
GameWonTime = GameWonTime + 1
End Sub
Dim i As Integer
End Sub
InfoScreen - 1
End Sub
Dim i As Integer
Dim CKey As DDCOLORKEY
Dim ddsdNewSprite As DDSURFACEDESC2
'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
End Sub
Dim i As Integer
Dim SrcRect As RECT
Dim DestRect As RECT
End Sub
End Sub
End Sub
Timer - 1
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
End Function