TOC

This article has been localized into Chinese by the community.

创建一个游戏:WPF贪吃蛇:
Chapter introduction:

In this article series, we're building a complete Snake game from scratch. It makes sense to start with the Introduction and then work your way through the articles one by one, to get the full understanding.

If you want to get the complete source code for the game at once, to get started modifying and learning from it right now, consider downloading all our samples!

改善WPF贪吃蛇:添加声音

大多数游戏都会有声音效果来增强体验,但目前为止,我们的小贪吃蛇实现是完全安静的。我们过去在这个教程中讨论到音频和视频,因此如果你有读到那些文章,你就知道播放声音在WPF是非常简单的。事实上,如果你能够忍受系统声音,用一行代码就能够做到:

SystemSounds.Beep.Play();

如果你需要更多东西,你能够使用MediaPlayer 类播放你自己的音频文件(例如MP3)。你能够在这篇文章阅读所有相关内容:用WPF播放音频.一个有趣的小项目可能去记录你咬苹果的声音,然后在贪吃蛇吃到一个苹果的时候播放它,这非常容易去完成!

让蛇说话

对于本教程,我决定用另一种方式,而不仅仅只是播放常规的声音,我想要让蛇说话!这可能听起来困难,但是前提是你还没有阅读本教程的全部内容,因为如果你有,你知道WPF有很好的支持语音合成. 有了这个,我们能够让贪吃蛇说话变得非常简单!

首先,你需要去添加一个System.Speech程序集的引用到你的项目。有关将此程序集添加到项目中的说明,请看过去这篇文章:语音合成 (让WPF说话)。在上面,你将找到如何去实现的具体细节。

我决定在几种情况下让贪吃蛇说话,因此我会创建一个常用的SpeechSynthesizer实例,我每次都会重复使用它,但是首先在顶部添加一个System.Speech.Synthesis 命名空间的引用:

using System.Speech.Synthesis;

然后在你的Window顶部声明和初始化实例:

public partial class MainWindow : Window
{
private SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer();
.......

"好吃!"

这是我们让蛇说话需要的一切-什么时候应该让它说和该说什么?我决定去让它每次吃到一个苹果的时候说“yummy”(好吃的)。这发生在我们过去实现的EatSnakeFood()方法中,因此只要在顶部添加这一行:

private void EatSnakeFood()
{
    speechSynthesizer.SpeakAsync("yummy");
    ......

是的-运行游戏,然后享受美味,每次贪吃蛇吃一个苹果时候。当然该字符串能被改为另外一个单词或者甚至一句话,如果你开心的话。

"哦,不 - 你死了!"

如你所见,说"yummy"超级简单,但是SpeechSynthesizer能够做多的多!为了验证,我决定去让贪吃蛇讨论自己的死亡和最终游戏的分数,换句话说,我们需要去添加一些对话到EndGame()方法中。这将有非常多额外的代码行,因为我需要用不同语音设置几句话,因此我决定去将结束游戏时的对话代码,封装在我们叫SpeakEndOfGameInfo()方法中,它将在我们过去在这个教程实现的EndGame()方法的底部调用,因此只需在方法的底部添加调用:

private void EndGame()
{
    .......
    SpeakEndOfGameInfo(isNewHighscore);
}

以下是我们实现它的方式:

private void SpeakEndOfGameInfo(bool isNewHighscore)  
{  
    PromptBuilder promptBuilder = new PromptBuilder();  

    promptBuilder.StartStyle(new PromptStyle()  
    {  
Emphasis = PromptEmphasis.Reduced,  
Rate = PromptRate.Slow,  
Volume = PromptVolume.ExtraLoud  
    });  
    promptBuilder.AppendText("oh no");  
    promptBuilder.AppendBreak(TimeSpan.FromMilliseconds(200));  
    promptBuilder.AppendText("you died");  
    promptBuilder.EndStyle();  

    if(isNewHighscore)  
    {  
promptBuilder.AppendBreak(TimeSpan.FromMilliseconds(500));  
promptBuilder.StartStyle(new PromptStyle()  
{  
    Emphasis = PromptEmphasis.Moderate,  
    Rate = PromptRate.Medium,  
    Volume = PromptVolume.Medium  
});  
promptBuilder.AppendText("new high score:");  
promptBuilder.AppendBreak(TimeSpan.FromMilliseconds(200));  
promptBuilder.AppendTextWithHint(currentScore.ToString(), SayAs.NumberCardinal);  
promptBuilder.EndStyle();  
    }  
    speechSynthesizer.SpeakAsync(promptBuilder);  
}

因此这有相当多的代码,但是如果你仔细看,你会看到这里有很多同样的东西,我们使用一个PromptBuilder 的实例去创建一个长句子,具有各种发音设置。在最后,这将会让贪吃蛇每次你死的时候说 "Oh no - you died"。如果你进入了高分榜,在之前一篇文章添加了,将会添加“新高分”,然后是实际得分。

使用适当的设置说出每部分,使用PromptStyle 实例,例如,“oh no - you died”缓慢被说出,音量增加。我们也使用AppendBreak()方法在不同部分中添加自然断句。你能够在我们前一篇关于语音合成的文章阅读有关这些技术的信息.

小结

感谢System.Speech 程序集和SpeechSynthesizer 类,我们贪吃蛇刚学会说话了!这让我们的游戏更加有趣,同时展示了.NET framework如何酷,强大和灵活。


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!