As a beginner in WPF, I am trying to become really better in the subject by changing existing codes and adding some new features in it. This code simply adds a voice control feature of the snake WPF game from here. In general, the original code is kept as it was and I have just added a few lines, to be able to control the snake with voice.
Additionally, I have decided to limit the directions to “left” and “right” only as far as I was yelling a lot while I was testing the code 🙂 Thus, if the snake is going left and you say “right”, the snake would go “up”. Simple as that.
In order to start the game simply say “Left” or “Right”. You may say “Game over” and this would end the game. Other voice commands are not included.
The colors are changed as well 🙂
I have changed a few things in the code, but in general it stays the same. The voice recognition is achieved through the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public MainWindow() { InitializeComponent(); DispatcherTimer timer = new DispatcherTimer(); timer.Tick += new EventHandler(timer_Tick); timer.Interval = SLOW; timer.Start(); sre = new SpeechRecognitionEngine(); GrammarBuilder gb = new GrammarBuilder(new Choices("game over", "left", "right")); sre.LoadGrammar(new Grammar(gb)); sre.SetInputToDefaultAudioDevice(); sre.RecognizeAsync(RecognizeMode.Multiple); sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(RecognizeSpeech); paintSnake(startingPoint); currentPosition = startingPoint; // Instantiate Food Objects for (int n = 0; n < 10; n++) { paintBonus(n); } } |
In order to obtain the classes “Speech RecognitionEngine()”, “GrammerBuilder()” and etc. we need to add the namespaces:
1 2 |
using System.Speech.Recognition; using System.Speech.Recognition.SrgsGrammar; |
Once, we have them it is really easy to make the code. Two other methods are added. A method for recognizing the speech:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
void RecognizeSpeech(object sender, SpeechRecognizedEventArgs e) { switch (e.Result.Text) { case "game over": GameOver(); break; case "right": if (previousDirection == (int)MOVINGDIRECTION.TORIGHT) direction = (int)MOVINGDIRECTION.DOWNWARDS; else if ((previousDirection == (int)MOVINGDIRECTION.DOWNWARDS)) { direction = (int)MOVINGDIRECTION.TOLEFT; } else if ((previousDirection == (int)MOVINGDIRECTION.TOLEFT)) { direction = (int)MOVINGDIRECTION.UPWARDS; } else { direction = (int)MOVINGDIRECTION.TORIGHT; } break; case "left": if (previousDirection == (int)MOVINGDIRECTION.TORIGHT) direction = (int)MOVINGDIRECTION.UPWARDS; else if ((previousDirection == (int)MOVINGDIRECTION.DOWNWARDS)) { direction = (int)MOVINGDIRECTION.TORIGHT; } else if ((previousDirection == (int)MOVINGDIRECTION.TOLEFT)) { direction = (int)MOVINGDIRECTION.DOWNWARDS; } else { direction = (int)MOVINGDIRECTION.TOLEFT; } break; } previousDirection = direction; } |
In this method we simply set actions for the three speech commands – “left”, “right” and “game over”. The idea is, as explained to reduce the number of commands.
The process was interesting for me, as far as I am a beginner in WPF and I was really happy to find out that man can do really what he wants. The code here proves it!
The game is somehow “challenging” as far as it has a little slowing between the commands and the action of the snake. Thus, this slowing should be taken into account and the commands should be given 1-2 seconds in advance 🙂
A lot of features could be added, but my initial idea was the voice control only.
Here is the whole game code.
Here you may find the exe file only.
Enjoy the code 🙂