我正在使用youtuber JavidX9的一些技巧在控制台中制作一个简单的游戏。当窗口和缓冲区大小正确时,所有内容都会正确显示,对于尝试运行它的任何人来说都是FYI。当我尝试在调试时关闭应用程序时,我的问题就出现了。有时如果我通过随机移动屏幕周围的字符来搞定它就会破坏它。我得到的错误是标题中列出的错误。但在那之后,我收到一个链接器错误,告诉我该进程仍在运行(我找不到)。因此调试是一场噩梦。我已经好几天都在寻找这个问题的答案,希望你们中的一个C ++神可以帮助我。代码很短。
编辑:刚刚发现了一些新信息。如果我启动该程序,并且什么都不做,它将在大约一分钟后崩溃。潜在的线程问题?#include <iostream>
#include <Windows.h>
#include <chrono>
using namespace std;
int screenWidth = 120; //Console Screen Size. X = Cols & Y = Rows
int screenHeight = 40;
int mapWidth = 48; //World Dimensions
int mapHeight = 48;
int playerX = mapWidth / 2;
int playerY = mapHeight / 2;
int main()
{
wchar_t *screen = new wchar_t[screenWidth * screenHeight];
HANDLE consoleHandle = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
SetConsoleActiveScreenBuffer(consoleHandle);
DWORD bytesWritten = 0;
// 16 x 3
wstring map; //^
map += L"################################################";
map += L"#..............................................#";
map += L"#.......########........########........########";
map += L"#..............#...............#...............#";
map += L"#......##......#.......##......#.......##......#";
map += L"#......##......#.......##......#.......##......#";
map += L"#..............#...............#...............#";
map += L"###............#.##............#.##............#";
map += L"##.............#.#.............#.#.............#";
map += L"#......####..###.......####..###.......####..###";
map += L"#......#.......#.......#.......#.......#.......#";
map += L"#......#.......#.......#.......#.......#.......#";
map += L"#..............#...............#...............#";
map += L"#......#########.......#########.......#########";
map += L"#..............#...............................#";
map += L"#..............................................#";
map += L"#..............................................#";
map += L"#..............................................#";
map += L"#.......########........########........########";
map += L"#..............#...............#...............#";
map += L"#......##......#.......##......#.......##......#";
map += L"#......##......#.......##......#.......##......#";
map += L"#..............#...............#...............#";
map += L"###............#.##............#.##............#";
map += L"##.............#.#.............#.#.............#";
map += L"#......####..###.......####..###.......####..###";
map += L"#......#.......#.......#.......#.......#.......#";
map += L"#......#.......#.......#.......#.......#.......#";
map += L"#..............#...............#...............#";
map += L"#......#########.......#########.......#########";
map += L"#..............#...............................#";
map += L"#..............................................#";
map += L"#..............................................#";
map += L"#..............................................#";
map += L"#.......########........########........########";
map += L"#..............#...............#...............#";
map += L"#......##......#.......##......#.......##......#";
map += L"#......##......#.......##......#.......##......#";
map += L"#..............#...............#...............#";
map += L"###............#.##............#.##............#";
map += L"##.............#.#.............#.#.............#";
map += L"#......####..###.......####..###.......####..###";
map += L"#......#.......#.......#.......#.......#.......#";
map += L"#......#.......#.......#.......#.......#.......#";
map += L"#..............#...............#...............#";
map += L"#......#########.......#########.......#########";
map += L"#..............#...............................#";
map += L"################################################";
const int TICKS_PER_SECOND = 20;
const int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
const int MAX_FRAMESKIP = 10;
DWORD next_game_tick = GetTickCount();
int loops;
bool game_is_running = true;
while (game_is_running)
{
loops = 0;
while (GetTickCount() > next_game_tick && loops < MAX_FRAMESKIP)
{
// Update
for (int i = 0; i < screenWidth * screenHeight; i++)
{
screen[i] = ' ';
}
if (GetAsyncKeyState((unsigned short)'A'))
{
if (map[(playerX - 1) + mapWidth * playerY] == '#')
{
break;
}
else
{
playerX--;
}
}
if (GetAsyncKeyState((unsigned short)'D'))
{
if (map[(playerX + 1) + mapWidth * playerY] == '#')
{
break;
}
else
{
playerX++;
}
}
if (GetAsyncKeyState((unsigned short)'S'))
{
if (map[playerX + mapWidth * (playerY + 1)] == '#')
{
break;
}
else
{
playerY++;
}
}
if (GetAsyncKeyState((unsigned short)'W'))
{
if (map[playerX + mapWidth * (playerY - 1)] == '#')
{
break;
}
else
{
playerY--;
}
}
next_game_tick += SKIP_TICKS;
loops++;
}
// Draw the map to the buffer
for (int y = 0; y < mapHeight; y++)
{
for (int x = 0; x < mapWidth; x++)
{
screen[x + screenWidth * y] = map[x + mapWidth * y];
}
screen[playerX + screenWidth * playerY] = '@'; // Draw the player
}
// Display the Buffer
screen[screenWidth * screenHeight - 1] = '\0';
WriteConsoleOutputCharacter(consoleHandle, screen, screenWidth * screenHeight, { 0,0 }, &bytesWritten);
}
}
答案 0 :(得分:0)
我解决了这个问题。我的地图数组大于我的屏幕数组导致了问题。我无法深入解释这是如何导致错误的,但确实如此。添加2d相机后,将其锁定到播放器坐标,并将其夹在地图阵列的边框,它现在工作正常。我现在有一整套其他问题要处理,但是这个问题可以标记为已解决。如果其他人可以深入解释为什么这个问题会令人惊讶。感谢。