gpt4 book ai didi

c# - Ping Pong - 超出范围异常

转载 作者:太空狗 更新时间:2023-10-30 01:36:21 24 4
gpt4 key购买 nike

我在电脑上创建了这个 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);
}
}
}
}

最佳答案

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

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

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

关于c# - Ping Pong - 超出范围异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22434551/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com