示例#1
0
        public void paddleDetection_TouchingPaddleSituations_ReturnsOne()
        {
            Pong_PCC.Ball ball = new Pong_PCC.Ball(640, 480, 10, 10, 5, 60);

            int[] paddleYPositions = new int[3] {
                0, 210, 420
            };
            int[] ballYPositions = new int[] { 30, 220, 470 };


            ball.XPosition = 615;
            for (int i = 0; i < paddleYPositions.Length; i++)
            {
                ball.YPosition = ballYPositions[i];
                int actual = ball.paddleDetection(paddleYPositions[i]);

                Assert.AreEqual(1, actual);
            }

            ball.XPosition = 15;
            for (int i = 0; i < paddleYPositions.Length; i++)
            {
                ball.YPosition = ballYPositions[i];
                int actual = ball.paddleDetection(paddleYPositions[i]);

                Assert.AreEqual(1, actual);
            }
        }
示例#2
0
        public void paddleDetection_NotTouchingPaddleSituations_ReturnsZero()
        {
            Pong_PCC.Ball ball = new Pong_PCC.Ball(640, 480, 10, 10, 5, 60);

            int[] paddleYPositions = new int[5] {
                0, 100, 200, 300, 420
            };
            int[] ballYPositions = new int[5] {
                30, 130, 230, 330, 450
            };


            for (int i = 0; i < paddleYPositions.Length; i++)
            {
                ball.XPosition = 20 + 147.5f * i;
                ball.YPosition = ballYPositions[i];
                int actual = ball.paddleDetection(paddleYPositions[i]);
                Assert.AreEqual(0, actual);
            }
        }
示例#3
0
        public void paddleDetection_MissingPaddleSituations_ReturnsNegativeOne()
        {
            Pong_PCC.Ball ball = new Pong_PCC.Ball(640, 480, 10, 10, 5, 60);

            //Sitations where ball is below and above paddle, and past paddle
            int[] paddleYPositions = new int[4] {
                0, 210, 420, 0
            };
            int[] ballYPositions = new int[4] {
                70, 10, 470, 30
            };
            int[] ballXPositions = new int[4] {
                615, 15, 640, 0
            };


            for (int i = 0; i < paddleYPositions.Length; i++)
            {
                ball.XPosition = ballXPositions[i];
                ball.YPosition = ballYPositions[i];
                int actual = ball.paddleDetection(paddleYPositions[i]);
                Assert.AreEqual(-1, actual);
            }
        }
示例#4
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
        }