共用方式為


步驟 4:加入方法以重新啟動遊戲

您已看到 IDE 如何自動將事件處理常式方法加入至您的程式中。您也可以自行撰寫方法並加入至您的程式碼。許多程式設計人員都會花很多時間加入他們自己的方法。

注意事項注意事項

當有一組陳述式需要在不同的位置執行許多次時,撰寫自己的方法就很有用。撰寫程式時經常有這種情形。

例如,以建立這個迷宮程式而言,當程式啟動時,您希望它自動將滑鼠指標移至面板的左上角。當使用者將指標移至圍牆時,您希望它將指標移回起點。當使用者將指標移至遊戲場外又移回來時,您希望它再次將指標移回起點。

您可以使用三行程式碼將指標移回起點。但是,如果不需要在程式中的許多不同位置重複撰寫這三行程式碼,就可以節省時間。如果將這三行程式碼放在一個方法中,例如稱為 MoveToStart() 的方法,則這三行程式碼只需要撰寫一次。之後,每當您想要將指標移回至面板左上角時,只要呼叫 MoveToStart() 方法即可。

若要加入方法以重新啟動遊戲

  1. 在 [方案總管] 中以滑鼠右鍵按一下 [Form1.cs],然後從功能表中選取 [檢視程式碼],以移至表單的程式碼。

  2. 您應該會看到您已加入的 finishLabel_MouseEnter() 方法。緊接在該方法下方,加入新的 MoveToStart() 方法。

    Private Sub MoveToStart()
        Dim startingPoint = Panel1.Location
        startingPoint.Offset(10, 10)
        Cursor.Position = PointToScreen(startingPoint)
    End Sub
    
    private void MoveToStart()
    {
        Point startingPoint = panel1.Location;
        startingPoint.Offset(10, 10);
        Cursor.Position = PointToScreen(startingPoint);
    }
    
  3. 您可以在任何方法的上方加入一種特殊類型的註解,而 IDE 也會協助您加入這種註解。將游標移至新方法的上一行。在 Visual C# 中,加入三個斜線符號 (///)。在 Visual Basic 中,加入三個單引號 (''')。IDE 會自動填入下列文字。

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub MoveToStart()
        Dim startingPoint = Panel1.Location
        startingPoint.Offset(10, 10)
        Cursor.Position = PointToScreen(startingPoint)
    End Sub
    
    /// <summary>
    /// 
    /// </summary>
    private void MoveToStart()
    {
        Point startingPoint = panel1.Location;
        startingPoint.Offset(10, 10);
        Cursor.Position = PointToScreen(startingPoint);
    }
    
  4. 在兩個摘要標記中間的行上,填入下列註解 (按下 ENTER 鍵之後,依據您的程式語言,IDE 會自動加入含三個斜線符號 (///) 或三個單引號 (''') 的新行,讓您繼續輸入註解)。

    ''' <summary>
    ''' Move the pointer to a point 10 pixels down and to the right
    ''' of the starting point in the upper-left corner of the maze.
    ''' </summary>
    
    /// <summary>
    /// Move the pointer to a point 10 pixels down and to the right
    /// of the starting point in the upper-left corner of the maze.
    /// </summary> 
    
    注意事項注意事項

    您剛剛已加入一個 XML 註解。您可能記得,當您停留在 MessageBox 這個字上方時,IDE 會以工具提示的形式顯示資訊。IDE 會自動為您的方法填入工具提示。您在 XML 註解中輸入的任何文字都會出現在 IDE 的工具提示中,也會出現在 [IntelliSense] 視窗中。對於含有許多方法的程式而言,這樣很有用。另外,如果您在面板左上角的右下方 10 個像素處放置一面圍牆,您可以在程式碼中變更 (10, 10)。試驗不同的數字,直到找到您的迷宮適合使用的指標起點為止。

  5. 加入方法之後,您需要呼叫它。因為您希望程式在啟動時立即將指標移至起點,所以您應該在表單開始時立即呼叫該方法。在 Visual C# 中,請在表單的程式碼中尋找下列方法。

    public Form1()
    {
        InitializeComponent();
    }
    

    在 Visual Basic 中,請在表單的程式碼中加入這個方法。在 finishLabel_MouseEnter 方法前面,開始輸入下列程式碼。

    Public Sub New()
    

    當您按下 ENTER 鍵移至下一行時,IntelliSense 應該會讓下列程式碼看起來已完成。

    Public Sub New()
        ' This call is required by Windows Forms Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
    End Sub
    

    這是一個稱為建構函式的特殊方法。它只會在表單建立時執行一次。目前,它會執行的動作只有呼叫稱為 InitializeComponent() 的方法。您將會加入一行來呼叫您剛撰寫的新 MoveToStart() 方法。繼續之前,請考慮要在程式中加入什麼,才能讓它在呼叫 InitializeComponent() 方法之後又立即呼叫 MoveToStart() 方法。

    注意事項注意事項

    表單建構函式中的 InitializeComponent() 方法是 IDE 撰寫的方法。該方法將所有控制項和元件加入至表單,並設定它們的屬性。每當您變更表單的任何屬性或其控制項時,IDE 就會更改這個方法。您可以從 [方案總管] 中開啟 Form1.Designer.cs 檔案,就可以查看這個方法。您不需要編輯 InitializeComponent() 方法的內容。IDE 會根據您在 [設計] 檢視中建立的表單來處理它。

  6. 加入程式碼,在呼叫 InitializeComponent() 方法之後,立即呼叫 MoveToStart() 方法。您的表單程式碼應該看起來如下。

    Public Class Form1
    
        Public Sub New()
            ' This call is required by Windows Forms Designer.
            InitializeComponent()
            ' Add any initialization after the InitializeComponent() call.
            MoveToStart()
        End Sub
    
        Private Sub finishLabel_MouseEnter() Handles finishLabel.MouseEnter
            ' Show a congratulatory MessageBox, then close the form.
            MessageBox.Show("Congratulations!")
            Close()
        End Sub
    
        ''' <summary>
        ''' Move the mouse pointer to a point 10 pixels down and to the right
        ''' of the starting point in the upper-left corner of the maze.
        ''' </summary>
        ''' <remarks></remarks>
        Private Sub MoveToStart()
            Dim startingPoint = Panel1.Location
            startingPoint.Offset(10, 10)
            Cursor.Position = PointToScreen(startingPoint)
        End Sub
    
    End Class
    
    namespace Maze
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                MoveToStart();
            }
    
            private void finishLabel_MouseEnter(object sender, EventArgs e)
            {
                // Show a congratulatory MessageBox, then close the form.
                MessageBox.Show("Congratulations!");
                Close();
            }
    
            /// <summary>
            /// Move the pointer to a point 10 pixels down and to the right
            /// of the starting point in the upper-left corner of the maze.
            /// </summary>
            private void MoveToStart()
            {
                Point startingPoint = panel1.Location;
                startingPoint.Offset(10, 10);
                Cursor.Position = PointToScreen(startingPoint);
            }
        }
    }
    

    請注意在 InitializeComponent() 之下對 MoveToStart() 方法的呼叫。如果您使用 Visual C# 來編寫程式,請記得在該行尾端加上分號 (;),否則無法建置程式。

  7. 現在儲存並執行程式。在程式啟動後,指標會自動從面板的左上角往右下方稍微移動。

若要繼續或檢視