使用C++编写的简单贪吃蛇游戏

介绍

在本文中,我们将学习如何使用C++编写一个简单的贪吃蛇游戏。这个程序包括游戏场景、移动方向、食物和蛇的身体等基本元素。

库文件,全局变量和常量,宽度和高度、贪吃蛇头的位置、食物位置和贪吃蛇尾部位置

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

const int width = 20; // 游戏场景宽度
const int height = 20; // 游戏场景高度
int x, y; // 贪吃虫头部位置
int fruitX, fruitY; // 食物位置
int tailX[100], tailY[100]; // 贪吃虫尾部位置
int nTail; // 贪吃虫长度
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; // 移动方向
eDirection dir;
bool gameOver; // 游戏结束标志

接下来,我们为游戏创建各种函数,包括初始化游戏设置(Setup())、绘制游戏场景(Draw())、处理玩家输入(Input())、更新游戏逻辑(Logic())以及主菜单(MainMenu())和游戏结束菜单(GameOverMenu())。

// 初始化游戏设置
void Setup()
{
    gameOver = false;
    dir = STOP;
    x = width / 2;
    y = height / 2;
    fruitX = rand() % width;
    fruitY = rand() % height;
    nTail = 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;
}

// 处理玩家输入
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':
            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)
    {
        nTail++;
        fruitX = rand() % width;
        fruitY = rand() % height;
    }
}

// 主菜单
void MainMenu()
{
    system("cls");
    cout << "      贪吃蛇" << endl;
    cout << "按任意键开始游戏..." << endl;
    _getch();
}

这些函数分别负责不同的任务,例如,Setup()函数用于初始化游戏的各种参数,Draw()函数用于绘制游戏场景,Input()函数用于处理玩家的键盘输入,Logic()函数用于更新游戏的逻辑状态,而MainMenu()和GameOverMenu()函数则分别用于显示主菜单和游戏结束菜单。

最后,在main()函数中,我们调用这些函数来运行游戏。首先,我们调用MainMenu()函数显示主菜单,然后调用Setup()函数初始化游戏。接着,我们进入一个循环,在循环中,我们调用Draw()函数绘制游戏场景,然后调用Input()函数处理玩家的输入,再调用Logic()函数更新游戏的状态。如果游戏结束,我们会调用GameOverMenu()函数显示游戏结束菜单并询问玩家是否要重新开始游戏。

void GameOverMenu()
{
    cout << "Game Over!" << endl;
    cout << "是否再玩一局(y/n)" << endl;
    char choice;
    cin >> choice;
    if (choice == 'y' || choice == 'Y')
    {
        gameOver = false;
        nTail = 0;
        Setup();
    }
    else
    {
        gameOver = true;
    }
}

// 主程序
int main()
{
    MainMenu();
    Setup();
    while (!gameOver)
    {
        Draw();
        Input();
        Logic();
        if (gameOver)
        {
            GameOverMenu();
        }
        Sleep(100); // 控制游戏速度
    }
    return 0;
}

完整代码

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

// 定义游戏场景大小和变量
const int width = 20; // 游戏场景宽度
const int height = 20; // 游戏场景高度
int x, y; // 贪吃虫头部位置
int fruitX, fruitY; // 食物位置
int tailX[100], tailY[100]; // 贪吃虫尾部位置
int nTail; // 贪吃虫长度
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; // 移动方向
eDirection dir;
bool gameOver; // 游戏结束标志

// 初始化游戏设置
void Setup()
{
    gameOver = false;
    dir = STOP;
    x = width / 2;
    y = height / 2;
    fruitX = rand() % width;
    fruitY = rand() % height;
    nTail = 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;
}

// 处理玩家输入
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':
            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)
    {
        nTail++;
        fruitX = rand() % width;
        fruitY = rand() % height;
    }
}

// 主菜单
void MainMenu()
{
    system("cls");
    cout << "      贪吃蛇" << endl;
    cout << "按任意键开始游戏..." << endl;
    _getch();
}

// 游戏结束菜单
void GameOverMenu()
{
    cout << "Game Over!" << endl;
    cout << "是否再玩一局(y/n)" << endl;
    char choice;
    cin >> choice;
    if (choice == 'y' || choice == 'Y')
    {
        gameOver = false;
        nTail = 0;
        Setup();
    }
    else
    {
        gameOver = true;
    }
}

// 主程序
int main()
{
    MainMenu();
    Setup();
    while (!gameOver)
    {
        Draw();
        Input();
        Logic();
        if (gameOver)
        {
            GameOverMenu();
        }
        Sleep(100); // 控制游戏速度
    }
    return 0;
}

以上就是使用C++编写的简单贪吃蛇游戏的全部内容。通过这个例子,我们可以看到如何使用C++来实现一个有趣的游戏。希望这篇文章对你有所帮助!

请注意,这只是一个简化的示例,你可以根据需要添加更多的功能,比如增加游戏难度、添加得分系统等。

  • 9
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用c++编写贪吃蛇游戏的代码: ```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], 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 < 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; } if (x == fruitx && y == fruity) { score += 10; fruitx = rand() % width; fruity = rand() % height; ntail++; } } int main() { Setup(); while (!gameover) { Draw(); Input(); Logic(); Sleep(100); // 控制游戏速度 } return 0; } ``` 这个贪吃蛇游戏的实现使用了Windows.h库,所以只能在Windows系统中运行。如果想在其他操作系统中运行,需要对部分代码进行修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值