C++ 在c+中设置控制台窗口大小和缓冲区大小+;(win32应用程序)
我有个大问题,至少对我来说。。。 (我为我的英语提前道歉,我还在学校学习) 我在C++中使用了一个简单的“蛇”游戏,使用Windows .h进行图形处理,手动操作屏幕缓冲区,而不是使用STD::CUT,因为它非常慢。 游戏运行得很好,这不是一个游戏逻辑问题,这是一个终端问题。。。(我使用的是VS 2015) 以下是程序的完整代码:C++ 在c+中设置控制台窗口大小和缓冲区大小+;(win32应用程序),c++,C++,我有个大问题,至少对我来说。。。 (我为我的英语提前道歉,我还在学校学习) 我在C++中使用了一个简单的“蛇”游戏,使用Windows .h进行图形处理,手动操作屏幕缓冲区,而不是使用STD::CUT,因为它非常慢。 游戏运行得很好,这不是一个游戏逻辑问题,这是一个终端问题。。。(我使用的是VS 2015) 以下是程序的完整代码: #include <iostream> #include <Windows.h> #include <list> #include
#include <iostream>
#include <Windows.h>
#include <list>
#include <thread>
using namespace std;
int nScreenWidth = 60;
int nScreenHeight = 63;
int main()
{
wchar_t *screen = new wchar_t[nScreenWidth*nScreenHeight];
HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL,
CONSOLE_TEXTMODE_BUFFER, NULL);
SetConsoleActiveScreenBuffer(hConsole);
DWORD dwBytesWritten = 0;
// MAIN LOOP ----------
while (1)
{
// RESET POINT ----------
bool bKeyLeft = false;
bool bKeyRight = false;
bool bKeyLeftOld = false;
bool bKeyRightOld = false;
struct sSnakeSegment {
int posx;
int posy;
};
list<sSnakeSegment> snake = {
{ 30,30 },
{ 31,30 },
{ 32,30 },
{ 33,30 },
{ 34,30 },
{ 35,30 },
{ 36,30 }
};
int x = 0;
int y = 0;
bool isDead = false;
int fx = 20;
int fy = 30;
int score = 0;
int nSnakeDirection = 3; // 0 = north (Clockwise)
while(!isDead)
{
// TIMING ----------
auto t1 = chrono::system_clock::now();
while ((chrono::system_clock::now() - t1) < 200ms)
{
// INPUT ----------
bKeyRight = (0x8000 & GetAsyncKeyState((unsigned char)('\x27'))) != 0;
bKeyLeft = (0x8000 & GetAsyncKeyState((unsigned char)('\x25'))) != 0;
if (bKeyRight && !bKeyRightOld)
{
nSnakeDirection++;
if (nSnakeDirection == 4) nSnakeDirection = 0;
}
if (bKeyLeft && !bKeyLeftOld)
{
nSnakeDirection--;
if (nSnakeDirection == -1) nSnakeDirection = 3;
}
bKeyRightOld = bKeyRight;
bKeyLeftOld = bKeyLeft;
}
// ===== GAME LOGIC =====
// HEAD CREATION ----------
switch (nSnakeDirection)
{
case 0: //UP
snake.push_front({ snake.front().posx , snake.front().posy - 1 });
break;
case 1: //RIGHT
snake.push_front({ snake.front().posx + 1 , snake.front().posy });
break;
case 2: //DOWN
snake.push_front({ snake.front().posx , snake.front().posy + 1 });
break;
case 3: //LEFT
snake.push_front({ snake.front().posx - 1 , snake.front().posy });
break;
}
// SNAKE VS FOOD
if (snake.front().posx == fx && snake.front().posy == fy)
{
while (screen[fy * nScreenWidth + fx] != L' ')
{
fx = rand() % nScreenWidth;
fy = (rand() % (nScreenHeight - 3)) + 3;
}
for (int i = 0; i < 5; i++)
snake.push_back({ snake.back().posx, snake.back().posy });
}
// TAIL CHOPPING
snake.pop_back();
// TITLE DISPLAY ----------
for (x = 0; x < nScreenWidth; x++)
{
for (y = 0; y < nScreenHeight; y++)
{
screen[y*nScreenWidth + x] = ' ';
}
}
for (int i = 0; i < nScreenWidth; i++)
{
screen[i] = L'=';
screen[2 * nScreenWidth + i] = L'=';
}
wsprintf(&screen[nScreenWidth + 10], L" simncstrn@gmail.com ! S N A K E !");
// SNAKE DISPLAY ----------
for (auto s : snake)
{
screen[s.posy * nScreenWidth + s.posx] = isDead ? L'+' : L'O';
}
screen[snake.front().posy * nScreenWidth + snake.front().posx] = isDead ? L'@' : L'#';
// FOOD DISPLAY ----------
screen[fy * nScreenWidth + fx] = L'F';
// SCREEN WRITING
screen[nScreenWidth * nScreenHeight] = '\0';
WriteConsoleOutputCharacter(hConsole, screen, nScreenWidth * nScreenHeight, { 0,0 }, &dwBytesWritten);
}
}
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
屏幕宽度=60;
屏幕高度=63;
int main()
{
wchar_t*screen=新的wchar_t[nScreenWidth*nScreenHeight];
HANDLE hConsole=CreateConsolesScreenbuffer(通用的|读|通用的|写,0,NULL,
控制台\文本模式\缓冲区,空);
设置控制台活动屏幕缓冲区(hConsole);
DWORD DWBYTESSWRITED=0;
//主回路----------
而(1)
{
//重置点----------
bool bKeyLeft=false;
bool bKeyRight=false;
bool bKeyLeftOld=false;
bool bKeyRightOld=false;
结构sSnakeSegment{
int-posx;
int-posy;
};
列表蛇={
{ 30,30 },
{ 31,30 },
{ 32,30 },
{ 33,30 },
{ 34,30 },
{ 35,30 },
{ 36,30 }
};
int x=0;
int y=0;
bool-isDead=false;
int fx=20;
int fy=30;
智力得分=0;
int nSnakeDirection=3;//0=北(顺时针)
而(!isDead)
{
//时机----------
自动t1=chrono::system_clock::now();
而((时钟::系统时钟::now()-t1)<200ms)
{
//输入----------
bKeyRight=(0x8000&GetAsyncKeyState((无符号字符)('\x27'))!=0;
bKeyLeft=(0x8000&GetAsyncKeyState((无符号字符)('\x25'))!=0;
if(bKeyRight&!bKeyRightOld)
{
nSnakeDirection++;
如果(nSnakeDirection==4)nSnakeDirection=0;
}
if(bKeyLeft&!bKeyLeftOld)
{
nSnakeDirection--;
如果(nSnakeDirection==-1)nSnakeDirection=3;
}
bKeyRightOld=bKeyRightOld;
bKeyLeftOld=bKeyLeft;
}
//====游戏逻辑=====
//头部创造----------
开关(nSnakeDirection)
{
案例0://向上
snake.push_front({snake.front().posx,snake.front().posy-1});
打破
案例1://对
snake.push_front({snake.front().posx+1,snake.front().posy});
打破
案例2://关闭
snake.push_front({snake.front().posx,snake.front().posy+1});
打破
案例3://左
snake.push_front({snake.front().posx-1,snake.front().posy});
打破
}
//蛇与食物
if(snake.front().posx==fx&&snake.front().posy==fy)
{
同时(屏幕[fy*nScreenWidth+fx]!=L'')
{
fx=rand()%n屏幕宽度;
fy=(rand()%(nScreenHeight-3))+3;
}
对于(int i=0;i<5;i++)
snake.push_back({snake.back().posx,snake.back().posy});
}
//斩尾
snake.pop_back();
//标题显示----------
对于(x=0;x
游戏甚至还没有完成,它缺少一些功能。
问题出现在启动应用程序时。控制台默认以120x40模式(缓冲区和窗口)打开,并使用不同的字符,我的游戏地图是60x63(标题为3),因此我必须手动右键单击窗口并手动设置选项。但我想自动完成这项工作。有办法做到这一点吗?事实上,我也在寻找类似的问题,但它们并不适合我。例如,我尝试使用SetConsoleWindowInfo()函数,但它似乎不起作用,也许我用错了
提前感谢您的帮助 请参阅中的MoveWindow()
解决方案请参阅中的MoveWindow()
解决方案