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

📄 frmmain.vb

📁 This a A* pathfinding example to illustrate how to implement a A* pathfinding algorithm into your pr
💻 VB
📖 第 1 页 / 共 2 页
字号:
'//Thanks to Patrick Lester for the A* information found on his site.
'//This is a really rough (first draft) messy port from Patrick Lesters
'//example A* BlitzBasic source to VB.Net.
'//It's not well organized, encapsulated, or coded to be a usable class.  I used it
'//as a preliminary design for a game I'm working on and figured I would share it.
'//I implemented my own Binary Heap class that could be improved upon to increase
'//search speed.  (Making the dynamic heap array static would help).  Anyways enjoy and
'//note that Patrick Lester has full credit for the orignal BlitzBasic design which this
'//source is heavily based off of.

Imports System
Imports System.Drawing

Public Class frmMain
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents picMain As System.Windows.Forms.PictureBox
    Friend WithEvents gpbTileSelection As System.Windows.Forms.GroupBox
    Friend WithEvents radStart As System.Windows.Forms.RadioButton
    Friend WithEvents radEnd As System.Windows.Forms.RadioButton
    Friend WithEvents radWall As System.Windows.Forms.RadioButton
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmMain))
        Me.picMain = New System.Windows.Forms.PictureBox()
        Me.gpbTileSelection = New System.Windows.Forms.GroupBox()
        Me.radWall = New System.Windows.Forms.RadioButton()
        Me.radEnd = New System.Windows.Forms.RadioButton()
        Me.radStart = New System.Windows.Forms.RadioButton()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.gpbTileSelection.SuspendLayout()
        Me.SuspendLayout()
        '
        'picMain
        '
        Me.picMain.Location = New System.Drawing.Point(8, 8)
        Me.picMain.Name = "picMain"
        Me.picMain.Size = New System.Drawing.Size(376, 376)
        Me.picMain.TabIndex = 0
        Me.picMain.TabStop = False
        '
        'gpbTileSelection
        '
        Me.gpbTileSelection.Controls.AddRange(New System.Windows.Forms.Control() {Me.radWall, Me.radEnd, Me.radStart})
        Me.gpbTileSelection.Location = New System.Drawing.Point(392, 8)
        Me.gpbTileSelection.Name = "gpbTileSelection"
        Me.gpbTileSelection.Size = New System.Drawing.Size(144, 112)
        Me.gpbTileSelection.TabIndex = 1
        Me.gpbTileSelection.TabStop = False
        Me.gpbTileSelection.Text = "Tile Selection"
        '
        'radWall
        '
        Me.radWall.Location = New System.Drawing.Point(8, 80)
        Me.radWall.Name = "radWall"
        Me.radWall.Size = New System.Drawing.Size(128, 16)
        Me.radWall.TabIndex = 2
        Me.radWall.Text = "Wall Tile"
        '
        'radEnd
        '
        Me.radEnd.Location = New System.Drawing.Point(8, 52)
        Me.radEnd.Name = "radEnd"
        Me.radEnd.Size = New System.Drawing.Size(128, 16)
        Me.radEnd.TabIndex = 1
        Me.radEnd.Text = "Ending Tile"
        '
        'radStart
        '
        Me.radStart.Checked = True
        Me.radStart.Location = New System.Drawing.Point(8, 24)
        Me.radStart.Name = "radStart"
        Me.radStart.Size = New System.Drawing.Size(128, 16)
        Me.radStart.TabIndex = 0
        Me.radStart.TabStop = True
        Me.radStart.Text = "Starting Tile"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(392, 128)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(144, 23)
        Me.Button1.TabIndex = 2
        Me.Button1.Text = "Run"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(392, 160)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(144, 23)
        Me.Button2.TabIndex = 3
        Me.Button2.Text = "Clear"
        '
        'frmMain
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(546, 391)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button2, Me.Button1, Me.gpbTileSelection, Me.picMain})
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.MaximizeBox = False
        Me.Name = "frmMain"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "A* PathFinder"
        Me.gpbTileSelection.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

#Region " Declares "
    Private Const inOpened = 1
    Private Const inClosed = 2
    Private Heap As New BinaryHeap()
    Private StartX, StartY, EndX, EndY As Int16
    Private Map(24, 24) As CellData
    Private oBuff As New Bitmap(376, 376)
    Private oBuffG As Graphics = Graphics.FromImage(oBuff)
    Private PathFound, PathHunt As Boolean
    Private ParentX, ParentY As Int16
#End Region

#Region " Subs "

    Private Sub FindPath()
        Dim xCnt, yCnt As Int16

        'Make sure the starting point and ending point are not the same
        If (StartX = EndX) And (StartY = EndY) Then Exit Sub

        'Make sure the starting point is not a wall
        If Map(StartX, StartY).Wall Then Exit Sub
        If Map(EndX, EndY).Wall Then Exit Sub

        'Set the flags
        PathFound = False
        PathHunt = True

        'Put the starting point on the open list
        Map(StartX, StartY).OCList = inOpened
        Heap.Add(0, StartX, StartY)

        'Find the children
        While PathHunt
            If Heap.Count <> 0 Then
                'Get the parent node
                ParentX = Heap.GetX
                ParentY = Heap.GetY

                'Remove the root
                Map(ParentX, ParentY).OCList = inClosed
                Heap.RemoveRoot()

                'Find the available children to add to the open list
                For yCnt = (ParentY - 1) To (ParentY + 1)
                    For xCnt = (ParentX - 1) To (ParentX + 1)

                        'Make sure we are not out of bounds
                        If xCnt <> -1 And xCnt <> 25 And yCnt <> -1 And yCnt < 25 Then

                            'Make sure it's not on the closed list
                            If Map(xCnt, yCnt).OCList <> inClosed Then

                                'Make sure no wall

⌨️ 快捷键说明

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