private void rebotarPelota() { if (Ball.Top < Score.Height) { DatosJuego.dirY = -DatosJuego.dirY; } if (Ball.Bottom > Height) { DatosJuego.vidas--; DatosJuego.juegoIniciado = false; gameTimer.Stop(); ReposicionarElementos(); ActualizarElementos(); if (DatosJuego.vidas == 0) { gameTimer.Stop(); TerminarJuego?.Invoke(); } } if (Ball.Left < 0 || Ball.Right > Width) { DatosJuego.dirX = -DatosJuego.dirX; return; } if (Ball.Bounds.IntersectsWith(player.Bounds)) { DatosJuego.dirY = -DatosJuego.dirY; } for (int i = 4; i >= 0; i--) { for (int j = 0; j < 10; j++) { if (cpb[i, j] != null && Ball.Bounds.IntersectsWith(cpb[i, j].Bounds)) { if (cpb[i, j] != null && i == 4) { DatosJuego.puntajes += cpb[i, j].Hits; cpb[i, j].Hits--; } else { DatosJuego.puntajes += (int)(cpb[i, j].Hits * DatosJuego.ticksRealizados); cpb[i, j].Hits--; } if (cpb[i, j].Hits == 0) { Controls.Remove(cpb[i, j]); cpb[i, j] = null; remainingPb--; } else if (cpb[i, j].Tag.Equals("blinded")) { cpb[i, j].BackgroundImage = Image.FromFile("../../../Sprites/broken.png"); } DatosJuego.dirY = -DatosJuego.dirY; Puntajes.Text = DatosJuego.puntajes.ToString(); if (remainingPb == 0) { DatosJuego.puntajes = DatosJuego.puntajes * DatosJuego.vidas; //El if que suma una vida se agrega porque la pelota sigue rebotando al destruir el ultimo bloque //entonces esto evita que se pierda el juego ReposicionarElementos(); gameTimer.Stop(); WinningGame?.Invoke(); } return; } } } }
// Rutina de rebote de pelota private void BounceBall() { // Rebote en la parte superior if (ball.Top < scorePanel.Height) { GameData.dirY = -GameData.dirY; return; } // Pelota se sale de los bounds if (ball.Bottom > Height) { throw new OutOfBoundsException(""); } // Rebote en lado derecho o izquierdo de la ventana if (ball.Left < 0 || ball.Right > Width) { GameData.dirX = -GameData.dirX; return; } // Rebote con el jugador if (ball.Bounds.IntersectsWith(playerPb.Bounds)) { GameData.dirY = -GameData.dirY; return; } // Rutina de colisiones con cpb for (int i = 4; i >= 0; i--) { for (int j = 0; j < 10; j++) { if (cpb[i, j] != null && ball.Bounds.IntersectsWith(cpb[i, j].Bounds)) { GameData.score += (int)(cpb[i, j].Hits * GameData.ticksCount); cpb[i, j].Hits--; if (cpb[i, j].Hits == 0) { Controls.Remove(cpb[i, j]); cpb[i, j] = null; remainingPb--; } else if (cpb[i, j].Tag.Equals("blinded")) { cpb[i, j].BackgroundImage = Image.FromFile("../../Img/tb2.png"); } GameData.dirY = -GameData.dirY; score.Text = GameData.score.ToString(); if (remainingPb == 0) { WinningGame?.Invoke(); } return; } } } }
//Metodo que se encarga de revisar las colisiones private void Bounces() { #region Seteando sonidos //seteando sonido de caida, rebote y hit System.Media.SoundPlayer fall = new System.Media.SoundPlayer ("../../Resources/Fall.wav"); System.Media.SoundPlayer bounce = new System.Media.SoundPlayer ("../../Resources/Bounce.wav"); System.Media.SoundPlayer hit = new System.Media.SoundPlayer ("../../Resources/Hit.wav"); #endregion //Random para el rebote Random random = new Random(); #region Cargado de vidas //verifica si la pelota cae al vacio if (ball.Bottom > Height) { fall.Play(); GameData.life--; tmrGame.Stop(); switch (GameData.life) { case 2: life3.Image = Image.FromFile("../../Resources/heartn.png"); Relocation(); break; case 1: life2.Image = Image.FromFile("../../Resources/heartn.png"); Relocation(); break; case 0: life1.Image = Image.FromFile("../../Resources/heartn.png"); FinishGame?.Invoke(); break; } } #endregion #region Posibles Rebotes //Genera un rebote en la parte superior if (ball.Top < scorePnl.Bottom) { bounce.Play(); GameData.ySpeed = -GameData.ySpeed; } //Condicion para que rebote cuando colisione con el borde if (ball.Left < 0 || ball.Right > Width) { bounce.Play(); GameData.xSpeed = -GameData.xSpeed; return; } // Condicion para que la pelota rebote con la plataforma if (ball.Bounds.IntersectsWith(plataform.Bounds)) { bounce.Play(); ball.Top -= (ball.Bottom - plataform.Top); GameData.ySpeed = -random.Next(12, 20); return; } //Condicion para conocer si el bloque esta activo y existen colisiones if (Collisions()) { hit.Play(); GameData.ySpeed = -GameData.ySpeed; //Cambia la direccion al chocar if (GameData.remainingBlocks == 0) { WinningGame?.Invoke(); } } #endregion }
// rebote de la pelota private void RebounceBall() { if (ball.Top < Scores.Height) { DataGame.dirY = -DataGame.dirY; } if (ball.Bottom > Height) { DataGame.lifes--; DataGame.GameStart = false; tmrGame.Stop(); ReloadElements(); UpdateElements(); if (DataGame.lifes == 0) { tmrGame.Stop(); EndGame?.Invoke(); this.Dispose(); Controls.Remove(this); } } if (ball.Left < 0 || ball.Right > Width) { DataGame.dirX = -DataGame.dirX; return; } if (ball.Bounds.IntersectsWith(picPlayerBar.Bounds)) { DataGame.dirY = -DataGame.dirY; } // Coliciones con los bloques for (int i = 4; i >= 0; i--) { for (int j = 0; j < 10; j++) { if (cpb[i, j] != null && ball.Bounds.IntersectsWith(cpb[i, j].Bounds)) { if (cpb[i, j].Hits == 3) { DataGame.score += (3 * DataGame.lifes); cpb[i, j].Hits--; // cambio de imagen de fondo segun los golpes cpb[i, j].BackgroundImage = Image.FromFile("../../Resources/Brick_Lvl3_Semibroken.jpg"); } else if (cpb[i, j].Hits == 2) { DataGame.score += (3 * DataGame.lifes); cpb[i, j].Hits--; // cambio de imagen de fondo segun los golpes if (cpb[i, j].Tag.Equals("blockLevel3")) { cpb[i, j].BackgroundImage = Image.FromFile("../../Resources/Brick_Lvl3_Broken.jpg"); } else if (cpb[i, j].Tag.Equals("blockLevel2")) { cpb[i, j].BackgroundImage = Image.FromFile("../../Resources/Brick_Lvl2_Broken.jpg"); } } else if (cpb[i, j].Hits == 1) { DataGame.score += (3 * DataGame.lifes); cpb[i, j].Hits--; } if (cpb[i, j].Hits == 0) { Controls.Remove(cpb[i, j]); cpb[i, j] = null; remainingBlocks--; } DataGame.dirY = -DataGame.dirY; Points.Text = DataGame.score.ToString(); if (remainingBlocks == 0) { tmrGame.Stop(); WinningGame?.Invoke(); this.Dispose(); Controls.Remove(this); } return; } } } }
private void BounceBall() { if (picBall.Top < pnlScore.Height) { GameData.dirY = -GameData.dirY; return; } //Si la pelota toca la parte inferior del control, el usuario ha perdido if (picBall.Bottom > Height) { throw new OutOfBoundsException(""); } //Si la pelota toca los bordes izq o der, rebota if (picBall.Left < 0 || picBall.Right > Width) { GameData.dirX = -GameData.dirX; return; } //Rebotar la pelota cuando choca con la barra del jugador if (picBall.Bounds.IntersectsWith(picPaddle.Bounds) || picBall.Top < 0) { GameData.dirY = -GameData.dirY; return; } //la variable i se disminuye ya que se esta iniciando desde la fila mas cercana a la pelota //esto se hace para hacer el menor numero de verificaciones posible for (int i = 5; i >= 0; i--) { for (int j = 0; j < 10; j++) { //Cuando la pelota choca con un bloque, este se elimina y la pelota rebota // y se mueve hacia el otro lado if (cpb[i, j] != null && picBall.Bounds.IntersectsWith(cpb[i, j].Bounds)) { if (i == 0) { cpb[i, j].BackgroundImage = Image.FromFile("../../Resources/" + GenerateRandomNumber() + ".png"); } // Actuaiza los puntos en base a los golpes y el tiempo GameData.score += (int)(cpb[i, j].Hits * GameData.performedTicks); cpb[i, j].Hits--; if (cpb[i, j].Hits == 0) { Controls.Remove(cpb[i, j]); cpb[i, j] = null; remainingPb--; } GameData.dirY = -GameData.dirY; lblScore.Text = GameData.score.ToString(); // Si ya no quedan bloques, entonces de detiene el timer, se reposicionan los elementos y el jugador ha ganado if (remainingPb == 0) { timer1.Stop(); RepositionElements(); WinningGame?.Invoke(); } return; } } } }