注:AI生成的
想要的可以拿去啊
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <vector>
#include <ctime>
#include <fstream>
#include <iomanip>
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<Point> 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 << highScore;
fout.close();
}
}
// 随机生成食物
void spawnFood() {
srand((unsigned)time(0) + rand());
while (true) {
food.x = rand() % (WIDTH - 2) + 1; // 1 ~ WIDTH-2
food.y = rand() % (HEIGHT - 2) + 1;
// 确保食物不在蛇身上
bool onSnake = false;
for (auto& p : snake) {
if (p == food) { onSnake = true; break; }
}
if (!onSnake) break;
}
}
// 初始化游戏
void init() {
snake.clear();
// 蛇从屏幕中央开始,水平向右
int startX = WIDTH / 2;
int startY = HEIGHT / 2;
for (int i = 0; i < INIT_LENGTH; i++) {
snake.push_back({startX - i, startY});
}
dir = RIGHT;
score = 0;
gameOver = false;
paused = false;
spawnFood();
}
// 绘制游戏界面
void draw() {
gotoxy(0, 0);
setColor(GRAY);
// 上边框
for (int i = 0; i < WIDTH + 2; i++) cout << "#";
cout << "\n";
for (int y = 0; y < HEIGHT; y++) {
setColor(GRAY);
cout << "#"; // 左边框
setColor(WHITE);
for (int x = 0; x < WIDTH; x++) {
Point p = {x, y};
if (p == snake[0]) {
setColor(GREEN);
cout << "O"; // 蛇头
} else {
bool isBody = false;
for (size_t i = 1; i < snake.size(); i++) {
if (p == snake[i]) {
setColor(GREEN);
cout << "o"; // 蛇身
isBody = true;
break;
}
}
if (!isBody) {
if (p == food) {
setColor(RED);
cout << "*"; // 食物
} else {
cout << " "; // 空格
}
}
}
}
setColor(GRAY);
cout << "#\n"; // 右边框
}
// 下边框
for (int i = 0; i < WIDTH + 2; i++) cout << "#";
// 显示信息
gotoxy(0, HEIGHT + 3);
setColor(YELLOW);
cout << "得分: " << score << " 最高分: " << highScore
<< " 长度: " << snake.size() << "\n";
setColor(GRAY);
cout << "操作: W/A/S/D 或 方向键移动 P暂停 R重启 Q退出\n";
if (paused) {
setColor(YELLOW);
gotoxy(WIDTH / 2 - 4, HEIGHT / 2);
cout << "【已暂停】";
}
setColor(WHITE);
}
// 输入处理
void input() {
if (_kbhit()) {
char ch = _getch();
// 方向键需要两个字节,第二字节作为判断
if (ch == -32 || ch == 224) {
ch = _getch();
switch (ch) {
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; // →
}
} else {
switch (ch) {
case 'w': case 'W': if (dir != DOWN) dir = UP; break;
case 's': case 'S': if (dir != UP) dir = DOWN; break;
case 'a': case 'A': if (dir != RIGHT) dir = LEFT; break;
case 'd': case 'D': if (dir != LEFT) dir = RIGHT; break;
case 'p': case 'P': paused = !paused; break;
case 'r': case 'R': init(); break;
case 'q': case 'Q': gameOver = true; break;
}
}
}
}
// 移动蛇
void move() {
if (dir == STOP || paused) return;
Point head = snake[0];
switch (dir) {
case UP: head.y--; break;
case DOWN: head.y++; break;
case LEFT: head.x--; break;
case RIGHT: head.x++; break;
default: break;
}
// 撞墙检测
if (head.x < 0 || head.x >= WIDTH || head.y < 0 || head.y >= HEIGHT) {
gameOver = true;
return;
}
// 撞自己检测(除了尾巴,因为尾巴会移动)
for (size_t i = 0; i < snake.size() - 1; i++) {
if (head == snake[i]) {
gameOver = true;
return;
}
}
// 移动蛇身
snake.insert(snake.begin(), head);
if (head == food) {
score += 10;
spawnFood();
} else {
snake.pop_back(); // 没吃到食物就去掉尾巴
}
}
// 游戏结束画面
void gameOverScreen() {
saveHighScore();
system("cls");
setColor(RED);
gotoxy(WIDTH / 2 - 5, HEIGHT / 2 - 2);
cout << "============";
gotoxy(WIDTH / 2 - 5, HEIGHT / 2 - 1);
cout << " GAME OVER ";
gotoxy(WIDTH / 2 - 5, HEIGHT / 2);
cout << "============";
setColor(YELLOW);
gotoxy(WIDTH / 2 - 8, HEIGHT / 2 + 2);
cout << "最终得分: " << score;
gotoxy(WIDTH / 2 - 8, HEIGHT / 2 + 3);
cout << "最高分: " << highScore;
setColor(WHITE);
gotoxy(WIDTH / 2 - 10, HEIGHT / 2 + 5);
cout << "按 R 重玩 / 按 Q 退出";
setColor(WHITE);
}
// 主函数
int main() {
system("mode con cols=35 lines=25");
system("title 贪吃蛇 Snake");
hideCursor();
loadHighScore();
init();
while (true) {
if (!gameOver) {
draw();
input();
move();
Sleep(SPEED);
} else {
gameOverScreen();
char ch = _getch();
if (ch == 'r' || ch == 'R') {
system("cls");
init();
} else if (ch == 'q' || ch == 'Q') {
break;
}
}
}
return 0;
}







没有回复内容