简易的贪吃蛇小游戏(以后或许会更新)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;
}

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

相关推荐
湫ccc7 分钟前
《Python基础》之基本数据类型
开发语言·python
Matlab精灵8 分钟前
Matlab函数中的隐马尔可夫模型
开发语言·matlab·统计学习
Microsoft Word9 分钟前
c++基础语法
开发语言·c++·算法
数据小爬虫@11 分钟前
如何利用java爬虫获得淘宝商品评论
java·开发语言·爬虫
qq_1728055919 分钟前
RUST学习教程-安装教程
开发语言·学习·rust·安装
wjs202426 分钟前
MongoDB 更新集合名
开发语言
monkey_meng30 分钟前
【遵守孤儿规则的External trait pattern】
开发语言·后端·rust
一只小小汤圆34 分钟前
opencascade源码学习之BRepOffsetAPI包 -BRepOffsetAPI_DraftAngle
c++·学习·opencascade
legend_jz1 小时前
【Linux】线程控制
linux·服务器·开发语言·c++·笔记·学习·学习方法
嘿BRE1 小时前
【C++】几个基本容器的模拟实现(string,vector,list,stack,queue,priority_queue)
c++