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

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

相关推荐
远望清一色15 分钟前
基于MATLAB的实现垃圾分类Matlab源码
开发语言·matlab
confiself25 分钟前
大模型系列——LLAMA-O1 复刻代码解读
java·开发语言
凌云行者34 分钟前
OpenGL入门005——使用Shader类管理着色器
c++·cmake·opengl
XiaoLeisj36 分钟前
【JavaEE初阶 — 多线程】Thread类的方法&线程生命周期
java·开发语言·java-ee
凌云行者38 分钟前
OpenGL入门006——着色器在纹理混合中的应用
c++·cmake·opengl
杜杜的man40 分钟前
【go从零单排】go中的结构体struct和method
开发语言·后端·golang
幼儿园老大*41 分钟前
走进 Go 语言基础语法
开发语言·后端·学习·golang·go
半桶水专家41 分钟前
go语言中package详解
开发语言·golang·xcode
llllinuuu42 分钟前
Go语言结构体、方法与接口
开发语言·后端·golang
cookies_s_s43 分钟前
Golang--协程和管道
开发语言·后端·golang