用c++实现简单的贪吃蛇游戏


#include <iostream>
#include <conio.h>
#include <windows.h>

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 printTail = false;
                for (int k = 0; k < nTail; k++)
                {
                    if (tailX[k] == j && tailY[k] == i)
                    {
                        cout << "o"; // 绘制蛇身
                        printTail = true;
                    }
                }
                if (!printTail)
                    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 >= 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;
}

这个代码实现了一个简单的贪吃蛇游戏。其中,`Setup`函数用于初始化游戏设置,`Draw`函数用于绘制游戏界面,`Input`函数用于处理用户输入,`Logic`函数用于处理游戏逻辑。`main`函数是程序的入口,通过不断调用这些函数来实现游戏的运行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我们来实现一个简单贪吃蛇游戏吧!我们将使用C++编程语言和控制台窗口来实现它。 首先,我们需要定义一些变量来存储游戏数据。我们需要一个二维数组来表示游戏地图,一个表示蛇身体的链表,一个表示食物的坐标,以及一些其他的变量。下面是一些定义: ```c++ #include <iostream> #include <conio.h> #include <ctime> using namespace std; const int width = 20; // 游戏窗口宽度 const int height = 20; // 游戏窗口高度 struct point { int x, y; }; point food; // 食物的坐标 int score; // 当前得分 bool gameOver; // 游戏是否结束 enum direction { STOP = 0, LEFT, RIGHT, UP, DOWN }; // 定义方向 direction dir; // 当前方向 ``` 接下来,我们需要实现一些函数来初始化游戏,绘制地图、蛇和食物,以及控制蛇的移动。下面是这些函数的实现: ```c++ void init() { gameOver = false; dir = STOP; score = 0; srand(time(NULL)); // 设置随机种子 food.x = rand() % width; food.y = rand() % height; } 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 == food.y && j == food.x) { cout << "F"; // 绘制食物 } else if (i == snake.front().y && j == snake.front().x) { cout << "O"; // 绘制蛇头 } else { bool print = false; for (auto& p : snake) { if (p.x == j && p.y == 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() { point prev = snake.front(); point next = { prev.x, prev.y }; switch (dir) { case LEFT: next.x--; break; case RIGHT: next.x++; break; case UP: next.y--; break; case DOWN: next.y++; break; } if (next.x < 0 || next.x >= width || next.y < 0 || next.y >= height) { gameOver = true; // 撞墙,游戏结束 } for (auto& p : snake) { if (p.x == next.x && p.y == next.y) { gameOver = true; // 撞到自己,游戏结束 } } if (next.x == food.x && next.y == food.y) { score++; point tail = snake.back(); snake.push_back(tail); food.x = rand() % width; food.y = rand() % height; } else { snake.pop_back(); } snake.push_front(next); } int main() { init(); while (!gameOver) { draw(); input(); logic(); } cout << "Game over! Your score is " << score << endl; return 0; } ``` 我们使用`init()`函数来初始化游戏数据。在`draw()`函数中,我们首先清空控制台,然后绘制地图和食物。我们使用一个链表来存储蛇的身体,链表头表示蛇头,链表尾表示蛇尾。在`input()`函数中,我们通过`_kbhit()`和`_getch()`函数来获取用户输入。在`logic()`函数中,我们根据当前方向移动蛇,并检查是否吃到了食物或者撞墙或者撞到自己。 最后,在`main()`函数中,我们不断循环直到游戏结束,然后输出最终得分。 现在我们就实现了一个简单贪吃蛇游戏。你可以根据自己的需求来修改代码和调整游戏规则。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值