控制台下贪食蛇闪屏严重
菜鸟第一次写贪食蛇结果发现闪屏十分严重
大神们 有没有什么解决方法_(:з」∠)_
#include <stdio.h> #include <Windows.h> #include "MyConsole.h" #include "MyTools.h" static HANDLE _Output = GetStdHandle(STD_OUTPUT_HANDLE); static SHORT _MaxHeight = 25; static SHORT _MaxWidth = 80; // 设置窗口标题文字 void SetWindowTitle(const char* title) { SetConsoleTitleA(title); } // 获取窗口标题文字 char* GetWindowTitle(char* title, size_t maxLength) { GetConsoleTitleA(title, maxLength); return title; } // 设置窗口尺寸 void SetWindowSize(SHORT rows, SHORT columns) { // 修改全局变量 COORD largestSize = GetLargestConsoleWindowSize(_Output); _MaxWidth = (columns <= largestSize.X) ? columns : largestSize.X; _MaxHeight = (rows <= largestSize.Y) ? rows : largestSize.Y; // 设置缓冲区尺寸 COORD bufferSize; bufferSize.X = _MaxWidth; bufferSize.Y = _MaxHeight; // 设置窗口尺寸 SMALL_RECT rectangle; rectangle.Left = 0; rectangle.Top = 0; rectangle.Right = _MaxWidth - 1; rectangle.Bottom = _MaxHeight - 1; SetConsoleScreenBufferSize(_Output, bufferSize); SetConsoleWindowInfo(_Output, TRUE, &rectangle); } // 返回窗口最大列数 SHORT GetWindowColumns(void) { return _MaxWidth; } // 返回窗口最大行数 SHORT GetWindowRows(void) { return _MaxHeight; } // 把光标定位到指定位置 void GotoXY(SHORT row, SHORT column) { if ((column >= 0) && (column < _MaxWidth) && (row >= 0) && (row < _MaxHeight)) { COORD pos; pos.X = column; pos.Y = row; SetConsoleCursorPosition(_Output, pos); } } // 获取光标位置 COORD GetCursorPosition(void) { CONSOLE_SCREEN_BUFFER_INFO screenInfo; GetConsoleScreenBufferInfo(_Output, &screenInfo); return screenInfo.dwCursorPosition; } // 将指定的前景色和背景色组合成文本属性 WORD CombineTextAttribute(BYTE fore, BYTE back) { return (WORD) (back << 4) + fore; } // 将指定的文本属性分解为前景色和背景色 void SplitTextAttribute(WORD attr, BYTE* fore, BYTE* back) { *fore = attr & 0xf; *back = (attr >> 4); } // 用指定的前景色和背景色设置输出文本颜色属性并返回指定的属性 WORD SetTextColor(BYTE fore, BYTE back) { WORD attr = CombineTextAttribute(fore, back); SetConsoleTextAttribute(_Output, attr); return attr; } // 用指定的颜色属性清除一块矩形区域 void ClearWindow(SMALL_RECT rect, WORD attr) { COORD size; size.X = rect.Right - rect.Left + 1; size.Y = rect.Bottom - rect.Top + 1; CHAR_INFO* buffer = new CHAR_INFO[size.X * size.Y]; for (int index = 0; index < (size.X * size.Y); ++index) { buffer[index].Char.AsciiChar = ' '; buffer[index].Attributes = attr; } FillRegion(buffer, rect); delete [] buffer; } // 用预置的文本填充指定区域 void FillRegion(CHAR_INFO* buffer, SMALL_RECT region) { COORD bufferSize; bufferSize.X = region.Right - region.Left + 1; bufferSize.Y = region.Bottom - region.Top + 1; COORD pos; pos.X = 0; pos.Y = 0; WriteConsoleOutputA(_Output, buffer, bufferSize, pos, ®ion); }