C++ Cout仅在控制台中显示黑色屏幕
为我的考试写“蛇游戏”。我决定让用户选择字段大小,从理论上讲,在这个阶段,它必须显示一些东西,但函数“图形”只显示黑色屏幕。 我猜想,问题可能是当图形引用结构高度和宽度定义时,可能在某种程度上它看不到我更改了这些定义,并将其计为零和零,所以字段的大小分别为零和零。。。不管怎样,我不知道,有人能帮忙吗C++ Cout仅在控制台中显示黑色屏幕,c++,for-loop,struct,cout,C++,For Loop,Struct,Cout,为我的考试写“蛇游戏”。我决定让用户选择字段大小,从理论上讲,在这个阶段,它必须显示一些东西,但函数“图形”只显示黑色屏幕。 我猜想,问题可能是当图形引用结构高度和宽度定义时,可能在某种程度上它看不到我更改了这些定义,并将其计为零和零,所以字段的大小分别为零和零。。。不管怎样,我不知道,有人能帮忙吗 #include <iostream> #include <stdlib.h> bool GameOver; struct GameData { int x{ 0
#include <iostream>
#include <stdlib.h>
bool GameOver;
struct GameData
{
int x{ 0 };
int y{ 0 };
int Width;
int Height;
int AppleX;
int AppleY;
int Score;
};
enum SnakeDirection {Stop = 0, Up, Down, Left, Right};
SnakeDirection Way;
void EnterFieldSize()
{
GameData GMD;
int Width{ 0 };
int Height{ 0 };
std::cout << "Enter field size (Min 10, Max 100): \n";
std::cout << "Width: "; std::cin >> Width;
std::cout << "Height: "; std::cin >> Height;
GMD.Width = Width;
GMD.Height = Height;
if (Width < 10 || Width > 100) //Если ширина меньше 10 или больше 100, повторить ввод
{
std::cout << "Unaccetable width! Enter again:\n ";
return EnterFieldSize();
}
if (Height < 10 || Height > 100)
{
std::cout << "Unaccetable height! Enter again:\n ";
return EnterFieldSize();
}
return;
}
void Settings()
{
EnterFieldSize();
GameData GMD;
Way = Stop;
GMD.x = rand() % GMD.Width; и
GMD.y = rand() % GMD.Height;
GMD.AppleX = rand() % GMD.Height;
GMD.AppleY = rand() % GMD.Width;
GMD.Score = 0 ;
return;
}
void GameLogic()
{
}
void Graphics() //This part doesn't show
{
GameData GMD;
system("cls");
for (int i{ 0 }; i < GMD.Width; i++) //Upper border
{
std::cout << "#";
}
for (int i{ 0 }; i < GMD.Height; i++) //Side borders
{
for (int q{ 0 }; q < GMD.Width; q++)
{
if (q == 0 || q == GMD.Width)
std::cout << "#"; std::cout << " ";
}
std::cout << std::endl;
}
for (int i = 0; i < GMD.Width; i++) //Lower border
{
std::cout << "#";
}
return ;
}
void Controller()
{
}
int main()
{
std::cout << "Welcome to \"Snake\"\n";
Settings();
while (!GameOver)
{
Graphics(); // THis moment, console just dark screen
GameLogic();
Controller();
}
return 0;
}
#包括
#包括
布尔·加莫弗;
结构游戏数据
{
int x{0};
int y{0};
整数宽度;
内部高度;
int AppleX;
int AppleY;
智力得分;
};
枚举蛇形方向{Stop=0,向上、向下、向左、向右};
蛇道;
无效EnterFieldSize()
{
GameData-GMD;
整数宽度{0};
整数高度{0};
标准:曲线宽度;
标准:cout>高度;
GMD.宽度=宽度;
GMD.高度=高度;
如果(宽度<10 | |宽度>100)//
{
标准::cout 100)
{
std::cout您有几个名为GMD
的变量。一个在设置中,一个在图形中,一个在输入字段大小中。这些都是不同的变量
不同函数中的两个变量是不同的变量,即使它们具有相同的名称。您的设置
函数正在更改一个变量,但您的图形
函数正在使用完全不同的变量
您应该在main
中声明一个GMD
变量,并将其作为参考参数传递给所有需要使用它的函数
您可能需要对C++基本原理进行一些阅读。参数传递是几乎所有编程语言的基本技能,变量范围的概念也是如此。
< P>它是因为您设置了实例化的GMD、放置高度和宽度,而没有返回任何东西。GMD在图形功能中的应用
第二个选项是创建一个带有变量GMD的类,并使用相同的代码来实现更好的屏幕控制。您需要使用“curses”类型库,而不仅仅是std::cout
。当它说“添加更多解释”时,您有一个return;std::coutw不要只输入垃圾文本。请详细解释。GMD.Width
和GMD.Height
在Settings()
中未定义。