c++贪吃蛇代码(带注释)

#include <iostream>
#include <conio.h>
using namespace std;

// 定义贪吃蛇节点结构体
struct Node {
    int x;
    int y;
};

const int width = 20;     // 地图宽度
const int height = 20;    // 地图高度
int score = 0;            // 记录得分
bool gameOver = false;    // 记录游戏是否结束
int foodX, foodY;        // 食物的坐标
int sleepTime = 100;      // 控制游戏速度的睡眠时间
char snakeHead = 'O';     // 蛇头的字符
char snakeBody = 'o';     // 蛇身的字符

Node snake[100];          // 定义贪吃蛇节点数组
int snakeSize = 1;        // 记录贪吃蛇长度

void initMap() {
    // 初始化地图
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            if (i == 0 || i == height - 1 || j == 0 || j == width - 1) {
                cout << '#';
            } else {
                cout << ' ';
            }
        }
        cout << endl;
    }
}

void initSnake() {
    // 初始化贪吃蛇
    snake[0].x = 5;
    snake[0].y = 5;
    cout << snakeHead;
}

void generateFood() {
    // 随机生成食物
    int x = rand() % (height - 2) + 1;
    int y = rand() % (width - 2) + 1;
    foodX = x;
    foodY = y;
    gotoxy(foodY, foodX);
    cout << '*';
}

void moveSnake() {
    // 移动贪吃蛇
    for (int i = snakeSize - 1; i > 0; i--) {
        snake[i].x = snake[i - 1].x;
        snake[i].y = snake[i - 1].y;
    }
    if (_kbhit()) {
        // 如果用户按下了键盘,则根据键盘上的方向键,调整贪吃蛇的移动方向
        switch (_getch()) {
            case 'w':
                snake[0].x--;
                break;
            case 'a':
                snake[0].y--;
                break;
            case 's':
                snake[0].x++;
                break;
            case 'd':
                snake[0].y++;
                break;
            default:
                break;
        }
    }
    // 判断贪吃蛇是否吃到了食物
    if (snake[0].x == foodX && snake[0].y == foodY) {
        score += 10;
        snakeSize++;
        generateFood();
    }
    // 判断贪吃蛇是否撞到自己或撞到边界
    if (snake[0].x == 0 || snake[0].x == height - 1 || snake[0].y == 0 || snake[0].y == width - 1) {
        gameOver = true;
    }
    for (int i = 1; i < snakeSize; i++) {
        if (snake[0].x == snake[i].x && snake[0].y == snake[i].y) {
            gameOver = true;
            break;
        }
    }
}

void showScore() {
    // 显示得分
    gotoxy(width + 2, 3);
    cout << "Score: " << score;
}

void gameOverDisplay() {
    // 游戏结束时的显示
    system("cls");
    cout << "Game Over!" << endl;
    cout << "Your Score: " << score << endl;
}

int main() {
    srand(time(NULL));
    initMap();
    initSnake();
    generateFood();
    while (!gameOver)  

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
下面是C++实现贪吃蛇游戏的代码,已附详细注释。 ```c++ #include <iostream> #include <conio.h> // 用于 getch() 函数,可在控制台获取用户按下的键 #include <windows.h> // 用于 Sleep() 函数,可使程序暂停指定的时间 using namespace std; bool gameOver; const int width = 20; const int height = 20; int x, y, fruitX, fruitY, score; int tailX[100], tailY[100]; // 记录蛇身体的坐标 int nTail; // 蛇身长度 enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; eDirection dir; void Setup() { gameOver = false; dir = STOP; x = width / 2; y = height / 2; fruitX = rand() % width; fruitY = rand() % height; score = 0; } 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 (int k = 0; k < nTail; k++) { if (tailX[k] == j && tailY[k] == i) { cout << "o"; // 画蛇身 print = true; } } 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': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': gameOver = true; break; } } } void Logic() { // 移动蛇身 int prevX = tailX[0]; int prevY = tailY[0]; int prev2X, prev2Y; tailX[0] = x; tailY[0] = y; for (int i = 1; i < nTail; i++) { prev2X = tailX[i]; prev2Y = tailY[i]; tailX[i] = prevX; tailY[i] = prevY; prevX = prev2X; prevY = prev2Y; } // 移动蛇头 switch (dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; default: break; } // 判断是否吃到水果 if (x == fruitX && y == fruitY) { score += 10; fruitX = rand() % width; fruitY = rand() % height; nTail++; } // 判断是否碰到边界 if (x < 0 || x == width || y < 0 || y == height) gameOver = true; // 判断是否碰到自己的身体 for (int i = 0; i < nTail; i++) { if (tailX[i] == x && tailY[i] == y) gameOver = true; } } int main() { Setup(); while (!gameOver) { Draw(); Input(); Logic(); Sleep(50); // 暂停 50 毫秒 } return 0; } ``` 该程序的运行方式为控制台窗口,玩家使用键盘上的方向键控制贪吃蛇的移动方向,每吃到一个水果加 10 分,并增加一节身体长度。如果贪吃蛇碰到边界或自己的身体,则游戏结束。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值