/// <summary> /// Comprueba si el jugador pasado por parámetro ha recibido un tanto /// </summary> /// <param name="rival"></param> /// <param name="ball"></param> /// <returns></returns> private bool checkIfGoalOver(Jugador rival) { bool isInRivalArea = false, isScore = false; switch (rival.getSide()) { case Jugador.Side.Player1: isInRivalArea = ball.getPosition().X - Utilities.BALL_RADIUS < Utilities.MARGIN_OFFSET + Utilities.PADDLE_WIDTH - Utilities.BALL_RADIUS; isScore = ball.getPosition().X - Utilities.BALL_RADIUS < 0; break; case Jugador.Side.Player2: isInRivalArea = ball.getPosition().X + Utilities.BALL_RADIUS > GameCanvas.Width - Utilities.MARGIN_OFFSET - Utilities.PADDLE_WIDTH; isScore = ball.getPosition().X + Utilities.BALL_RADIUS > GameCanvas.Width; break; } if (isInRivalArea) { if (ball.getPosition().Y < (rival.getPaddlePosition().Y + Utilities.PADDLE_HEIGHT + (Utilities.BALL_RADIUS / 2)) && ball.getPosition().Y > (rival.getPaddlePosition().Y - (Utilities.BALL_RADIUS / 2))) { Utilities.Play(Utilities.Sound.Bounce); ball.bounceBallHorizontal(rival, GameCanvas.Height); } else if(isScore){ return true; } } return false; }
/// <summary> /// Rebote horizontal de la pelota. /// Usado cuando la pelota choca contra la barra. /// </summary> /// <param name="player">Jugador contra el que choca</param> /// <param name="canvasHeight">Altura del canvas</param> public void bounceBallHorizontal(Jugador player, double canvasHeight) { fwd.Y = ((player.getPlayerPosition().Y - player.getPaddlePositionPast().Y) / canvasHeight) * 2; fwd.X *= -1; increaseSpeed(); }
/// <summary> /// Función que inicializa las variables del juego /// </summary> private void initializeGame() { GameCanvas.Children.Clear(); newBallTimer = new System.Timers.Timer(); newBallTimer.Elapsed += new ElapsedEventHandler(countdownTick); newBallTimer.Interval = 1000; playerOne = new Jugador(Jugador.Side.Player1); playerOne.initiatePosition(GameCanvas.Width, GameCanvas.Height); playerTwo = new Jugador(Jugador.Side.Player2); playerTwo.initiatePosition(GameCanvas.Width, GameCanvas.Height); ball = new Pelota(); ball.resetBall(GameCanvas.Width, GameCanvas.Height); GameCanvas.Children.Add(ball.getShape()); GameCanvas.Children.Add(playerOne.getPaddleShape()); GameCanvas.Children.Add(playerTwo.getPaddleShape()); GameCanvas.Children.Add(playerOne.getMarkShape()); GameCanvas.Children.Add(playerTwo.getMarkShape()); scoreOneLabel.Content = playerOne.getScore().ToString(); scoreTwoLabel.Content = playerTwo.getScore().ToString(); startThread(); }