classxybot.cls
来自「遗传算法和神经网络的结合应用」· CLS 代码 · 共 130 行
CLS
130 行
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "ClassXYbot"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Public WorldWidth As Single
Public WorldHeight As Single
Public x As Single
Public y As Single
Public vx As Single
Public vy As Single
Public direction As Single
Public maxSpeed As Single
Public Accn As Single
Const turnInc = 1
Public Function DistSqr(otherObject As ClassXYbot) As Single
'returns the distance to another object
Dim dx As Single
Dim dy As Single
dx = x - otherObject.x
dy = y - otherObject.y
DistSqr = (dx * dx) + (dy * dy)
End Function
Public Function AngleTo(otherObject As ClassXYbot) As Single
'returns the angle to the other object
Dim dx As Single
Dim dy As Single
Dim dist As Single
Dim ang As Single
dx = x - otherObject.x
dy = y - otherObject.y
dist = (dx * dx) + (dy * dy)
If (dist > 0) Then
ang = Acos(dx / dist)
'If (dy < 0) Then
' ang = (2 * 3.14159227) - ang
'End If
AngleTo = ang
End If
End Function
Public Sub TurnLeft()
direction = direction - turnInc
If (direction < 0) Then
direction = direction + (2 * 3.1415927)
End If
End Sub
Public Sub TurnRight()
direction = direction + turnInc
If (direction > 2 * 3.1415927) Then
direction = direction - (2 * 3.1415927)
End If
End Sub
Public Sub TurnRandom()
If (Rnd < 0.5) Then
Call TurnLeft
Else
Call TurnRight
End If
End Sub
Public Sub Move()
vx = vx + (Accn * Cos(direction))
vy = vy + (Accn * Sin(direction))
If (vx < -maxSpeed) Then
vx = -maxSpeed
End If
If (vx > maxSpeed) Then
vx = maxSpeed
End If
If (vy < -maxSpeed) Then
vy = -maxSpeed
End If
If (vy > maxSpeed) Then
vy = maxSpeed
End If
x = x + vx
y = y + vy
If (x > WorldWidth - 5) Then
x = WorldWidth - 5
End If
If (x < 5) Then
x = 5
End If
If (y > WorldHeight - 5) Then
y = WorldHeight - 5
End If
If (y < 5) Then
y = 5
End If
End Sub
Private Sub Class_Initialize()
maxSpeed = 1
Accn = 0.1
End Sub
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?