CYS • 1天前
using namespace std;
// 光标操作函数 void gotoxy(int x, int y) {
COORD pos = { static_cast<SHORT>(x), static_cast<SHORT>(y) };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void hidecursor() {
CONSOLE_CURSOR_INFO info;
info.dwSize = 100;
info.bVisible = FALSE;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
}
// 游戏常量 - 增大游戏区域 const int WIDTH = 35; // 增加游戏区域宽度 const int HEIGHT = 22; // 增加游戏区域高度 const int SPEED = 200; // 速度控制
// 游戏类 class SnakeGame {
private:
struct Point {
int x, y;
};
deque<Point> snake;
Point food;
int direction; // 0:右, 1:下, 2:左, 3:上
int score;
bool gameover;
string gameoverReason; // 存储游戏结束的原因
void drawBorder() {
// 上边框
gotoxy(0, 0);
for (int i = 0; i < WIDTH + 2; i++)
cout << "#";
// 左右边框
for (int i = 1; i <= HEIGHT; i++) {
gotoxy(0, i);
cout << "#";
gotoxy(WIDTH + 1, i);
cout << "#";
}
// 下边框
gotoxy(0, HEIGHT + 1);
for (int i = 0; i < WIDTH + 2; i++)
cout << "#";
}
void generateFood() {
bool onSnake;
do {
onSnake = false;
food.x = rand() % WIDTH;
food.y = rand() % HEIGHT;
for (const auto &segment : snake) {
if (segment.x == food.x && segment.y == food.y) {
onSnake = true;
break;
}
}
} while (onSnake);
gotoxy(food.x + 1, food.y + 1);
cout << "@";
}
// 检查是否撞墙
bool isWallCollision(int x, int y) {
return (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT);
}
// 检查是否撞到自己
bool isSelfCollision(int x, int y) {
for (const auto &segment : snake) {
if (segment.x == x && segment.y == y) {
return true;
}
}
return false;
}
public:
SnakeGame() {
// 初始化蛇
snake.push_back({WIDTH / 2, HEIGHT / 2});
direction = 0;
score = 0;
gameover = false;
gameoverReason = "";
// 初始化随机种子
srand(time(NULL));
// 设置更大的控制台窗口
// 宽度 = 游戏区域宽度 + 边框 + 一些额外空间
// 高度 = 游戏区域高度 + 边框 + 分数显示区域
system("mode con cols=50 lines=30");
hidecursor();
system("cls");
// 游戏标题
gotoxy((WIDTH + 2) / 2 - 5, 0);
cout << "贪吃蛇游戏";
// 绘制边框
drawBorder();
// 生成第一个食物
generateFood();
// 绘制初始蛇
gotoxy(snake[0].x + 1, snake[0].y + 1);
cout << "O";
// 显示分数和操作说明
gotoxy(0, HEIGHT + 3);
cout << "分数: " << score;
gotoxy(0, HEIGHT + 4);
cout << "控制: WASD 移动, X 退出";
gotoxy(0, HEIGHT + 5);
cout << "按任意方向键开始游戏...";
}
void processInput() {
if (_kbhit()) {
char key = _getch();
switch (key) {
case 'w':
case 'W':
if (direction != 1)
direction = 3;
break;
case 's':
case 'S':
if (direction != 3)
direction = 1;
break;
case 'a':
case 'A':
if (direction != 0)
direction = 2;
break;
case 'd':
case 'D':
if (direction != 2)
direction = 0;
break;
case 'x':
case 'X':
gameover = true;
gameoverReason = "你主动退出了游戏";
break;
}
}
}
void update() {
// 清除开始提示
static bool firstMove = true;
if (firstMove && direction != 0) {
gotoxy(0, HEIGHT + 5);
cout << " ";
firstMove = false;
}
// 计算新的蛇头位置
Point newHead = snake.front();
switch (direction) {
case 0:
newHead.x++;
break; // 右
case 1:
newHead.y++;
break; // 下
case 2:
newHead.x--;
break; // 左
case 3:
newHead.y--;
break; // 上
}
// 检查是否撞墙 - 碰到墙就死
if (isWallCollision(newHead.x, newHead.y)) {
gameover = true;
gameoverReason = "撞墙了!";
return;
}
// 检查是否撞到自己
if (isSelfCollision(newHead.x, newHead.y)) {
gameover = true;
gameoverReason = "撞到自己了!";
return;
}
// 移动蛇
snake.push_front(newHead);
// 绘制新蛇头
gotoxy(newHead.x + 1, newHead.y + 1);
cout << "O";
// 检查是否吃到食物
if (newHead.x == food.x && newHead.y == food.y) {
score += 10;
gotoxy(7, HEIGHT + 3);
cout << score;
generateFood();
} else {
// 如果没有吃到食物,移除蛇尾
Point tail = snake.back();
snake.pop_back();
gotoxy(tail.x + 1, tail.y + 1);
cout << " ";
}
// 将旧蛇头位置改为蛇身
if (snake.size() > 1) {
gotoxy(snake[1].x + 1, snake[1].y + 1);
cout << "o";
}
}
bool isGameOver() const {
return gameover;
}
int getScore() const {
return score;
}
string getGameOverReason() const {
return gameoverReason;
}
};
int main() {
SnakeGame game;
while (!game.isGameOver()) {
game.processInput();
game.update();
Sleep(SPEED); // 控制游戏速度
}
system("cls");
cout << "===== 游戏结束 =====" << endl;
cout << "最终分数: " << game.getScore() << endl;
string reason = game.getGameOverReason();
if (!reason.empty()) {
cout << "原因: " << reason << endl;
} else {
cout << "游戏结束!" << endl;
}
cout << "\n按任意键退出...";
_getch();
return 0;
}
评论:
请先登录,才能进行评论