Пример #1
0
        public void yDetection_DifferentYPositionStarts_ReturnsEqualValue()
        {
            Pong_PCC.Ball ball = new Pong_PCC.Ball(640, 480, 10, 10, 5, 60);

            //Expected values and ball initialized with current settings
            float[] expectedValues = new float[6] {
                126, 34, 58, 150, 242, 334
            };                                                             //Expected Values derived from theory and experimentation


            //yDistance and xDistance constant - always starting from xPosition 618
            ball.YDistance = 4;
            ball.XDistance = 3 * (480 / 365);
            ball.XPosition = 618;


            //Testing different y values
            for (int i = 0; i < expectedValues.Length; i++)
            {
                ball.YPosition = 10 + i * 92;
                ball.yDetection();
                float actualPosition = ball.PredictedYPosition;

                Assert.AreEqual(expectedValues[i], actualPosition);
            }
        }
Пример #2
0
        private void OnTimedEvent(object sender, ElapsedEventArgs e)
        {
            uint distancePerTick = 5; //Amount of y px moved per tick

            //Checks if key is pressed down and moves accordingly
            // Done in timer to avoid keyboard repeat delay when holding
            if (leftPaddle.negativeMovement == true)
            {
                leftPaddle.negativeMove(distancePerTick);
                pboxGameboard.Invalidate();
            }
            else if (leftPaddle.positiveMovement == true)
            {
                leftPaddle.positiveMove(distancePerTick);
                pboxGameboard.Invalidate();
            }

            if (rightPaddle.negativeMovement == true)
            {
                rightPaddle.negativeMove(distancePerTick);
                pboxGameboard.Invalidate();
            }
            else if (rightPaddle.positiveMovement == true)
            {
                rightPaddle.positiveMove(distancePerTick);
                pboxGameboard.Invalidate();
            }



            ball.moveBall(); //Moves the ball each tick

            //Processing  only done when ball moving in the direction of the paddle
            if (leftPaddle.BallDirection == true)
            {
                //Result of whether or not ball has hit paddle (1=hit the paddle, -1= missed the paddle)
                int result = ball.paddleDetection((int)leftPaddle.YPosition);

                //Change the y position of the ball automatically if playing singleplayer
                if (singlePlayer)
                {
                    leftPaddle.moveY(ball.PredictedYPosition, boardSizeY / 240);
                }


                if (result == 1)
                {
                    //Changes the direction the ball is heading towards
                    leftPaddle.BallDirection  = false;
                    rightPaddle.BallDirection = true;
                }
                else if (result == -1)
                {
                    bool regeneration = ball.ballRegenerationCheck(-1); //If missed, wait until ball is 50px beyond edge of panel
                    if (regeneration)
                    {
                        //Change the score of the opposite paddle
                        rightPaddle.Score += 1;

                        //Creates new thread to start the update method
                        Thread labelThread = new Thread(new ThreadStart(updateThread));
                        labelThread.Start();

                        //Finds new y from launch point
                        if (singlePlayer)
                        {
                            ball.yDetection();
                        }
                    }
                }
            }
            else if (rightPaddle.BallDirection == true)
            {
                //Result of whether or not ball has hit paddle (1=hit the paddle, -1= missed the paddle)
                int result = ball.paddleDetection((int)rightPaddle.YPosition);


                if (result == 1)
                {
                    //Changes the projected y position to new point
                    if (singlePlayer == true)
                    {
                        ball.yDetection();
                    }

                    //Changes the direction the ball is heading towards
                    rightPaddle.BallDirection = false;
                    leftPaddle.BallDirection  = true;
                }
                else if (result == -1)
                {
                    bool regeneration = ball.ballRegenerationCheck(1); //If missed, wait until ball is 50px beyond edge of panel
                    if (regeneration)
                    {
                        //Change the score of the opposite paddle
                        leftPaddle.Score += 1;

                        //Creates new thread to start the update method
                        Thread labelThread = new Thread(new ThreadStart(updateThread));
                        labelThread.Start();

                        //Finds new y from launch point
                        if (singlePlayer)
                        {
                            ball.yDetection();
                        }
                    }
                }
            }

            //Add 10ms to the time elapsed
            ball.RoundTimeElapsed += 10;

            ball.sideDetection();       //Check if the ball hit the sides, processing done in the class
            pboxGameboard.Invalidate(); //Redraw the gameboard
        }