/// <summary> /// Handle all the key events. /// </summary> /// <param name="e">The key event.</param> /// <param name="dispatcherTimer">The timer.</param> public void KeyDown(KeyEventArgs e) { try { #region EscapeKey // The pressed key is the Esc key. if (e.Key == Key.Escape) { // Pause the game and send message. gameIsPaused = true; if (MessageBox.Show("Do you want to quit?", "Warning", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { gameInSession = false; viewAction = ViewActionStatus.OpenMenu; } } #endregion EscapeKey #region OptionBindedKeys // The pressed key is one of the binded keys. else if (SpecKeys(e.Key) == GetOption().FireButton || SpecKeys(e.Key) == GetOption().PauseButton || SpecKeys(e.Key) == GetOption().LeftMove || SpecKeys(e.Key) == GetOption().RightMove) { #region MovementKeys // The pressed key is one of the movement keys, the keyboard is controller item and the game is not paused. if ((SpecKeys(e.Key) == GetOption().LeftMove || SpecKeys(e.Key) == GetOption().RightMove) && GetOption().IsKeyboardEnabled && !gameIsPaused) { #region LeftMovement // The pressed key is the left movement key. if (SpecKeys(e.Key) == GetOption().LeftMove) { // There is at least one racket. if (racketList.Count > 0) { // Check each racket. foreach (var oneRacket in racketList) { // The racket is not deleted. if (!oneRacket.IsDeleted) { // Move the racket left. oneRacket.Direction = Racket.Directions.Left; oneRacket.KeyMove(racketSpeed, canvasWidth); // There is at least one ball. if (ballList.Count > 0) { // Check each ball. foreach (var oneBall in ballList) { // The ball is not deleted. if (!oneBall.IsDeleted) { // The ball is on the racket. if (!oneBall.BallInMove) { // Move the ball with the racket left if ball is not moving. oneBall.KeyMove(canvasWidth, oneRacket); } } } } } } } } #endregion LeftMovement #region RightMovement // The pressed key is the right movement key. else if (SpecKeys(e.Key) == GetOption().RightMove) { // There is at least one racket. if (racketList.Count > 0) { // Check each racket. foreach (var oneRacket in racketList) { // The racket is not deleted. if (!oneRacket.IsDeleted) { // Move the racket right. oneRacket.Direction = Racket.Directions.Right; oneRacket.KeyMove(racketSpeed, canvasWidth); // There is at least one ball. if (ballList.Count > 0) { // Check each ball. foreach (var oneBall in ballList) { // The ball is not deleted. if (!oneBall.IsDeleted) { // The ball is on the racket. if (!oneBall.BallInMove) { // Move the ball with the racket right if ball is not moving. oneBall.KeyMove(canvasWidth, oneRacket); } } } } } } } } #endregion RightMovement } #endregion MovementKeys #region FireKey // The pressed key is the fire key. else if (SpecKeys(e.Key) == GetOption().FireButton) { // The keyboard is a controller item and the game didn't begin yet. if (GetOption().IsKeyboardEnabled && !gameInSession) { // The game didn't start yet. if (!gameInSession) { // Start the game. gameInSession = true; } // There is at least one ball. if (ballList.Count > 0) { bool oneGo = false; int iteratorCounter = 0; // Move the first ball. while (!oneGo && iteratorCounter < ballList.Count) { // Start a ball. if (!ballList[iteratorCounter].BallInMove) { ballList[iteratorCounter].BallInMove = true; oneGo = true; } iteratorCounter++; } } } } #endregion FireKey #region PauseKey // The pressed key is the pause key. else if (SpecKeys(e.Key) == GetOption().PauseButton) { // The game is not paused and the game has started. if (!gameIsPaused && gameInSession) { // Pause the game. gameIsPaused = true; } // The game is paused and the game has started. else if (gameIsPaused && gameInSession) { // Continue the game. gameIsPaused = false; } } #endregion PauseKey } #endregion OptionBindedKeys } catch (Exception error) { errorLogViewModel.LogError(error); } }
/// <summary> /// Handles the end of the game. /// </summary> /// <param name="status">The status.</param> private void GameOver(string status) { try { #region Fail // The game ended in failure. if (status == "fail") { MessageBox.Show("You've failed.", "Game Over"); gameIsOver = false; gameOverStatus = null; viewAction = ViewActionStatus.OpenHighscores; } #endregion Fail #region Success // The game ended in success. else if (status == "success") { gameIsOver = false; gameOverStatus = null; #region NotLastMap if (GetOption().MapNumber < mapMaxNumber) { #region Continue // The map has been cleared and the player chose to continue. if (MessageBox.Show("You've succeeded. \n Would you like to continue.", "Game Over", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { // Increment the map's number. optionsViewModel.OptionModel.MapNumber += 1; optionsViewModel.SaveToXml(); viewAction = ViewActionStatus.NewMap; } #endregion Constinue #region Exit // The map has been cleared and the player chose to exit. else { viewAction = ViewActionStatus.OpenHighscores; } #endregion Exit } #endregion NotLastMap #region LastMap // This was the last map. else { MessageBox.Show("You've succeeded.", "Game Over"); viewAction = ViewActionStatus.OpenHighscores; } #endregion LastMap } #endregion Success } catch (Exception e) { errorLogViewModel.LogError(e); } }
/// <summary> /// Presets the values. /// </summary> private void PresetValues1() { try { #region PresetValues // Set the values for the functionality fileds. viewAction = ViewActionStatus.DoNothing; playerLife = 3; playerScorePoint = 0; gameInSession = false; gameIsPaused = false; gameIsOver = false; gameOverStatus = ""; currentMapPath = "notfound"; bonusSpeed = 1; mapMaxNumber = 5; ballHorizontalMovement = 5; ballVerticalMovement = 5; racketSpeed = 20; ballSpeed = 1; speedScale = 1; brickWidth = 27.7; brickHeight = 8; racketWidth = 80; racketHeight = 8; bonusWidth = 24; bonusHeight = 24; ballRadius = 5; ballMinRadius = 3; ballMaxRadius = 15; ballExaminationProximity = 4; horizontalScaleNumber = 1; verticalScaleNumber = 1; racketMaxSize = 160; racketMinSize = 40; racketDifference = 16; mediaPlayer = new MediaPlayer(); rnd = new Random(); // Initialize the lists. ballList = new ObservableCollection<Ball>(); brickList = new ObservableCollection<Brick>(); racketList = new ObservableCollection<Racket>(); bonusList = new ObservableCollection<Bonus>(); #endregion PresetValues #region SetScaling // Set the dimensions scaling based on the resolution. switch (GetOption().Resolution) { case "580x420": horizontalScaleNumber = 0.9; verticalScaleNumber = 0.9; break; case "640x480": horizontalScaleNumber = 1; verticalScaleNumber = 1; break; case "800x600": horizontalScaleNumber = 1.25; verticalScaleNumber = 1.25; break; } // Set the speed scaling based on the difficulty. switch (GetOption().Difficulty) { case 1: speedScale = 0.9; break; case 2: speedScale = 1; break; case 3: speedScale = 1.2; break; } // Set the scaling. bonusWidth *= horizontalScaleNumber; bonusHeight *= verticalScaleNumber; racketWidth *= horizontalScaleNumber; racketHeight *= verticalScaleNumber; brickWidth *= horizontalScaleNumber; brickHeight *= verticalScaleNumber; ballHorizontalMovement *= speedScale; ballVerticalMovement *= speedScale; ballRadius *= horizontalScaleNumber; ballMinRadius *= horizontalScaleNumber; ballMaxRadius *= horizontalScaleNumber; bonusSpeed *= speedScale; ballSpeed *= speedScale; racketSpeed *= speedScale; ballExaminationProximity *= horizontalScaleNumber; racketMaxSize *= horizontalScaleNumber; racketMinSize *= horizontalScaleNumber; racketDifference *= horizontalScaleNumber; #endregion SetScaling } catch (Exception e) { errorLogViewModel.LogError(e); } }
/// <summary> /// Sets the values for the map. /// </summary> public void WindowLoaded() { try { // Set the values for the current map. PresetValues2(); // If the map wasn't found then close the window. if (!string.IsNullOrEmpty(currentMapPath) && currentMapPath == "notfound") { MessageBox.Show("Couldn't find the map file.", "Error"); viewAction = ViewActionStatus.OpenMenu; } // The current map path is empty, this should be impossible. else if (string.IsNullOrEmpty(currentMapPath)) { MessageBox.Show("Couldn't find the options xml file.", "Error"); viewAction = ViewActionStatus.OpenMenu; } } catch (Exception e) { errorLogViewModel.LogError(e); } }