简易的贪吃蛇小游戏(以后或许会更新)C++/C语言

第一版:

cpp 复制代码
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>

#define WIDTH 20
#define HEIGHT 20

int gameOver;
int score;
int x, y; // 蛇头的坐标
int fruitX, fruitY; // 食物的坐标
int tailX[100], tailY[100]; // 蛇身的坐标
int tailLength;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
enum eDirection dir;

void Setup()
{
    gameOver = 0;
    score = 0;
    x = WIDTH / 2;
    y = HEIGHT / 2;
    fruitX = rand() % WIDTH;
    fruitY = rand() % HEIGHT;
    dir = STOP;
}

void Draw()
{
    system("cls"); // 清屏

    for (int i = 0; i < WIDTH + 2; i++)
        printf("#");
    printf("\n");

    for (int i = 0; i < HEIGHT; i++)
    {
        for (int j = 0; j < WIDTH; j++)
        {
            if (j == 0)
                printf("#");
            if (i == y && j == x)
                printf("O");
            else if (i == fruitY && j == fruitX)
                printf("F");
            else
            {
                int printTail = 0;
                for (int k = 0; k < tailLength; k++)
                {
                    if (tailX[k] == j && tailY[k] == i)
                    {
                        printf("o");
                        printTail = 1;
                    }
                }
                if (!printTail)
                    printf(" ");
            }
            if (j == WIDTH - 1)
                printf("#");
        }
        printf("\n");
    }

    for (int i = 0; i < WIDTH + 2; i++)
        printf("#");
    printf("\n");

    printf("Score: %d\n", score);
}

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 = 1;
            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 < tailLength; 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 < tailLength; i++)
    {
        if (tailX[i] == x && tailY[i] == y)
            gameOver = 1;
    }

    if (x == fruitX && y == fruitY)
    {
        score += 10;
        fruitX = rand() % WIDTH;
        fruitY = rand() % HEIGHT;
        tailLength++;
    }
}

int main()
{
    Setup();
    while (!gameOver)
    {
        Draw();
        Input();
        Logic();
        Sleep(10); // 控制游戏速度
    }
    return 0;
}

第二版:

cpp 复制代码
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>

#define WIDTH 30
#define HEIGHT 20

int gameOver;
int score;
int x, y; // 蛇头的坐标
int fruitX, fruitY; // 食物的坐标
int tailX[100], tailY[100]; // 蛇身的坐标
int tailLength;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
enum eDirection dir;

int aiX, aiY; // AI蛇头的坐标
int aiTailX[100], aiTailY[100]; // AI蛇身的坐标
int aiTailLength;
enum eDirection aiDir;

void Setup()
{
    gameOver = 0;
    score = 0;
    x = WIDTH / 2;
    y = HEIGHT / 2;
    fruitX = rand() % WIDTH;
    fruitY = rand() % HEIGHT;
    dir = STOP;

    aiX = WIDTH / 2 - 5;
    aiY = HEIGHT / 2;
    aiDir = STOP;
    aiTailLength = 0;
}

void Draw()
{
    system("cls"); // 清屏

    for (int i = 0; i < WIDTH + 2; i++)
        printf("#");
    printf("\n");

    for (int i = 0; i < HEIGHT; i++)
    {
        for (int j = 0; j < WIDTH; j++)
        {
            if (j == 0)
                printf("#");
            if (i == y && j == x)
                printf("O");
            else if (i == fruitY && j == fruitX)
                printf("F");
            else if (i == aiY && j == aiX)
                printf("A");
            else
            {
                int printTail = 0;
                for (int k = 0; k < tailLength; k++)
                {
                    if (tailX[k] == j && tailY[k] == i)
                    {
                        printf("o");
                        printTail = 1;
                    }
                }
                for (int k = 0; k < aiTailLength; k++)
                {
                    if (aiTailX[k] == j && aiTailY[k] == i)
                    {
                        printf("a");
                        printTail = 1;
                    }
                }
                if (!printTail)
                    printf(" ");
            }
            if (j == WIDTH - 1)
                printf("#");
        }
        printf("\n");
    }

    for (int i = 0; i < WIDTH + 2; i++)
        printf("#");
    printf("\n");

    printf("Score: %d\n", score);
}

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 = 1;
            break;
        }
    }
}

void AI()
{
    if (aiX < fruitX)
        aiDir = RIGHT;
    else if (aiX > fruitX)
        aiDir = LEFT;
    else if (aiY < fruitY)
        aiDir = DOWN;
    else if (aiY > fruitY)
        aiDir = UP;
}

void Logic()
{
    int prevX = tailX[0];
    int prevY = tailY[0];
    int prev2X, prev2Y;
    tailX[0] = x;
    tailY[0] = y;
    for (int i = 1; i < tailLength; i++)
    {
        prev2X = tailX[i];
        prev2Y = tailY[i];
        tailX[i] = prevX;
        tailY[i] = prevY;
        prevX = prev2X;
        prevY = prev2Y;
    }

    int aiPrevX = aiTailX[0];
    int aiPrevY = aiTailY[0];
    int aiPrev2X, aiPrev2Y;
    aiTailX[0] = aiX;
    aiTailY[0] = aiY;
    for (int i = 1; i < aiTailLength; i++)
    {
        aiPrev2X = aiTailX[i];
        aiPrev2Y = aiTailY[i];
        aiTailX[i] = aiPrevX;
        aiTailY[i] = aiPrevY;
        aiPrevX = aiPrev2X;
        aiPrevY = aiPrev2Y;
    }

    switch (dir)
    {
    case LEFT:
        x--;
        break;
    case RIGHT:
        x++;
        break;
    case UP:
        y--;
        break;
    case DOWN:
        y++;
        break;
    }

    switch (aiDir)
    {
    case LEFT:
        aiX--;
        break;
    case RIGHT:
        aiX++;
        break;
    case UP:
        aiY--;
        break;
    case DOWN:
        aiY++;
        break;
    }

    if (x >= WIDTH || x < 0 || y >= HEIGHT || y < 0)
        gameOver = 1;

    for (int i = 0; i < tailLength; i++)
    {
        if (tailX[i] == x && tailY[i] == y)
            gameOver = 1;
    }

    if (aiX >= WIDTH || aiX < 0 || aiY >= HEIGHT || aiY < 0)
        gameOver = 1;

    for (int i = 0; i < aiTailLength; i++)
    {
        if (aiTailX[i] == aiX && aiTailY[i] == aiY)
            gameOver = 1;
    }

    if (x == fruitX && y == fruitY)
    {
        score += 10;
        fruitX = rand() % WIDTH;
        fruitY = rand() % HEIGHT;
        tailLength++;
    }

    if (aiX == fruitX && aiY == fruitY)
    {
        score += 10;
        fruitX = rand() % WIDTH;
        fruitY = rand() % HEIGHT;
        aiTailLength++;
    }
}

int main()
{
    srand(time(NULL)); // 随机数种子
    Setup();
    while (!gameOver)
    {
        Draw();
        Input();
        AI();
        Logic();
        Sleep(100); // 控制游戏速度
    }
    printf("GAME OVER\n");
    return 0;
}

制作不易,喜欢的话,给个赞吧。

相关推荐
benchmark_cc1 小时前
一次获取多只股票实时行情:QuantDash `quotes.get(symbols=[...])` 批量快照
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
geovindu1 小时前
python: PCA
开发语言·后端·python·算法
Matt Feng1 小时前
STM32多路SWD烧录:并行下载与5类连接故障排查
c语言·stm32·单片机·嵌入式硬件
guizhoumen2 小时前
PageAdmin开源网站无障碍/适老化工具条插件-让网站“看得清、听得懂“
c++·mfc
__万波__2 小时前
嵌入式存储技术--1.Flash物理存储
java·开发语言
Chen_harmony2 小时前
十六、ALV树
开发语言·c++
qq_274475153 小时前
第 28 篇:Promise 全解析:状态、then/catch/finally、静态方法 all/race/allSettled/any
开发语言·javascript
西西弗Sisyphus3 小时前
C++ 最小 Agentic Loop 实现 - 回调函数系列 - 初识回调(5)
开发语言·c++
程序员小杰@3 小时前
Java 统一报文转换 + 签名验签完整介绍
java·开发语言
爱写代码的小朋友4 小时前
C++ 信息学奥赛入门教程-从零基础到通过 CSP-J 第二轮的十八堂课
c++