
沙盒
主页
我是一个 傻 逼
我是一个 渣 渣
我是一个 菜 鸡
我是一个 蒟 蒻
我是一个 大 佬
你是第
次访问我主页的蒟蒻
该内容暂不可见
C#include <iostream>
#include <conio.h>
#include <windows.h>
#include <cstdlib>
#include <ctime>
#include <queue>
using namespace std;
// 游戏配置
const int WIDTH = 40;
const int HEIGHT = 20;
const int BASE_SPEED = 100;
enum eDir { STOP = 0, LEFT, RIGHT, UP, DOWN };
struct SnakeSegment {
int x, y;
} snake[100];
// 游戏状态
struct GameState {
eDir direction;
int length;
int foodX, foodY;
int score;
bool gameOver;
bool autoPilot;
} state;
// 初始化游戏
void InitializeGame() {
state.direction = STOP;
state.length = 1;
snake[0].x = WIDTH / 2;
snake[0].y = HEIGHT / 2;
state.score = 0;
state.gameOver = false;
state.autoPilot = false;
// 生成食物
do {
state.foodX = rand() % WIDTH;
state.foodY = rand() % HEIGHT;
} while (state.foodX == snake[0].x && state.foodY == snake[0].y);
}
// 绘制游戏界面
void RenderGame() {
system("cls");
// 上边界
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 << "#"; // 左边界
bool isSnake = false;
if (x == snake[0].x && y == snake[0].y) {
cout << "O"; // 蛇头
isSnake = true;
}
else if (x == state.foodX && y == state.foodY) {
cout << "F"; // 食物
}
else {
for (int i = 1; i < state.length; i++) {
if (snake[i].x == x && snake[i].y == y) {
cout << "o"; // 蛇身
isSnake = true;
break;
}
}
if (!isSnake) cout << " ";
}
if (x == WIDTH-1) cout << "#"; // 右边界
}
cout << endl;
}
// 下边界
for (int i = 0; i < WIDTH+2; i++) cout << "#";
cout << endl;
// 状态信息
cout << "Score: " << state.score
<< " | Mode: " << (state.autoPilot ? "AUTO" : "MANUAL")
<< "\nControls: WASD移动 M切换模式 X退出";
}
// 处理玩家输入
void ProcessInput() {
if (_kbhit()) {
switch (_getch()) {
case 'w': if (state.direction != DOWN) state.direction = UP; break;
case 's': if (state.direction != UP) state.direction = DOWN; break;
case 'a': if (state.direction != RIGHT) state.direction = LEFT; break;
case 'd': if (state.direction != LEFT) state.direction = RIGHT; break;
case 'm': state.autoPilot = !state.autoPilot; break;
case 'x': state.gameOver = true; break;
}
}
}
// 自动导航系统
void AutoNavigate() {
int targetX = state.foodX;
int targetY = state.foodY;
// 简单追踪算法
int dx = targetX - snake[0].x;
int dy = targetY - snake[0].y;
if (abs(dx) > abs(dy)) {
if (dx > 0 && state.direction != LEFT) state.direction = RIGHT;
else if (dx < 0 && state.direction != RIGHT) state.direction = LEFT;
} else {
if (dy > 0 && state.direction != UP) state.direction = DOWN;
else if (dy < 0 && state.direction != DOWN) state.direction = UP;
}
// 边缘保护
if (snake[0].x == 0 && state.direction == LEFT) state.direction = UP;
if (snake[0].x == WIDTH-1 && state.direction == RIGHT) state.direction = DOWN;
if (snake[0].y == 0 && state.direction == UP) state.direction = LEFT;
if (snake[0].y == HEIGHT-1 && state.direction == DOWN) state.direction = RIGHT;
}
// 更新游戏逻辑
void UpdateGame() {
// 保存旧尾部位置
int prevX = snake[state.length-1].x;
int prevY = snake[state.length-1].y;
// 移动身体
for (int i = state.length-1; i > 0; i--) {
snake[i].x = snake[i-1].x;
snake[i].y = snake[i-1].y;
}
// 移动头部
switch (state.direction) {
case LEFT: snake[0].x--; break;
case RIGHT: snake[0].x++; break;
case UP: snake[0].y--; break;
case DOWN: snake[0].y++; break;
}
// 边界检测
if (snake[0].x < 0 || snake[0].x >= WIDTH ||
snake[0].y < 0 || snake[0].y >= HEIGHT) {
state.gameOver = true;
}
// 吃食物检测
if (snake[0].x == state.foodX && snake[0].y == state.foodY) {
state.score += 10;
state.length++;
// 生成新食物
bool validPosition;
do {
validPosition = true;
state.foodX = rand() % WIDTH;
state.foodY = rand() % HEIGHT;
// 检查食物是否生成在蛇身上
for (int i = 0; i < state.length; i++) {
if (snake[i].x == state.foodX && snake[i].y == state.foodY) {
validPosition = false;
break;
}
}
} while (!validPosition);
}
}
int main() {
srand(time(0));
InitializeGame();
while (!state.gameOver) {
RenderGame();
ProcessInput();
if (state.autoPilot) AutoNavigate();
UpdateGame();
Sleep(BASE_SPEED - (state.length * 2)); // 随长度增加加快速度
}
system("cls");
cout << "===================\n"
<< " 游戏结束!\n"
<< " 最终得分: " << state.score << "\n"
<< "===================\n";
system("pause");
return 0;
}