乒乓球 - 超出范围

时间:2014-03-16 08:12:27

标签: c# exception pong

我在计算机上创建了这个Ping Pong游戏,但我遇到了问题。一切正常,但每当球接触其中一个墙时,它就会显示我超出范围错误,即使我已将其设置为无法退出控制台。问题只发生在两面墙上,而其他墙面工作正常!我也有一个分数系统,如果我不解决这个问题,我无法检查它是否有效。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace Ping_Pong
{
    class Program
    {
        static int firstplayerpadsize = 4;
        static int secondplayerpadsize = 4;
        static int ballpositionx = 0;
        static int ballpositiony = 0;
        static bool balldirectionup = true;
        static bool balldirectionright = false;
        static int firstplayerposition = 0;
        static int secondplayerposition = 0;
        static int firstplayerresult = 0;
        static int secondplayerresult = 0;
        static Random randomGenerator = new Random();

        static void RemoveScrollbars()
        {
             Console.ForegroundColor = ConsoleColor.Yellow;
             Console.BufferHeight = Console.WindowHeight;
             Console.BufferWidth = Console.WindowWidth;
        }

        static void DrawFirstPlayer()
        {
            for (int y = firstplayerposition; y < firstplayerposition + firstplayerpadsize; y++)
            {
                PrintAtPosition(0, y, '|');
                PrintAtPosition(1, y, '|');
            }
        }

        static void PrintAtPosition(int x, int y, char symbol)
        {
            Console.SetCursorPosition(x, y);
            Console.Write(symbol);
        }

        static void DrawSecondPlayer()
        {
            for (int y = secondplayerposition; y < secondplayerposition + secondplayerpadsize; y++)
            {
                PrintAtPosition(Console.WindowWidth - 1, y, '|');
                PrintAtPosition(Console.WindowWidth - 2, y, '|');
            }
        }

        static void SetInitialsPoints()
        {
            firstplayerposition = Console.WindowHeight / 2 - firstplayerpadsize / 2;
            secondplayerposition = Console.WindowHeight / 2 - secondplayerpadsize / 2;
            SetBall();
        }

        static void SetBall()
        {
            ballpositionx = Console.WindowWidth / 2;
            ballpositiony = Console.WindowHeight / 2;
        }

        static void DrawBall()
        {
            PrintAtPosition(ballpositionx, ballpositiony, '@');
        }

        static void PrintResult()
        {
            Console.SetCursorPosition(Console.WindowWidth / 2 - 1, 0);
            Console.WriteLine("{0}-{1}", firstplayerresult, secondplayerresult);
        }

        static void MoveFirstPlayerDown()
        {
            if (firstplayerposition < Console.WindowHeight - firstplayerpadsize)
            {
                firstplayerposition++;
            }
        }

        static void MoveFirstPlayerUp()
        {
            if (firstplayerposition > 0)
            {
                firstplayerposition--;
            }
        }

        static void MoveSecondPlayerDown()
        {
            if (secondplayerposition < Console.WindowHeight - secondplayerpadsize)
            {
                secondplayerposition++;
            }
        }

        static void MoveSecondPlayerUp()
        {
            if (secondplayerposition > 0)
            {
                secondplayerposition--;
            }
        }

        static void SecondPlayerAIMove()
    {
        //int randomNumber = randomGenerator.Next(0, 2);
        //    if (randomNumber == 0)
        //    {
        //        MoveSecondPlayerUp();
        //    }
        //    if (randomNumber == 1)
        //    {
        //        MoveSecondPlayerDown();
        //    }

       // if (randomNumber == 0)
       // {
            if (balldirectionup == true)
            {
                MoveSecondPlayerUp();
            }
            else
            {
                MoveSecondPlayerDown();
            }
        }
   // }

        static void MoveBall()
        {
            if (ballpositiony == 0)
            {
                balldirectionup = false;
            }
            if (ballpositiony == Console.WindowHeight - 1)
            {
                balldirectionup = true;
            }
            if (ballpositionx == Console.WindowWidth - 1)
            {
                SetBall();
                balldirectionright = false;
                balldirectionup = true;
                firstplayerresult++;
                Console.SetCursorPosition(Console.WindowHeight / 2, Console.WindowWidth / 2);
                Console.WriteLine("Score for player 1.");
                Console.ReadKey();
            }
            if (ballpositionx == 0)
            {
                SetBall();
                balldirectionright = true;
                balldirectionup = true;
                secondplayerresult++;
                Console.SetCursorPosition(Console.WindowHeight / 2, Console.WindowWidth / 2);
                Console.WriteLine("Score for player 2.");
                Console.ReadKey();
            }

            if (ballpositionx < 3)
            {
                if (ballpositiony >= firstplayerposition && ballpositiony < firstplayerposition 
                   + firstplayerpadsize)
                {
                    balldirectionright = true;
                }
            }


            if (ballpositionx >= Console.WindowWidth - 3 - 1)
            {
                if (ballpositiony >= secondplayerposition && ballpositiony < secondplayerposition + secondplayerpadsize)
                {
                    balldirectionright = false;
                }
            }
            if (balldirectionup)
            {
                ballpositiony--;
            }
            else
            {
                ballpositiony++;
            }

            if (balldirectionright)
            {
                ballpositionx++;
            }
            else
            {
                ballpositionx--;
            }
        }

        static void Main(string[] args)
        {
            RemoveScrollbars();
            SetInitialsPoints();
            while (true)
            {
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo keyInfo = Console.ReadKey();
                    if (keyInfo.Key == ConsoleKey.UpArrow)
                    {
                        MoveFirstPlayerUp();
                    }
                    if (keyInfo.Key == ConsoleKey.DownArrow)
                    {
                        MoveFirstPlayerDown();
                    }
                }
                SecondPlayerAIMove();
                MoveBall();
                Console.Clear();
                DrawFirstPlayer();
                DrawSecondPlayer();
                DrawBall();
                PrintResult();
                Thread.Sleep(60);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:3)

在方法MoveBall中,您将SetCursorPosition参数反转 第一个是距离左边界的距离,第二个是距离顶边的距离。

Console.SetCursorPosition(Console.WindowWidth / 2, Console.WindowHeight / 2);

这会导致错误,因为y值的最大值为24,而你传递40。