版本比较
C++控制台游戏头文件
正文
增加 29 行删除 24 行
10 行未改动11121314151617181920212223242516 行未改动4243444546474849465051525354505152535455565755596061565763646566586869705960616263646566676877697071727374757677787980848586878182838485868788899091929394959639 行未改动10 行未改动#include <stdlib.h>#include <windows.h>#include <conio.h>
const WORD default_color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;const WORD red = FOREGROUND_RED | FOREGROUND_INTENSITY;const WORD green = FOREGROUND_GREEN | FOREGROUND_INTENSITY;const WORD blue = FOREGROUND_BLUE | FOREGROUND_INTENSITY;const WORD yellow = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;const WORD cyan = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;const WORD gray = FOREGROUND_INTENSITY;
inline void set_cursor_visible(bool visible) { CONSOLE_CURSOR_INFO cursorInfo;16 行未改动 if (_kbhit()) return _getch(); return 0;}
#define MAX_BUF 4000
class screen {private: CHAR_INFO* buffer; int width; int height; char text_buf[MAX_BUF]; WORD color_buf[MAX_BUF];
void draw(int x, int y, char ch, WORD color) { if (x >= 0 && x < width && y >= 0 && y < height) { int index = y * width + x; buffer[index].Char.AsciiChar = ch; buffer[index].Attributes = color; } }
public: screen(int w, int h) : width(w), height(h) { buffer = new CHAR_INFO[width * height]; } screen(int w, int h) : width(w), height(h) {}
~screen() { delete[] buffer; }
void clear() { for (int i = 0; i < width * height; i++) { buffer[i].Char.AsciiChar = ' '; buffer[i].Attributes = default_color; int total = width * height; for (int i = 0; i < total; i++) { text_buf[i] = ' '; color_buf[i] = default_color; } }
void text(int x, int y, const char* str, WORD color = default_color) { int cur_x = x; while (*str) { draw(cur_x++, y, *str, color); if (cur_x >= 0 && cur_x < width && y >= 0 && y < height) { int index = y * width + cur_x; text_buf[index] = *str; color_buf[index] = color; } cur_x++; str++; } }
void render() { HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); COORD size = { (SHORT)width, (SHORT)height }; COORD pos = { 0, 0 }; SMALL_RECT rect = { 0, 0, (SHORT)(width - 1), (SHORT)(height - 1) }; WriteConsoleOutput(h, buffer, size, pos, &rect); COORD pos = {0, 0}; SetConsoleCursorPosition(h, pos); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int index = y * width + x; SetConsoleTextAttribute(h, color_buf[index]); putchar(text_buf[index]); } if (y < height - 1) { putchar('\n'); } } }};
#endif39 行未改动