步骤 4:添加方法来重新启动游戏

您已了解 IDE 是如何自动将事件处理程序方法添加到程序的。您还可以编写方法并将其添加到代码中。许多程序员花费大量的时间来添加他们自己的方法。

说明说明

如果有一组语句需要在不同的位置执行多次,则编写自己的方法会非常有用。这在编写程序时经常会发生。

例如,在创建此迷宫程序时,您希望此程序在启动时会自动将鼠标指针重新定位到面板的左上角。当用户将指针移到墙上时,您希望此程序将指针重新定位到起始位置。当用户将指针移出游戏场并再次返回时,您希望此程序将指针再次重新定位到起始位置。

可以使用三行代码将指针重新定位到起始点。不过,如果不必在程序中的多个不同位置编写这三行相同的代码,就能够节省时间了。如果将这三行代码放入某个方法(如名为 MoveToStart() 的方法)中,则只需编写这些代码一次。然后,无论您何时想要将指针移回到面板的左上角,只需调用 MoveToStart() 方法即可。

链接到视频有关本主题的视频版本,请参见 Tutorial 2: Create a Maze in Visual Basic - Video 2Tutorial 2: Create a Maze in C# - Video 2

添加方法来重新启动游戏

  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. 现在即可保存并运行程序。程序启动后,指针应自动重新定位到面板左上角的右侧稍微偏下的位置。

继续或查看