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

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

相关推荐
lly2024061 分钟前
Highcharts 饼图:数据可视化利器
开发语言
薄荷故人_6 分钟前
从零开始的C++之旅——红黑树封装map_set
c++
lw向北.7 分钟前
Qt For Android之环境搭建(Qt 5.12.11 Qt下载SDK的处理方案)
android·开发语言·qt
IT女孩儿29 分钟前
JavaScript--WebAPI查缺补漏(二)
开发语言·前端·javascript·html·ecmascript
m0_7482389229 分钟前
webgis入门实战案例——智慧校园
开发语言·ios·swift
悲伤小伞34 分钟前
C++_数据结构_详解二叉搜索树
c语言·数据结构·c++·笔记·算法
Clockwiseee43 分钟前
PHP伪协议总结
android·开发语言·php
小灰灰搞电子44 分钟前
Qt实现Android的图案密码(图形解锁)源码分享
开发语言·qt
m0_675988232 小时前
Leetcode3218. 切蛋糕的最小总开销 I
c++·算法·leetcode·职场和发展
吴冰_hogan2 小时前
JVM(Java虚拟机)的组成部分详解
java·开发语言·jvm