Ejemplo n.º 1
0
        //Function provides ball bounds inside platform
        private void bounceBall()
        {
            try
            {
                if (ball.Bottom > Height)
                {
                    GameData.lifes--;

                    repositionElements();
                    updateItems();

                    GameData.GameStarted = false;
                    GamePlatformTimer_Tick.Start();

                    if (GameData.lifes == 0)
                    {
                        GamePlatformTimer_Tick.Stop();
                        MessageBox.Show("Game over");
                        ConnectionDB.ExecuteNonQuery($"UPDATE PLAYER set score = {GameData.score} " +
                                                     $"where name = '{PlayerName}'");
                        ConnectionDB.ExecuteNonQuery($"INSERT INTO SCOREBOARD(name,score,dategame) " +
                                                     $"VALUES ('{PlayerName}',{GameData.score},NOW())");

                        this.Close();
                    }
                }

                if (ball.Left < 0 || ball.Right > Width)
                {
                    GameData.dirX = -GameData.dirX;
                    return;
                }

                if (ball.Top < 0)
                {
                    GameData.dirY = -GameData.dirY;
                    return;
                }

                if (ball.Bounds.IntersectsWith(picPaddle.Bounds))
                {
                    GameData.dirY = -GameData.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))
                        {
                            cpb[i, j].Hits--;

                            if (cpb[i, j].Hits == 0)
                            {
                                //Calling the function to increase the score
                                GameData.score = GameData.score + 150;
                                Controls.Remove(cpb[i, j]);
                                cpb[i, j] = null;
                            }
                            else if (cpb[i, j].Tag.Equals("blinded"))
                            {
                                cpb[i, j].BackgroundImage = Image.FromFile("../../Textures/Tileblindedbroken.png");
                            }

                            GameData.dirY = -GameData.dirY;

                            score.Text = "Score: " + GameData.score;
                            return;
                        }
                    }
                }
            }
            catch (Exception exceptionBallBounds)
            {
                MessageBox.Show("An error has ocurred with the ball bounds");
            }
        }