/// <summary> /// Checks if the files exist. /// </summary> private void CheckForFiles() { if (!File.Exists(@"..\..\Resources\OptionsSettings.xml")) { if (MessageBox.Show("Couldn't find the xml file for the settings. \n Would you like to create a new with default settings?", "Error", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { XElement mouseElement = new XElement("mouse", "true"); XElement keyboardElement = new XElement("keyboard", "true"); XElement soundElement = new XElement("sound", "true"); XElement resolutionElement = new XElement("resolution", "1024x768"); XElement leftkeyElement = new XElement("leftkey", "Left"); XElement rightkeyElement = new XElement("rightkey", "Right"); XElement firekeyElement = new XElement("firekey", "Space"); XElement pausekeyElement = new XElement("pausekey", "P"); XElement difficultyElement = new XElement("difficulty", "1"); XElement mapElement = new XElement("map", "1"); XAttribute newAttribute = new XAttribute("id", 1); XElement newElements = new XElement("option", newAttribute, mouseElement, keyboardElement, soundElement, resolutionElement, leftkeyElement, rightkeyElement, firekeyElement, pausekeyElement, difficultyElement, mapElement); XElement newOptions = new XElement("Options", newElements); XDocument newDocument = new XDocument(newOptions); newDocument.Save(@"..\..\Resources\OptionsSettings.xml"); } else { MapSelection returnToMap = new MapSelection(); returnToMap.Show(); Close(); } } // If the OptionsSettings xml doesn't exist, then send message. }
/// <summary> /// Handles the Click event of the okButton control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param> private void okButton_Click(object sender, RoutedEventArgs e) { if (!String.IsNullOrEmpty(nameTextBox.Text)) { HighScore.InjectiontoXML(nameTextBox.Text, int.Parse(ScoreLabel.Content.ToString())); HighScore.OrderBy(); MapSelection returnToMapWindow = new MapSelection(); returnToMapWindow.Show(); Close(); } }
/// <summary> /// Handles the Loaded event of the Window control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param> private void Window_Loaded(object sender, RoutedEventArgs e) { if (!String.IsNullOrEmpty(mapName) && mapName == "notfound") { MessageBox.Show("Couldn't find the map file.", "Error"); MapSelection returnToMapSelection = new MapSelection(); returnToMapSelection.Show(); Close(); } else if (String.IsNullOrEmpty(mapName)) { MessageBox.Show("Couldn't find the options xml file.", "Error"); MapSelection returnToMapSelection = new MapSelection(); returnToMapSelection.Show(); Close(); } // If no map or no options file was found then close the window. lifePoint = gamePresets != null && gamePresets.LifePoint != 0 ? gamePresets.LifePoint : 3; scoreValue = gamePresets != null && gamePresets.ScorePoint != 0 ? gamePresets.ScorePoint : 0; ScoreLabel.Content = "Score: " + scoreValue; // Show the scorepoints. LifeLabel.Content = "Life: " + lifePoint; // Show the lifepoints. TimeLabel.Content = "Time: " + timeOfGame.ToString("HH:mm:ss"); // Show the time. }
/// <summary> /// Handles the end of the game. /// </summary> /// <param name="status">The status.</param> private void GameOver(string status) { if (status == "fail") { movingTimer.Stop(); MessageBox.Show("You've failed.", "Game Over"); gameOver = false; gameOverStatus = null; if (CheckHighScores(scoreValue)) { HighScores(scoreValue); Close(); } else { MapSelection returnToMap = new MapSelection(); returnToMap.Show(); Close(); } } else if (status == "success") { movingTimer.Stop(); MessageBox.Show("You've succeeded.", "Game Over"); gameOver = false; gameOverStatus = null; if (optionsSettings.MapNumber < mapMaxNumber) { if (MessageBox.Show("You've succeeded. \n Would you like to continue.", "Game Over", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { try { XDocument settingsFromXml = XDocument.Load(@"..\..\Resources\OptionsSettings.xml"); var readDataFromXml = settingsFromXml.Descendants("option"); var fromXml = from x in readDataFromXml select x; // Load the values stored in the xml. fromXml.Single().Element("map").Value = (optionsSettings.MapNumber + 1).ToString(); // Sets the number of the map to the xml for later use. settingsFromXml.Save(@"..\..\Resources\OptionsSettings.xml"); // Save the changes in the values of the xml. } catch { } Drawer newMap = new Drawer(); newMap.gamePresets = new GamePresets(lifePoint, scoreValue); newMap.Show(); Close(); } else { if (CheckHighScores(scoreValue)) { HighScores(scoreValue); Close(); } else { MapSelection returnToMap = new MapSelection(); returnToMap.Show(); Close(); } } } else { if (CheckHighScores(scoreValue)) { HighScores(scoreValue); Close(); } else { MapSelection returnToMap = new MapSelection(); returnToMap.Show(); Close(); } } } }
/// <summary> /// Handles the KeyDown event of the Window control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="KeyEventArgs"/> instance containing the event data.</param> private void Window_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Escape) { // Pause the game and send message. movingTimer.Stop(); gameIsPaused = true; TimeLabel.Content = "The game is paused."; if (MessageBox.Show("Do you want to quit?", "Warning", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { MapSelection returnToMapSelection = new MapSelection(); returnToMapSelection.Show(); Close(); } } else if (LookUpKey(e.Key) == optionsSettings.FireKey || LookUpKey(e.Key) == optionsSettings.PauseKey || LookUpKey(e.Key) == optionsSettings.LeftKey || LookUpKey(e.Key) == optionsSettings.RightKey) { if ((LookUpKey(e.Key) == optionsSettings.LeftKey || LookUpKey(e.Key) == optionsSettings.RightKey) && optionsSettings.KeyboardIsOn && !gameIsPaused) { if (LookUpKey(e.Key) == optionsSettings.LeftKey) { // Move the racket left. if (racketList.Count > 0) { foreach (var oneRacket in racketList) { oneRacket.MoveKey(racketHorizontalMovement, "left", canvasLayer); if (ballList.Count > 0) { foreach (var oneBall in ballList) { if (!oneBall.BallInMove) { // Move the ball with the racket left if ball is not moving. oneBall.MoveKey(racketHorizontalMovement, "left", canvasLayer, oneRacket); } } } } } } else if (LookUpKey(e.Key) == optionsSettings.RightKey) { // Move the racket right. if (racketList.Count > 0) { foreach (var oneRacket in racketList) { oneRacket.MoveKey(racketHorizontalMovement, "right", canvasLayer); if (ballList.Count > 0) { foreach (var oneBall in ballList) { if (!oneBall.BallInMove) { // Move the ball with the racket right if ball is not moving. oneBall.MoveKey(racketHorizontalMovement, "right", canvasLayer, oneRacket); } } } } } } RefreshCanvas(); } else if (LookUpKey(e.Key) == optionsSettings.FireKey) { if (optionsSettings.KeyboardIsOn && !gameIsPaused && !gameInSession) { if (!movingTimer.IsEnabled) { // Start the game. movingTimer.Start(); gameInSession = true; } if (ballList.Count > 0) { bool oneGo = false; int iteratorCounter = 0; while (!oneGo && iteratorCounter < ballList.Count) { // Start a ball. if (!ballList[iteratorCounter].BallInMove) { ballList[iteratorCounter].BallInMove = true; oneGo = true; } iteratorCounter++; } } } } else if (LookUpKey(e.Key) == optionsSettings.PauseKey) { // Pause the game. if (!gameIsPaused) { movingTimer.Stop(); gameIsPaused = true; TimeLabel.Content = "The game is paused."; } else { movingTimer.Start(); gameIsPaused = false; TimeLabel.Content = "Time: " + timeOfGame.ToString("HH:mm:ss"); } } // Process the control keys. } }
/// <summary> /// Handles the Click event of the cancelButton control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param> private void cancelButton_Click(object sender, RoutedEventArgs e) { MapSelection returnToMapWindow = new MapSelection(); returnToMapWindow.Show(); Close(); }
/// <summary> /// Handles the Click event of the btnNewGame control wich navigates to the map selection window. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param> private void btnNewGame_Click(object sender, RoutedEventArgs e) { MapSelection mapSelectionWindow = new MapSelection(); mapSelectionWindow.Show(); Close(); }