⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 globals.bas

📁 VB编写的RPG游戏演示,适合VB游戏初学者的参考。
💻 BAS
字号:
Attribute VB_Name = "Globals"
Option Explicit

'Some API..
Declare Function IntersectRect Lib "user32" (lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT) As Long

'DirectX
Global gdx As DirectX7

'Some general constants
Global Const MAX_FPS = 50
Global Const FADE_DELAY_MS = 5
Global Const SCREEN_WIDTH = 640
Global Const SCREEN_HEIGHT = 480
Global Const SCREEN_BITDEPTH = 16
Global Const SPEECH_WIDTH = 640
Global Const SPEECH_HEIGHT = 120
Global Const SPEECH_START_HEIGHT = 25
Global Const SPEECH_START_WIDTH = 25
Global Const SPEECH_LINE_SPACING = 20
Global Const TILE_WIDTH = 32
Global Const TILE_HEIGHT = 32
Global Const TILESET_COLUMNS = 19                   'Zero based...
Global Const TILESET_ROWS = 14
Global Const HORIZ_EDGE = 10
Global Const VERT_EDGE = 7

'Sprite display global constants (facing)
Global Const FACE_DOWN = 0
Global Const FACE_LEFT = 1
Global Const FACE_UP = 2
Global Const FACE_RIGHT = 3
Global Const FIGHT = 4
Global Const MAGIC = 5

'Heading global constants
Global Const MOVE_DOWN = 0
Global Const MOVE_LEFT = 1
Global Const MOVE_UP = 2
Global Const MOVE_RIGHT = 3
Global Const MOVE_NONE = 4

'Movement behaviour global constants
Global Const RANDOM_WALK = 1

Type PORTAL_TYPE
    strMapName As String            'Name of the map to "portal" to
    intX As Integer                 'Coordinates of the start location within new map
    intY As Integer
End Type
Type MONSTER_TYPE
    bytMonster(3) As Byte           'Monsters to fight
    bytProbability As Integer       'Probability of combat
    lngProgChange As Long           'Change in game progress as a result of victory
End Type
Type MAP_TYPE
    intColumn As Byte
    intRow As Byte
    blnNonWalkable As Boolean
    udtPortal As PORTAL_TYPE
    udtMonster As MONSTER_TYPE
End Type
Global gudtMap() As MAP_TYPE           'Our map array

'Character type
Type CHARACTERTYPE
    intXTile As Integer
    intYTile As Integer
    bytFacing As Byte
    bytAnim As Byte
    intAnimDelay As Integer
    intWalkAnimRate As Integer
    intWalkSpeed As Integer
    intHorizontalDisp As Integer
    intVerticalDisp As Integer
    blnMoving As Boolean
    bytHeading As Byte
    bytCharNum As Byte
    bytBehaviour As Byte
    strSpeech As String
    blnSpeaking As Boolean
End Type
Global gudtCharacter() As CHARACTERTYPE

Type BEHAVIOUR_TYPE
    lngProgressReq As Long          'Progress required to exhibit this behaviour set
    strText As String               'Speech text
    bytTalkItemChange As Byte       'Item change after talking?
    lngTalkProgChange As Long       'Progress change after talking?
    blnDisapear As Boolean          'Disappear after talking?
    bytBehaviour As Byte            'Walking behaviour
    bytCharNum As Byte              'Sprite to display
    bytMonster As Byte              'Monster to fight after speech
    blnVisible As Boolean           'Is the sprite visible at this time?
    intX As Integer                 'Starting coords of the sprite
    intY As Integer
End Type
Type NPC_TYPE
    udtBehaviour() As BEHAVIOUR_TYPE
End Type
Global gudtNPC() As NPC_TYPE

'Our current game progress
Global glngProgress As Long

'Character screen is centered on
Global gintCenter As Integer

'NPC Speech variables
Global gblnspeaking As Boolean
Global gstrSpeech As String
Global gintSpeakingNPC As Integer

Global gstrMapName As String * 16
Global gstrMusic As String * 16
Global gstrMusicPlaying As String * 16

Public Sub LoadMap(strMapName As String)

Dim i As Integer
Dim j As Integer
Dim intMapWidth As Integer
Dim intMapHeight As Integer
Dim intNPCNum As Integer
Dim bytBehaviour As Byte

    'Load map data
    Open App.Path & "\" & strMapName For Binary Access Read Lock Write As #1
        'Get the map data
        Get #1, 1, intMapWidth
        Get #1, , intMapHeight
        ReDim gudtMap(intMapWidth, intMapHeight)
        Get #1, , gudtMap
        'Get the NPC data
        Get #1, , intNPCNum
        ReDim gudtNPC(intNPCNum)
        Get #1, , gudtNPC
        'Get the title/music
        Get #1, , gstrMapName
        Get #1, , gstrMusic
    Close #1
    
    'Make the edges of the map unwalkable
    For i = 0 To intMapWidth
        gudtMap(i, VERT_EDGE).blnNonWalkable = True
        gudtMap(i, intMapHeight - VERT_EDGE).blnNonWalkable = True
    Next i
    For i = 0 To intMapHeight
        gudtMap(HORIZ_EDGE, i).blnNonWalkable = True
        gudtMap(intMapWidth - HORIZ_EDGE, i).blnNonWalkable = True
    Next i
    
    'Create the character array
    ReDim gudtCharacter(intNPCNum)
        
    'Set the main character as the center object
    gintCenter = 0
    
    'Set the initial player values (if they aren't already set!)
    If gudtCharacter(gintCenter).intXTile = 0 Then gudtCharacter(gintCenter).intXTile = gudtNPC(gintCenter).udtBehaviour(0).intX
    If gudtCharacter(gintCenter).intYTile = 0 Then gudtCharacter(gintCenter).intYTile = gudtNPC(gintCenter).udtBehaviour(0).intY
    gudtCharacter(gintCenter).intWalkAnimRate = 10
    gudtCharacter(gintCenter).intWalkSpeed = 2
    
    'If there ARE any NPC's...
    If intNPCNum > 0 Then
        'Set the walk values
        For i = 1 To intNPCNum
            gudtCharacter(i).intWalkAnimRate = 5
            gudtCharacter(i).intWalkSpeed = 1
        Next i
        'Load the NPC data into the character array
        For i = 1 To intNPCNum
            'Determine the appropriate current behaviour for this sprite
            bytBehaviour = 0
            If UBound(gudtNPC(i).udtBehaviour) > 0 Then
                For j = 1 To UBound(gudtNPC(i).udtBehaviour)
                    If (gudtNPC(i).udtBehaviour(j).lngProgressReq And glngProgress) = gudtNPC(i).udtBehaviour(j).lngProgressReq Then
                        bytBehaviour = j
                        Exit For
                    End If
                Next j
            End If
            'Place the NPCs
            gudtCharacter(i).intXTile = gudtNPC(i).udtBehaviour(bytBehaviour).intX
            gudtCharacter(i).intYTile = gudtNPC(i).udtBehaviour(bytBehaviour).intY
            'Assign bitmaps
            gudtCharacter(i).bytCharNum = gudtNPC(i).udtBehaviour(bytBehaviour).bytCharNum
            'Asign walk behaviour
            gudtCharacter(i).bytBehaviour = gudtNPC(i).udtBehaviour(bytBehaviour).bytBehaviour
            'Save speech text
            gudtCharacter(i).strSpeech = gudtNPC(i).udtBehaviour(bytBehaviour).strText
        Next i
    End If
    
    'Set map squares non-walkable
    For i = 1 To UBound(gudtCharacter)
        gudtMap(gudtCharacter(i).intXTile, gudtCharacter(i).intYTile).blnNonWalkable = True
    Next i
    
    'Play music
    If (Left(gstrMusic, 1) <> " ") And (gstrMusic <> gstrMusicPlaying) Then DMusic.Play gstrMusic
        
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -