Esempio n. 1
0
 private void Form1_KeyDown(object sender, KeyEventArgs e)
 {
     if (e.KeyCode == Keys.A)
     {
         if (!paddle1.boundingBox.IntersectsWith(bottomSide))
         {
             paddle1.MovePaddleDown(PADDLE_SPEED);
         }
     }
     else if (e.KeyCode == Keys.Q)
     {
         if (!paddle1.boundingBox.IntersectsWith(topSide))
         {
             paddle1.MovePaddleUp(PADDLE_SPEED);
         }
     }
 }
Esempio n. 2
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (ball.boundingBox.IntersectsWith(paddle1.boundingBox))
            {
                Point offset = ball.Bounce();
                xVelocity = xVelocity * -1;
                ball.MoveBall(4 * xVelocity + offset.X, 4 * yVelocity + offset.Y);
            }
            else if (ball.boundingBox.IntersectsWith(paddle2.boundingBox))
            {
                Point offset = ball.Bounce();
                xVelocity = xVelocity * -1;
                ball.MoveBall(4 * xVelocity + offset.X, 4 * yVelocity + offset.Y);
            }
            else if (ball.boundingBox.IntersectsWith(rightSide))
            {
                if (xVelocity > 0)
                {
                    Point offset = ball.Bounce();
                    xVelocity = xVelocity * -1;
                    ball.MoveBall(4 * xVelocity + offset.X, 4 * yVelocity + offset.Y);
                    scoreKeeper.Player1Goal();

                    Player1Score.Text = scoreKeeper.player1Score.ToString();
                    if (scoreKeeper.player1Score == 15)
                    {
                        resetGame();
                    }
                    else
                    {
                        ball.Reset();
                    }
                }
            }
            else if (ball.boundingBox.IntersectsWith(leftSide))
            {
                if (xVelocity < 0)
                {
                    Point offset = ball.Bounce();
                    xVelocity = xVelocity * -1;
                    ball.MoveBall(4 * xVelocity + offset.X, 4 * yVelocity + offset.Y);
                    scoreKeeper.Player2Goal();
                    Player2Score.Text = scoreKeeper.player2Score.ToString();
                    if (scoreKeeper.player2Score == 15)
                    {
                        resetGame();
                    }
                    else
                    {
                        ball.Reset();
                    }
                }
            }
            else if (ball.boundingBox.IntersectsWith(bottomSide))
            {
                if (yVelocity > 0)
                {
                    Point offset = ball.Bounce();
                    yVelocity = yVelocity * -1;
                    ball.MoveBall(4 * xVelocity + offset.X, 4 * yVelocity + offset.Y);
                }
            }
            else if (ball.boundingBox.IntersectsWith(topSide))
            {
                if (yVelocity < 0)
                {
                    Point offset = ball.Bounce();
                    yVelocity = yVelocity * -1;
                    ball.MoveBall(4 * xVelocity + offset.X, 4 * yVelocity + offset.Y);
                }
            }
            if (paddle2.boundingBox.Y < ball.currentLocation.Y)
            {
                if (!paddle2.boundingBox.IntersectsWith(bottomSide))
                {
                    paddle2.MovePaddleDown(PADDLE_SPEED);
                }
            }
            else if (paddle2.boundingBox.Y > ball.currentLocation.Y)
            {
                if (!paddle2.boundingBox.IntersectsWith(topSide))
                {
                    paddle2.MovePaddleUp(PADDLE_SPEED);
                }
            }
            ball.MoveBall(2 * xVelocity, 1 * yVelocity);
            this.Refresh();
        }