
40天前发布8次阅读
该帖子部分内容已隐藏
付费阅读
10积分
#include
#include
#include
#include
#include
#include
#include
using namespace std;
// 游戏配置
const int WIDTH = 30; // 游戏区域宽度
const int HEIGHT = 20; // 游戏区域高度
const int INIT_LENGTH = 3; // 初始蛇长
const int SPEED = 120; // 移动间隔(毫秒),越小越快
// 方向枚举
enum Direction { UP, DOWN, LEFT, RIGHT, STOP };
// 位置结构
struct Point {
int x, y;
bool operator==(const Point& p) const { return x == p.x && y == p.y; }
};
// 全局变量
vector snake; // 蛇身
Point food; // 食物
Direction dir = STOP; // 当前方向
int score = 0; // 当前得分
int highScore = 0; // 最高分
bool gameOver = false; // 游戏结束标志
bool paused = false; // 暂停标志
// 光标移动
void gotoxy(int x, int y) {
COORD pos = {(SHORT)x, (SHORT)y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
// 隐藏光标
void hideCursor() {
CONSOLE_CURSOR_INFO ci = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &ci);
}
// 设置颜色
void setColor(int color) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
// 颜色定义
enum {
WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
GREEN = FOREGROUND_GREEN | FOREGROUND_INTENSITY,
RED = FOREGROUND_RED | FOREGROUND_INTENSITY,
YELLOW = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY,
BLUE = FOREGROUND_BLUE | FOREGROUND_INTENSITY,
GRAY = FOREGROUND_INTENSITY
};
// 读取最高分
void loadHighScore() {
ifstream fin("snake_highscore.txt");
if (fin) fin >> highScore;
fin.close();
}
// 保存最高分
void saveHighScore() {
if (score > highScore) {
highScore = score;
ofstream fout("snake_highscore.txt");
fout

15天前发布2次阅读

13天前发布3次阅读
该帖子内容已隐藏

36天前发布7次阅读

39天前发布13次阅读

12天前发布2次阅读
该帖子内容已隐藏
该帖子部分内容已隐藏

35天前发布5次阅读




该帖子内容已隐藏









