- 取石子
贪吃蛇*c++o11*gzjun
- 2025-7-13 15:26:28 @
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <vector>
using namespace std;
// 定义屏幕大小
const int width = 20;
const int height = 20;
// 蛇的位置和方向
int x, y;
int fruitX, fruitY;
int score;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirection dir;
// 蛇的身体
vector<pair<int, int>> snakeBody;
// 设置控制台光标位置
void gotoxy(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
// 隐藏控制台光标
void hideCursor() {
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO info;
info.dwSize = 100;
info.bVisible = FALSE;
SetConsoleCursorInfo(consoleHandle, &info);
}
// 初始化游戏
void Setup() {
hideCursor();
dir = STOP;
x = width / 2;
y = height / 2;
fruitX = rand() % width;
fruitY = rand() % height;
score = 0;
snakeBody.clear();
snakeBody.push_back(make_pair(x, y));
}
// 绘制游戏界面
void Draw() {
system("cls");
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (j == 0)
cout << "#";
if (i == y && j == x)
cout << "O";
else if (i == fruitY && j == fruitX)
cout << "F";
else {
bool print = false;
for (auto part : snakeBody) {
if (part.first == j && part.second == i) {
cout << "o";
print = true;
break;
}
}
if (!print)
cout << " ";
}
if (j == width - 1)
cout << "#";
}
cout << endl;
}
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
cout << "Score: " << score << 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':
exit(0);
break;
}
}
}
// 游戏逻辑更新
void Logic() {
pair<int, int> prevHead = snakeBody[0];
pair<int, int> newHead = prevHead;
switch (dir) {
case LEFT:
newHead.first--;
break;
case RIGHT:
newHead.first++;
break;
case UP:
newHead.second--;
break;
case DOWN:
newHead.second++;
break;
default:
break;
}
// 检查是否吃到水果
if (newHead.first == fruitX && newHead.second == fruitY) {
score++;
fruitX = rand() % width;
fruitY = rand() % height;
} else {
snakeBody.pop_back();
}
// 检查是否撞到边界或自己
if (newHead.first < 0 || newHead.first >= width || newHead.second < 0 || newHead.second >= height) {
cout << "Game Over!" << endl;
system("pause");
exit(0);
}
for (auto part : snakeBody) {
if (part.first == newHead.first && part.second == newHead.second) {
cout << "Game Over!" << endl;
system("pause");
exit(0);
}
}
snakeBody.insert(snakeBody.begin(), newHead);
}
int main() {
Setup();
while (true) {
Draw();
Input();
Logic();
Sleep(100);
}
return 0;
}
3 条评论
-
曹莫凡 @ 2025-7-14 14:27:48
这是什么啊?@高梓均
-
2025-7-13 17:15:39@
代码可能有问题访问15253201282@163.com邮箱
-
2025-7-13 17:00:48@
有问题请私信UP 操作方法: C++代码生成确保是C++O11 编译器版本:C++6.5、C++5.13、C++5.11 输入法确保是英文(全角) W,A,S,D 作者:高梓钧gzjun 邮箱:15253201282@163.com、gzjunde2013@163.com
- 1
信息
- ID
- 116
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 8
- 标签
- 递交数
- 56
- 已通过
- 9
- 上传者