- C++
贪吃蛇2.0
- @ 2026-6-30 17:36:50
#include #include #include #include #include <windows.h> #include <conio.h>
using namespace std;
// 全局常量 const int WIDTH = 30; // 游戏区域宽度 const int HEIGHT = 20; // 游戏区域高度
// 方向枚举 enum Dir { STOP = 0, LEFT, RIGHT, UP, DOWN };
// 皮肤类型枚举 enum SkinType { CLASSIC = 1, // 经典皮肤 COLORFUL = 2, // 彩色皮肤 SIMPLE = 3 // 简约皮肤 };
// 坐标结构体 struct Point { int x, y; Point(int x_ = 0, int y_ = 0) : x(x_), y(y_) {} };
// 皮肤配置结构体 struct SkinConfig { char head; // 蛇头字符 char body; // 蛇身字符 char food; // 食物字符 string name; // 皮肤名称 };
// 全局变量 vector snake; // 蛇的坐标 Point food; // 食物位置 Dir dir; // 当前移动方向 int score; // 分数 bool gameOver; // 游戏结束标志 int gameSpeed; // 游戏速度(毫秒Sleep时间) SkinType currentSkin; // 当前选择的皮肤 vector skins; // 皮肤配置列表
// 函数声明 void Setup(); // 初始化游戏 void Draw(); // 绘制画面 void Input(); // 处理输入 void Logic(); // 游戏逻辑处理 void HideCursor(); // 隐藏控制台光标 void ChooseDifficulty();// 选择游戏难度 void ChooseSkin(); // 选择蛇的皮肤 void InitSkins(); // 初始化皮肤配置
int main() { HideCursor(); InitSkins(); // 初始化皮肤配置 ChooseSkin(); // 选择皮肤 ChooseDifficulty(); // 选择难度 Setup();
// 游戏主循环
while (!gameOver) {
Draw();
Input();
Logic();
Sleep(gameSpeed); // 根据难度调整速度
}
cout << "\n游戏结束!你的得分:" << score << endl;
return 0;
}
// 隐藏控制台光标 void HideCursor() { CONSOLE_CURSOR_INFO cursor; cursor.dwSize = 1; cursor.bVisible = FALSE; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor); }
// 初始化皮肤配置 void InitSkins() { // 清空皮肤列表 skins.clear();
// 经典皮肤(默认)
skins.push_back({
'O', // 蛇头
'o', // 蛇身
'F', // 食物
"经典皮肤"
});
// 彩色皮肤(使用特殊字符)
skins.push_back({
'●', // 蛇头
'○', // 蛇身
'★', // 食物
"彩色皮肤"
});
// 简约皮肤
skins.push_back({
'#', // 蛇头
'-', // 蛇身
'*', // 食物
"简约皮肤"
});
}
// 选择蛇的皮肤 void ChooseSkin() { char choice; system("cls"); // 清屏 cout << "" << endl; cout << " 贪吃蛇游戏" << endl; cout << "" << endl; cout << " 选择皮肤" << endl; cout << "" << endl; cout << " 1 - " << skins[0].name << " (O o F)" << endl; cout << " 2 - " << skins[1].name << " (● ○ ★)" << endl; cout << " 3 - " << skins[2].name << " (# - *)" << endl; cout << "" << endl; cout << "请选择皮肤(1/2/3):";
// 循环直到输入有效选择
while (true) {
choice = _getch();
if (choice == '1' || choice == '2' || choice == '3') {
break;
}
}
// 根据选择设置当前皮肤
switch (choice) {
case '1':
currentSkin = CLASSIC;
break;
case '2':
currentSkin = COLORFUL;
break;
case '3':
currentSkin = SIMPLE;
break;
default:
currentSkin = CLASSIC; // 默认经典皮肤
break;
}
// 短暂显示选择结果
system("cls");
cout << "已选择:" << skins[currentSkin - 1].name << endl;
Sleep(800); // 显示800毫秒
}
// 选择游戏难度 void ChooseDifficulty() { char choice; system("cls"); // 清屏 cout << "" << endl; cout << " 贪吃蛇游戏" << endl; cout << "" << endl; cout << " 选择难度" << endl; cout << "" << endl; cout << " 1 - 简单 (慢)" << endl; cout << " 2 - 中等 (中)" << endl; cout << " 3 - 困难 (快)" << endl; cout << "" << endl; cout << "请选择难度(1/2/3):";
// 循环直到输入有效选择
while (true) {
choice = _getch();
if (choice == '1' || choice == '2' || choice == '3') {
break;
}
}
// 根据选择设置游戏速度(值越小速度越快)
switch (choice) {
case '1':
gameSpeed = 150; // 简单难度,最慢
break;
case '2':
gameSpeed = 80; // 中等难度,默认速度
break;
case '3':
gameSpeed = 40; // 困难难度,最快
break;
default:
gameSpeed = 80; // 默认中等难度
break;
}
}
// 初始化游戏参数 void Setup() { gameOver = false; dir = RIGHT; score = 0; snake.clear(); // 使用push_back添加初始蛇身 snake.push_back(Point(WIDTH / 2, HEIGHT / 2)); snake.push_back(Point(WIDTH / 2 - 1, HEIGHT / 2)); snake.push_back(Point(WIDTH / 2 - 2, HEIGHT / 2));
srand((unsigned)time(NULL));
// 随机生成食物
food.x = rand() % WIDTH;
food.y = rand() % HEIGHT;
}
// 绘制游戏画面 void Draw() { // 定位控制台光标到左上角,防止画面闪烁 COORD coord = {0, 0}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
// 获取当前皮肤配置
SkinConfig skin = skins[currentSkin - 1];
// 上边界
for (int i = 0; i < WIDTH + 2; i++)
cout << "#";
cout << endl;
// 绘制游戏区域
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
if (x == 0) cout << "#"; // 左边界
if (x == snake[0].x && y == snake[0].y)
cout << skin.head; // 蛇头
else {
bool isBody = false;
for (int i = 1; i < snake.size(); i++) {
if (snake[i].x == x && snake[i].y == y) {
cout << skin.body; // 蛇身体
isBody = true;
break;
}
}
if (!isBody) {
if (x == food.x && y == food.y)
cout << skin.food; // 食物
else
cout << " "; // 空格
}
}
if (x == WIDTH - 1) cout << "#"; // 右边界
}
cout << endl;
}
// 下边界
for (int i = 0; i < WIDTH + 2; i++)
cout << "#";
cout << endl;
// 显示分数和操作提示
cout << "得分:" << score << " | WASD移动 | X退出游戏" << endl;
// 显示当前难度
if (gameSpeed == 150)
cout << "当前难度:简单" << endl;
else if (gameSpeed == 80)
cout << "当前难度:中等" << endl;
else if (gameSpeed == 40)
cout << "当前难度:困难" << endl;
// 显示当前皮肤
cout << "当前皮肤:" << skin.name << endl;
}
// 处理输入 void Input() { if (_kbhit()) { // 判断是否有按键输入 switch (_getch()) { case 'a': if (dir != RIGHT) dir = LEFT; break; case 'd': if (dir != LEFT) dir = RIGHT; break; case 'w': if (dir != DOWN) dir = UP; break; case 's': if (dir != UP) dir = DOWN; break; case 'x': // 按X直接退出 gameOver = true; break; } } }
// 游戏逻辑处理(移动、吃食物、碰撞检测) void Logic() { // 1. 移动蛇身(从后往前复制前一个位置) for (int i = snake.size() - 1; i > 0; i--) { snake[i] = snake[i - 1]; } // 移动蛇头 switch (dir) { case LEFT: snake[0].x--; break; case RIGHT: snake[0].x++; break; case UP: snake[0].y--; break; case DOWN: snake[0].y++; break; default: break; }
// 2. 撞墙检测
if (snake[0].x < 0 || snake[0].x >= WIDTH ||
snake[0].y < 0 || snake[0].y >= HEIGHT) {
gameOver = true;
}
// 3. 撞到自己身体检测
for (int i = 1; i < snake.size(); i++) {
if (snake[0].x == snake[i].x && snake[0].y == snake[i].y) {
gameOver = true;
}
}
// 4. 吃到食物,分数+10,蛇身变长,重新生成食物
if (snake[0].x == food.x && snake[0].y == food.y) {
score += 10;
snake.push_back(Point(food.x, food.y));
// 重新生成食物,避免生成在蛇身上
bool overlap;
do {
overlap = false;
food.x = rand() % WIDTH;
food.y = rand() % HEIGHT;
for (vector<Point>::size_type i = 0; i < snake.size(); i++) {
Point p = snake[i];
if (p.x == food.x && p.y == food.y) {
overlap = true;
break;
}
}
} while (overlap);
}
}
1 comments
-
ALT_fanzequn LV 6 @ 2026-7-20 13:08:39疑似豆某人
- 1