Question

I created this Ping Pong game against a computer but I have a problem. Everything works fine but whenever the ball touches one of the walls it shows me out of range error, even though I have set it so it can't exit the console. The problem only happens on two of the walls and the others work fine! I also have a score system, and I can't check if it works if I don't fix that problem.

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);
            }
        }
    }
}
Was it helpful?

Solution

Inside the method MoveBall, you have the SetCursorPosition arguments inverted.
The first one is the distance from the left border and the second one the distance from the top border.

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

This causes the error because the max value for the y value is 24 and you pass 40.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top