每天推荐一个经典小游戏 贪吃蛇C++自取
2025-10-31 21:41:08
发布于:江苏
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <vector>
#include <time.h>
using namespace std;
// 游戏常量
const int WIDTH = 40;
const int HEIGHT = 20;
const char SNAKE_HEAD = 'O';
const char SNAKE_BODY = 'o';
const char FOOD = '*';
const char WALL = '#';
const char EMPTY = ' ';
// 方向枚举
enum Direction { STOP = 0, UP, DOWN, LEFT, RIGHT };
class SnakeGame {
private:
vector<pair<int, int>> snake; // 蛇的身体坐标
int foodX, foodY; // 食物坐标
Direction dir; // 当前方向
bool gameOver; // 游戏是否结束
int score; // 分数
public:
SnakeGame() {
resetGame();
}
void resetGame() {
snake.clear();
snake.push_back({HEIGHT/2, WIDTH/2}); // 初始位置
dir = STOP;
gameOver = false;
score = 0;
generateFood();
}
void generateFood() {
srand(time(NULL));
do {
foodX = rand() % WIDTH;
foodY = rand() % HEIGHT;
} while (isSnake(foodY, foodX));
}
bool isSnake(int y, int x) {
for (const auto& part : snake) {
if (part.first == y && part.second == x) {
return true;
}
}
return false;
}
void setup() {
system("mode con cols=42 lines=22"); // 设置控制台大小
system("title 贪吃蛇游戏");
}
void draw() {
system("cls"); // 清屏
// 绘制顶部边框
cout << WALL;
for (int i = 0; i < WIDTH; i++) {
cout << WALL;
}
cout << WALL << endl;
// 绘制游戏区域
for (int y = 0; y < HEIGHT; y++) {
cout << WALL;
for (int x = 0; x < WIDTH; x++) {
if (y == snake[0].first && x == snake[0].second) {
cout << SNAKE_HEAD; // 蛇头
} else if (y == foodY && x == foodX) {
cout << FOOD; // 食物
} else {
bool isBody = false;
for (size_t i = 1; i < snake.size(); i++) {
if (snake[i].first == y && snake[i].second == x) {
cout << SNAKE_BODY;
isBody = true;
break;
}
}
if (!isBody) {
cout << EMPTY;
}
}
}
cout << WALL << endl;
}
// 绘制底部边框
cout << WALL;
for (int i = 0; i < WIDTH; i++) {
cout << WALL;
}
cout << WALL << endl;
// 显示分数
cout << "分数: " << score << endl;
cout << "使用方向键移动,ESC退出游戏" << endl;
}
void input() {
if (_kbhit()) {
switch (_getch()) {
case 72: // 上箭头
if (dir != DOWN) dir = UP;
break;
case 80: // 下箭头
if (dir != UP) dir = DOWN;
break;
case 75: // 左箭头
if (dir != RIGHT) dir = LEFT;
break;
case 77: // 右箭头
if (dir != LEFT) dir = RIGHT;
break;
case 27: // ESC键
gameOver = true;
break;
}
}
}
void logic() {
// 移动蛇头
int headY = snake[0].first;
int headX = snake[0].second;
switch (dir) {
case UP:
headY--;
break;
case DOWN:
headY++;
break;
case LEFT:
headX--;
break;
case RIGHT:
headX++;
break;
case STOP:
return;
}
// 检查边界碰撞
if (headX < 0 || headX >= WIDTH || headY < 0 || headY >= HEIGHT) {
gameOver = true;
return;
}
// 检查自身碰撞
for (size_t i = 1; i < snake.size(); i++) {
if (snake[i].first == headY && snake[i].second == headX) {
gameOver = true;
return;
}
}
// 添加新的头部
snake.insert(snake.begin(), {headY, headX});
// 检查是否吃到食物
if (headY == foodY && headX == foodX) {
score += 10;
generateFood();
} else {
// 如果没吃到食物,移除尾部
snake.pop_back();
}
}
void run() {
setup();
while (!gameOver) {
draw();
input();
logic();
Sleep(100); // 控制游戏速度
}
system("cls");
cout << "游戏结束!" << endl;
cout << "最终分数: " << score << endl;
cout << "按任意键退出..." << endl;
_getch();
}
};
int main() {
SnakeGame game;
game.run();
return 0;
}
这个贪吃蛇游戏包含了以下功能:
游戏特性:
使用方向键控制蛇的移动
吃到食物后蛇身增长,分数增加
碰到墙壁或自己的身体游戏结束
实时显示当前分数
ESC 键退出游戏
如何编译运行:1 将代码保存为 snake.cpp2 使用 C++ 编译器编译:g++ snake.cpp -o snake.exe3 运行生成的可执行文件
游戏控制:
↑ ↓ ← → 方向键:控制蛇的移动方向
ESC 键:退出游戏
这是一个基础版本,你可以根据需要添加更多功能,比如:
难度选择
游戏暂停功能
最高分记录
不同类型的食物
障碍物
背景音乐
希望你喜欢这个经典的贪吃蛇游戏!
这里空空如也











有帮助,赞一个