我在电脑上创建了这个乒乓球游戏,但我遇到了问题。一切正常,但是每当球碰到其中一堵墙时,它都会显示我超出范围错误,即使我已将其设置为无法退出控制台。问题只发生在两堵墙上,其他的工作正常!我也有一个评分系统,如果我不解决这个问题,我就无法检查它是否有效。
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);
}
}
}
}
最佳答案
在 MoveBall
方法内部,您将 SetCursorPosition 参数反转。
第一个是离左边界的距离,第二个是离上边界的距离。
Console.SetCursorPosition(Console.WindowWidth / 2, Console.WindowHeight / 2);
这会导致错误,因为 y 值的最大值为 24,而您通过了 40。
关于c# - 乒乓 - 超出范围异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22434551/