C++贪吃蛇游戏:简单易懂的代码示例

想学习如何使用C++编写经典的贪吃蛇游戏吗?你来对地方了!本文将提供一个简单易懂的代码示例,并附带详细的注释,帮助你快速上手游戏开发。cpp#include #include <conio.h>#include <windows.h>

// 定义游戏状态和参数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++)        std::cout << '#';    std::cout << std::endl;

// 绘制游戏区域    for (int i = 0; i < height; i++)    {        for (int j = 0; j < width; j++)        {            if (j == 0)                std::cout << '#'; // 左边界

        if (i == y && j == x)                std::cout << 'O'; // 蛇头            else if (i == fruitY && j == fruitX)                std::cout << 'F'; // 食物            else            {                bool printTail = false;                for (int k = 0; k < nTail; k++)                {                    if (tailX[k] == j && tailY[k] == i)                    {                        std::cout << 'o'; // 蛇身                        printTail = true;                    }                }                if (!printTail)                    std::cout << ' '; // 空格            }

        if (j == width - 1)                std::cout << '#'; // 右边界        }        std::cout << std::endl;    }

// 绘制游戏边界    for (int i = 0; i < width + 2; i++)        std::cout << '#';    std::cout << std::endl;    std::cout << 'Score:' << score << std::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;    }

// 处理边界碰撞    if (x >= width)        x = 0;    else if (x < 0)        x = width - 1;

if (y >= height)        y = 0;    else if (y < 0)        y = height - 1;

// 判断是否吃到自己    for (int i = 0; i < nTail; i++)    {        if (tailX[i] == x && tailY[i] == y)            gameOver = true;    }

// 判断是否吃到食物    if (x == fruitX && y == fruitY)    {        score += 10;        fruitX = rand() % width;        fruitY = rand() % height;        nTail++;    }}

int main(){ Setup(); // 初始化游戏 while (!gameOver) // 游戏循环 { Draw(); // 绘制游戏画面 Input(); // 处理用户输入 Logic(); // 更新游戏逻辑 Sleep(10); // 控制游戏速度 } return 0;}

注意: 此示例使用了 Windows.h 头文件中的 Sleep() 函数,因此在 Windows 系统下运行时需要包含这个头文件。

希望这个示例代码能够帮助你开始编写自己的贪吃蛇游戏! 你可以根据自己的喜好修改游戏规则、添加新的功能,例如计分系统、关卡设计等等,让游戏更加有趣!

C++贪吃蛇游戏:简单易懂的代码示例

原文地址: https://www.cveoy.top/t/topic/PSN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录