示例#1
0
        //the function that runs every tick while the game is running
        private void RunGame()
        {
            //reset ball and remove a life when ball passes paddle on the left side
            if (ball.GetPosition().X < -20)
            {
                redPlayer.SetLives(redPlayer.GetLives() - 1);
                ResetBall(true);

                //if out of lives
                if (redPlayer.GetLives() < 1)
                {
                    currentState = GameStates.GameOver;
                    redPlayer.SetLives(3);
                    bluePlayer.SetLives(3);
                }
            }

            //reset ball and remove a life when ball passes paddle on the right side
            if (ball.GetPosition().X > 920)
            {
                bluePlayer.SetLives(bluePlayer.GetLives() - 1);
                ResetBall(false);

                //if out of lives
                if (bluePlayer.GetLives() < 1)
                {
                    currentState = GameStates.GameOver;
                    redPlayer.SetLives(3);
                    bluePlayer.SetLives(3);
                }
            }

            //set the current keyboard state
            KeyboardState state = Keyboard.GetState();

            //RED PLAYER UP
            if (state.IsKeyDown(Keys.W) && redPlayer.GetPosition().Y > 0)
            {
                redPlayer.SetPosition(new Vector2(0, (float)(redPlayer.GetPosition().Y - st.paddle_speed)));
                has_moved = true;
            }
            //RED PLAYER DOWN
            if (state.IsKeyDown(Keys.S) && redPlayer.GetPosition().Y < 600 - redPlayer.Height)
            {
                redPlayer.SetPosition(new Vector2(0, (float)(redPlayer.GetPosition().Y + st.paddle_speed)));
                has_moved = true;
            }
            //BLUE PLAYER UP
            if (state.IsKeyDown(Keys.Up) && bluePlayer.GetPosition().Y > 0)
            {
                bluePlayer.SetPosition(new Vector2(900 - bluePlayer.Width, (float)(bluePlayer.GetPosition().Y - st.paddle_speed)));
                has_moved = true;
            }
            //BLUE PLAYER DOWN
            if (state.IsKeyDown(Keys.Down) && bluePlayer.GetPosition().Y < 600 - bluePlayer.Height)
            {
                bluePlayer.SetPosition(new Vector2(900 - bluePlayer.Width, (float)(bluePlayer.GetPosition().Y + st.paddle_speed)));
                has_moved = true;
            }

            //change ball position
            if (has_moved)
            {
                ball.SetPosition(Vector2.Add(ball.GetPosition(), Vector2.Multiply(ball.GetSpeed(), (float)st.ball_defaultspeed * st.bounce_speed)));
            }

            //check boundaries of ball position vertically
            if (ball.GetPosition().Y >= 600 - ball.Height || ball.GetPosition().Y <= 0)
            {
                ball.InvertY();
            }

            //bounce on the red paddle
            if (player_turn == false && ball.GetPosition().X < redPlayer.Width && ball.GetPosition().Y > redPlayer.GetPosition().Y - ball.Height && ball.GetPosition().Y < redPlayer.GetPosition().Y + redPlayer.Height)
            {
                //calculate new direction
                double DistanceToMiddle   = (redPlayer.GetPosition().Y + redPlayer.Height / 2) - (ball.GetPosition().Y + ball.Height / 2);
                double normalizedDistance = DistanceToMiddle / (redPlayer.Height / 2);

                //set new direction
                ball.SetSpeed(new Vector2((float)Math.Cos(normalizedDistance), (float)-Math.Sin(normalizedDistance)));

                //switch player turn
                player_turn = !player_turn;

                //speed up the ball on bounce
                st.bounce_speed *= st.bounce_increase;
            }

            //bounce on the blue paddle
            if (player_turn == true && ball.GetPosition().X > 900 - bluePlayer.Width - ball.Width && ball.GetPosition().X < 900 && ball.GetPosition().Y > bluePlayer.GetPosition().Y - ball.Height && ball.GetPosition().Y < bluePlayer.GetPosition().Y + bluePlayer.Height)
            {
                //calculate new direction
                double DistanceToMiddle   = (bluePlayer.GetPosition().Y + bluePlayer.Height / 2) - (ball.GetPosition().Y + ball.Height / 2);
                double normalizedDistance = DistanceToMiddle / (bluePlayer.Height / 2);

                //set new direction
                ball.SetSpeed(new Vector2((float)-Math.Cos(normalizedDistance), (float)-Math.Sin(normalizedDistance)));

                //switch player turn
                player_turn = !player_turn;

                //speed up the ball on bounce
                st.bounce_speed *= st.bounce_increase;
            }
        }