Skip to content

Instantly share code, notes, and snippets.

@pmrozik
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmrozik/dd7ba2d12716196cb4a5 to your computer and use it in GitHub Desktop.
Save pmrozik/dd7ba2d12716196cb4a5 to your computer and use it in GitHub Desktop.
Play method for WordUp
private void Play(GameTime gameTime)
{
// Allows user to enter words
KeyboardState keyboard = Keyboard.GetState();
char pressedChar = KeyboardProcessor.GetLetter(keyboard);
if (pressedChar != ' ')
{
// Check whether the letter is one of the falling ones
if (currentWord.Contains(pressedChar.ToString()))
{
keyPressSound.Play();
typedLetters.Add(pressedChar);
}
else
{
errorSound.Play();
Debug.WriteLine("Current word: {0}", currentWord);
Debug.WriteLine("Current word doesn't have {0}", pressedChar);
}
}
// User presses enter, check if word exists in dictionary
if (typedLetters.Count > 0 && keyboard.IsKeyDown(Keys.Enter))
{
enterDown = true;
}
if (enterDown && keyboard.IsKeyUp(Keys.Enter))
{
string word = "";
foreach (char c in typedLetters)
{
word += c;
}
Debug.Write("Checking the following word: {0} ...", word);
// Check if word exists in dictionary
if (wordDictionary.ContainsKey(word))
{
Debug.WriteLine("found.");
// 1. Remove word from word dictionary
wordDictionary.Remove(word);
// 2. Update score
score += ScoreTools.GetWordScore(word);
Debug.WriteLine("Total score: {0}", score);
wordSuccessSound.Play();
// 3. Clear current letters
clearLetters();
}
else
{
// Word not found in dictionary, play error sound
errorSound.Play();
// Clear typed letters
typedLetters.Clear();
Debug.WriteLine("not found.");
}
enterDown = false;
}
// Remove one letter with backspace
if (keyboard.IsKeyDown(Keys.Back))
{
backspaceDown = true;
}
if (backspaceDown && (typedLetters.Count > 0) && !shiftBackspaceDown)
{
if (keyboard.IsKeyUp(Keys.Back))
{
letterDeleteSound.Play();
typedLetters.RemoveAt(typedLetters.Count - 1);
backspaceDown = false;
}
}
// Left shift + backspace pressed to delete entire typed word
if (keyboard.IsKeyDown(Keys.LeftShift) && keyboard.IsKeyDown(Keys.Back))
{
shiftBackspaceDown = true;
}
if (shiftBackspaceDown && (typedLetters.Count > 0))
{
if (keyboard.IsKeyUp(Keys.Back) || keyboard.IsKeyUp(Keys.LeftShift))
{
wordDeleteSound.Play();
typedLetters.Clear();
shiftBackspaceDown = false;
}
}
// Allows user to change the speed of the game via keyboard
if (keyboard.IsKeyDown(Keys.Up))
{
keyDownPressed = true;
}
if (keyDownPressed == true)
{
if (keyboard.IsKeyUp(Keys.Up))
{
if (gameSpeed != GameSpeed.ULTRA_FAST)
{
gameSpeed++;
}
else
{
gameSpeed = GameSpeed.VERY_SLOW;
}
foreach (Letter letter in wordLetterList)
{
letter.Speed = gameSpeed;
}
keyDownPressed = false;
Debug.WriteLine("Game speed changed to " + gameSpeed.ToString());
}
}
foreach (Letter letter in wordLetterList)
{
if (letter.OffScreen)
{
offScreen = true;
}
letter.Update(gameTime);
}
// Check if letters have gone off screen
if (offScreen)
{
clearLetters();
offScreen = false;
// Life lost
if (livesList.Count > 0)
{
livesList.RemoveAt(livesList.Count - 1);
lifeLostSound.Play();
}
// All lives lost, game over
else
{
currentGameState = GameState.GameOver;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment