现在,您可以准备添加第二个 SoundPlayer,然后添加方法来调用每个 SoundPlayer。
播放声音
首先添加第二个 SoundPlayer 来播放 Windows Tada 声音。当玩家到达**“完成”**标签时,游戏将播放此声音。
Public Class Form1 ' This SoundPlayer plays a sound whenever the player hits a wall. Dim startSoundPlayer = New System.Media.SoundPlayer("C:\Windows\Media\chord.wav") ' This SoundPlayer plays a sound when the player finishes the game. Dim finishSoundPlayer = New System.Media.SoundPlayer("C:\Windows\Media\tada.wav") Public Sub New() ' This call is required by Windows Forms Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. MoveToStart() End Sub
public partial class Form1 : Form { // This SoundPlayer plays a sound whenever the player hits a wall. System.Media.SoundPlayer startSoundPlayer = new System.Media.SoundPlayer(@"C:\Windows\Media\chord.wav"); // This SoundPlayer plays a sound when the player finishes the game. System.Media.SoundPlayer finishSoundPlayer = new System.Media.SoundPlayer(@"C:\Windows\Media\tada.wav"); public Form1() { InitializeComponent(); MoveToStart(); }
此时两个 SoundPlayer 都已添加到您的窗体中。添加 Play() 方法来调用 SoundPlayer,以便在适当的时候播放声音。希望在用户碰墙时播放一种声音。因此,请将语句 startSoundPlayer.Play(); 添加到 MoveToStart() 方法中。请记得更新注释。最终的方法将如下所示。
''' <summary> ''' Play a sound, then 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() startSoundPlayer.Play() Dim startingPoint = Panel1.Location startingPoint.Offset(10, 10) Cursor.Position = PointToScreen(startingPoint) End Sub
/// <summary> /// Play a sound, then 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> private void MoveToStart() { startSoundPlayer.Play(); Point startingPoint = panel1.Location; startingPoint.Offset(10, 10); Cursor.Position = PointToScreen(startingPoint); }
将语句 finishSoundPlayer.Play(); 添加到**“完成”**标签的 MouseEnter 事件处理程序。请记得更新注释,因为您已将代码更改为如下形式。
Private Sub finishLabel_MouseEnter() Handles finishLabel.MouseEnter ' Play a sound, show a congratulatory MessageBox, then close the form. finishSoundPlayer.Play() MessageBox.Show("Congratulations!") Close() End Sub
private void finishLabel_MouseEnter(object sender, EventArgs e) { // Play a sound, show a congratulatory MessageBox, then close the form. finishSoundPlayer.Play(); MessageBox.Show("Congratulations!"); Close(); }
继续或查看
若要转到下一个教程步骤,请参见步骤 8:运行程序并尝试其他功能。
若要返回上一个教程步骤,请参见步骤 6:添加 SoundPlayer。