private void GamePlatform_Load(object sender, EventArgs e)
        {
            try
            {
                picPaddle.BackgroundImage       = Image.FromFile("../../Textures/Player.jpg");
                picPaddle.BackgroundImageLayout = ImageLayout.Stretch;

                picPaddle.Top  = Height - picPaddle.Height - 80;
                picPaddle.Left = (Width / 2) - (picPaddle.Width / 2);

                ball                       = new PictureBox();
                ball.Width                 = ball.Height = 20;
                ball.BackgroundImage       = Image.FromFile("../../Textures/Ball.png");
                ball.BackgroundImageLayout = ImageLayout.Stretch;

                ball.Top  = picPaddle.Top - ball.Height;
                ball.Left = picPaddle.Left + (picPaddle.Width / 2) - (ball.Width / 2);

                Controls.Add(ball);

                LoadTiles();
                GamePlatformTimer_Tick.Start();
            }
            catch (Exception exceptionGame)
            {
                MessageBox.Show("An error has ocurred");
            }
        }
示例#2
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");
            }
        }