pacmod.bas
来自「吃豆子游戏的源代码。 嘿嘿」· BAS 代码 · 共 386 行 · 第 1/2 页
BAS
386 行
.Cherry(NewCherry).Top = Int((Grid - .Cherry(NewCherry).Width) / 2) + (Grid * (Lines - 1))
.Cherry(NewCherry).Left = Int((Grid - .Cherry(NewCherry).Height) / 2) + (Grid * (Char - 1))
.Cherry(NewCherry).Visible = True
Call AddObjectToList(MainFrm.CherryList, Lines, Char, NewCherry)
Case mLife
NewLife = .Life.Count + 1
Load .Life(NewLife)
.Life(NewLife).Top = Int((Grid - .Life(NewLife).Width) / 2) + (Grid * (Lines - 1))
.Life(NewLife).Left = Int((Grid - .Life(NewLife).Height) / 2) + (Grid * (Char - 1))
.Life(NewLife).Visible = True
Call AddObjectToList(MainFrm.LifeList, Lines, Char, NewLife)
Case mProtect
NewProtect = .Protect.Count + 1
Load .Protect(NewProtect)
.Protect(NewProtect).Top = Int((Grid - .Protect(NewProtect).Width) / 2) + (Grid * (Lines - 1))
.Protect(NewProtect).Left = Int((Grid - .Protect(NewProtect).Height) / 2) + (Grid * (Char - 1))
.Protect(NewProtect).Visible = True
Call AddObjectToList(MainFrm.ProtectList, Lines, Char, NewProtect)
Case mBerry
NewBerry = .Berry.Count + 1
Load .Berry(NewBerry)
.Berry(NewBerry).Top = Int((Grid - .Berry(NewBerry).Width) / 2) + (Grid * (Lines - 1))
.Berry(NewBerry).Left = Int((Grid - .Berry(NewBerry).Height) / 2) + (Grid * (Char - 1))
.Berry(NewBerry).Visible = True
Call AddObjectToList(MainFrm.BerryList, Lines, Char, NewBerry)
End Select
End With
End Sub
Public Sub UpdateObjPos(Lines As Integer, Char As Integer, NewFood As Integer)
'update the one string encoded position of an object
TmpStr3 = Trim(Str(NewFood)) 'the type of food ( ie beer, berry , cherry, etc,)
Lines = Lines - 1 'the y-pos , -1 because there is no 0 line
Char = Char - 1 'the x-pos
'the y-position
If Int(Grid * Lines) < 1000 Then 'the position has 3 digits
TmpStr1 = "0" & Trim(Str(Int(Grid * Lines))) 'make sure the string has 4 digits
Else
TmpStr1 = Trim(Str(Int(Grid * Lines))) 'the string already has 4 digits
End If
'the x-position
If Int(Grid * Char) < 1000 Then 'the position has 3 digits
TmpStr2 = "0" & Trim(Str(Int(Grid * Char - 1))) 'make sure the string has 4 digits
Else
TmpStr2 = Trim(Str(Int(Grid * Char - 1))) 'the string already has 4 digits
End If
FinalString = Trim(TmpStr1 & TmpStr2 & TmpStr3) 'the final string is all the 3 tmp string added together
End Sub
'add an objects position to its listbox
Public Sub AddObjectToList(List As ListBox, Lines As Integer, Char As Integer, NewFood As Integer)
Call UpdateObjPos(Lines, Char, NewFood) 'encode the positions into a string
List.AddItem FinalString 'add the string to the listbox
Lines = Lines + 1 'restore original line number
Char = Char + 1 'restore original char number
End Sub
'check for an onject in the specified direction
Public Function CheckForObject(Direction As Integer, WhichList As ListBox) As Integer
If UpdateStrObjectPos(Direction, MainFrm.PacMan) = 1 Then GoTo CollectFood 'there is food there
CheckForObject = 0 'ther is no food there
Exit Function
CollectFood: 'the food is being eaten , so remove it from the list
FinalString = Trim(TmpStr1 & TmpStr2) ' encode the string
For I = 0 To WhichList.ListCount 'go through each entry on in the list
TmpStr3 = Trim(Left$(WhichList.List(I), 8)) 'get the cutrrent list string
On Error Resume Next
If TmpStr3 = FinalString Then 'test for a match, there is a match
TmpStr3 = Trim(WhichList.List(I))
CheckForObject = Val(Right(TmpStr3, (Len(TmpStr3) - 8))) 'return the food array number
WhichList.List(CheckForObject - 2) = "" 'set the value to a null string, as the food has been eaten
End If
Next I
End Function
' update the encoded string position on the next direction
Public Function UpdateStrObjectPos(Direction As Integer, Object As Object) As Integer
Dim I As Integer 'used in for..next loops
On Error Resume Next
With MainFrm
If NowDead = True Then Exit Function 'make sure pacman is alive
Select Case Direction
Case Is = 1 'Up
'encode the string. see the sub "UpdateObjPos" to see how this works
If Int(Object.Top - Grid) < 1000 Then
TmpStr1 = "0" & Trim(Str(Int(Object.Top - Grid)))
Else
TmpStr1 = Trim(Str(Int(Object.Top - Grid)))
End If
If Int(Object.Left) < 1000 Then
TmpStr2 = "0" & Trim(Str(Int(Object.Left - 1)))
Else
TmpStr2 = Trim(Str(Int(Object.Left - 1)))
End If
UpdateStrObjectPos = 1
Case Is = 2 'Down
'encode the string. see the sub "UpdateObjPos" to see how this works
If Int(Object.Top + Grid) < 1000 Then
TmpStr1 = "0" & Trim(Str(Int(Object.Top + Grid)))
Else
TmpStr1 = Trim(Str(Int(Object.Top + Grid)))
End If
If Int(Object.Left) < 1000 Then
TmpStr2 = "0" & Trim(Str(Int(Object.Left - 1)))
Else
TmpStr2 = Trim(Str(Int(Object.Left - 1))) ' Else
End If
UpdateStrObjectPos = 1
Case Is = 3 'left
'encode the string. see the sub "UpdateObjPos" to see how this works
If Int(Object.Top) < 1000 Then
TmpStr1 = "0" & Trim(Str(Int(Object.Top)))
Else
TmpStr1 = Trim(Str(Int(Object.Top)))
End If
If Int(Object.Left - Grid) < 1000 Then
TmpStr2 = "0" & Trim(Str(Int(Object.Left - Grid) - 1))
Else
TmpStr2 = Trim(Str(Int(Object.Left - Grid) - 1))
End If
UpdateStrObjectPos = 1
Case Is = 4 'Right
'encode the string. see the sub "UpdateObjPos" to see how this works
If Int(Object.Top) < 1000 Then
TmpStr1 = "0" & Trim(Str(Int(Object.Top)))
Else
TmpStr1 = Trim(Str(Int(Object.Top)))
End If
If Int(Object.Left + Grid) < 1000 Then
TmpStr2 = "0" & Trim(Str(Int(Object.Left + Grid) - 1))
Else
TmpStr2 = Trim(Str(Int(Object.Left + Grid) - 1))
End If
UpdateStrObjectPos = 1
End Select
End With
End Function
'change the directory
Public Function ThisDir(Optional SubDir As String) As Boolean
On Error GoTo errHand:
ChDrive App.Path 'goto this drive
ChDir App.Path & "\" & SubDir 'goto this dir
ThisDir = True 'dir exsits
Exit Function
errHand:
ThisDir = False 'dir not there
End Function
'test for a wall
Public Function IsWallThere(Object As Object, Direction As Integer) As Boolean
Call UpdateStrObjectPos(Direction, Object) '\--encode the string
FinalString = Trim(TmpStr1 & TmpStr2) '/-^
For I = 0 To MainFrm.WallList.ListCount 'go throgh the list of walls
TmpStr3 = Trim(Left$(MainFrm.WallList.List(I), 8)) 'get the current list string
If TmpStr3 = FinalString Then 'test if it is there
IsWallThere = True 'there is a wall there
End If
Next I
End Function
'pause the app
Public Sub Pause(PauseTime As Single)
Dim StartTime As Single
StartTime = Timer
Do While Timer < PauseTime + StartTime
DoEvents
Loop
End Sub
'unloads all arrays (objects) and clears all lists
Public Sub UnloadArrays()
On Error Resume Next
With MainFrm
UnloadArray .Wall '\
UnloadArray .Food ' \
UnloadArray .Life ' \
UnloadArray .Protect ' \--unload all objects
UnloadArray .Ghoul ' /-^
UnloadArray .Beer ' /
UnloadArray .Berry ' /
UnloadArray .Cherry '/
.BerryList.Clear '\
.BeerList.Clear ' \
.CherryList.Clear ' \
.FoodList.Clear ' |-- clear lists
.LifeList.Clear ' /
.ProtectList.Clear ' /
.WallList.Clear '/
End With
End Sub
'unload the array
Public Sub UnloadArray(UnArray)
On Error Resume Next 'only happen if obj not exsist
For I = (UnArray.UBound + 5) To 0 Step -1 'go through each array
Unload UnArray(I) 'unload it
Next I
End Sub
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?