源码 | C++实现贪吃蛇小游戏

2022-05-31 09:18:27 浏览数 (1)

源码

代码语言:javascript复制
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<conio.h>
//包含easyx图形库,可以使用给我们提供的一些函数,绘图,贴图
#include<graphics.h>
#define WIN_WIDTH 640
#define WIN_HEIGHT 480
#define MAX_SNAKE 500 //蛇的最大节数

//枚举
enum DIR //枚举蛇的方向
{
    UP,
    DOWN,
    LEFT,
    RIGHT,
};
struct Food //食物
{
    int x;
    int y;
    DWORD color;
    bool flag;//是否被吃掉,是否要重新生成食物
}food;
struct Pos
{
    int x;
    int y;
    DWORD color;
};
struct Snake//蛇的结构体
{
    int num;//当前节数
    int dir;//蛇的方向
    int score;//分数
    int size;//蛇的宽和高
    int speed;//蛇的移动速度
    //表示每一节蛇的坐标 数组
    struct Pos coor[MAX_SNAKE];
}snake;
void GameInit()
{
    //设置随机数种子
    srand(GetTickCount());
    snake.num = 3;
    snake.dir = RIGHT;
    snake.score = 0;
    snake.size = 10;
    snake.speed = 10;
    snake.coor[2].x = 0;
    snake.coor[2].y = 0;
    snake.coor[2].color = RGB(rand() % 256, rand() % 256, rand() % 256);//随机生成三个数,用三原色表示颜色
    snake.coor[1].x = 10;
    snake.coor[1].y = 0;
    snake.coor[1].color = RGB(rand() % 256, rand() % 256, rand() % 256);//随机生成三个数,用三原色表示颜色
    snake.coor[0].x = 20;
    snake.coor[0].y = 0;
    snake.coor[0].color = RGB(rand() % 256, rand() % 256, rand() % 256);//随机生成三个数,用三原色表示颜色
    //初始化食物
    food.x = rand() % (WIN_WIDTH / 10) * 10;//1*10  10 20 30 40 50 60 
    food.y = rand() % (WIN_HEIGHT / 10) * 10;
    food.color = RGB(rand() % 256, rand() % 256, rand() % 256);//随机生成三个数,用三原色表示颜色
    food.flag = true;
}
void GameDraw()
{
    cleardevice();

    for (int i = 0; i < snake.num; i  )
    {
        //设置填充颜色
        setfillcolor(snake.coor[i].color);
        //画矩形
        fillrectangle(snake.coor[i].x, snake.coor[i].y, snake.coor[i].x   10, snake.coor[i].y   10);
    }
    //绘制食物
    //solidcircle(food.x,food.y,5)
    if (food.flag)
    {
        setfillcolor(food.color);
        solidellipse(food.x, food.y, food.x   10, food.y   10);
    }
    //绘制分数
      //先把整数转成字符串
    char temp[20] = " ";
    sprintf(temp, "分数:%d", snake.score);
    outtextxy(10, 10, temp);//如果报错  “outtextxy”: 2 个重载中没有一个可以转换所有参数类型  一定是字符集问题

}
void GameMove()
{
    for (int i = snake.num - 1; i > 0; i--)
    {
        snake.coor[i].x = snake.coor[i - 1].x;
        snake.coor[i].y = snake.coor[i - 1].y;
    }
    switch (snake.dir)
    {
    case UP:
        snake.coor[0].y -= snake.speed;
        if (snake.coor[0].y < 0)
        {
            snake.coor[0].y = WIN_HEIGHT;
        }
        break;
    case DOWN:
        snake.coor[0].y  = snake.speed;
        if (snake.coor[0].y > WIN_HEIGHT)
        {
            snake.coor[0].y = 0;
        }
        break;
    case LEFT:
        snake.coor[0].x -= snake.speed;
        if (snake.coor[0].x < 0)
        {
            snake.coor[0].x = WIN_WIDTH;
        }
        break;
    case RIGHT:
        snake.coor[0].x  = snake.speed;
        if (snake.coor[0].x > WIN_WIDTH)
        {
            snake.coor[0].x = 0;
        }
        break;
    }
}
void GameControl()
{
    //获取键盘输入,_getch(); 不回显函数,
    char key = _getch();
    switch (key)
    {
    case 72:
        if (snake.dir != DOWN)
        {
            snake.dir = UP;
        }
        break;
    case 80:
        if (snake.dir != UP)
        {
            snake.dir = DOWN;
        }
        break;
    case 75:
        if (snake.dir != RIGHT)
        {
            snake.dir = LEFT;
        }
        break;
    case 77:
        if (snake.dir != LEFT)
        {
            snake.dir = RIGHT;
        }
        break;
    }
    printf("%dn", key);
}
void CreateFood()
{
    if (!food.flag)
    {
        food.x = rand() % (WIN_WIDTH / 10) * 10;//1*10  10 20 30 40 50 60 
        food.y = rand() % (WIN_HEIGHT / 10) * 10;
        food.color = RGB(rand() % 256, rand() % 256, rand() % 256);//随机生成三个数,用三原色表示颜色
        food.flag = true;
    }
}
void EatFood()
{
    if (food.flag && snake.coor[0].x == food.x && snake.coor[0].y == food.y)
    {
        food.flag = false;
        snake.num  ;
        snake.score  = 10;
        snake.coor[snake.num - 1].color = RGB(rand() % 256, rand() % 256, rand() % 256);//随机生成三个数,用三原色表示颜色
        CreateFood();
    }
}
int main()
{
    //1,创建窗口,init初始化 graphics 图形窗口
    initgraph(WIN_WIDTH, WIN_HEIGHT, SHOWCONSOLE);
    setbkcolor(RGB(207, 214, 229));//设置颜色
    cleardevice();//刷新窗口
    GameInit();

    while (1)
    {

        GameDraw();
        GameMove();
        if (_kbhit())
        {
            GameControl();
        }
        EatFood();
        Sleep(100);

    }

    getchar();
    return 0;
}

运行结果:

VS2019项目目录

下载后双击TanChiShe.sln即可使用VS打开

打开项目后可能会报缺少graphics.h库的相关错误,如下下载安装即可

0 人点赞