贪吃蛇单人版(调用EasyX编写)

单人版:不可穿墙

#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
#include<graphics.h>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")//引用静态库
#define NUM 200
#define SIZE 10//一节蛇的宽度
#define SPEED 100
#define WIDTH 640
#define HEIGHT 480
enum Ch {
    
     up = 72, down = 80, left = 75, right = 77 };
typedef struct Coor
{
    
    
    int x;
    int y;
}Coor;
typedef struct Snake
{
    
    
    int n;//多少节
    Ch ch;//方向
    Coor xy[NUM];//结构体数组,两百个坐标
}Snake;
Snake snake;
struct Food//食物的结构
{
    
    
    int x;//食物坐标
    int y;
    bool iseat;//是否被吃掉
};
Food food;//food结构体变量
void BGM();
void GameInit();
void DrawGame();
void SnakeMove();
void ChangeCh();
void CreateFood();
void EatFood();
int GameOver();
int main()
{
    
    
    int n;
    //初始化一个窗口界面,宽640,高480
    initgraph(WIDTH, HEIGHT);
    BGM();//放过小曲儿
    GameInit();//初始化游戏
    while (1)//大循环
    {
    
    
        while (!_kbhit())//小循环,按任意键退出循环,kbhit()有按键返回1,否则0
        {
    
    
            SnakeMove();//移动,新产生的蛇,坐标变尾巴
            DrawGame();//绘制
            CreateFood();
            EatFood();
            n = GameOver();
            if (n)
            {
    
    
                break;
            }
            Sleep(SPEED);
        }
        if (n)
        {
    
    
            break;
        }
        ChangeCh();//改变方向

    }

    closegraph();
    return 0;
}
void BGM()
{
    
    
    //播放音乐 mci media control interface(多媒体设备接口)
    //打开音乐,播放音乐  alias取别名 repeat重复播放
    mciSendString("open ./甩葱歌.mp3 alias BGM", 0, 0, 0);
    mciSendString("play BGM repeat", 0, 0, 0);
    if (0)
    {
    
    
        mciSendString("close BGM ", 0, 0, 0);
    }
}
void GameInit()
{
    
    
    srand((unsigned int)time(NULL));//设置随机种子
    //初始化蛇
    snake.n = 3;
    snake.ch = right;
    snake.xy[0].x = 100;
    snake.xy[0].y = 100;
    snake.xy[1].x = 90;
    snake.xy[1].y = 100;
    snake.xy[2].x = 80;
    snake.xy[2].y = 100;
    //初始化食物
    food.iseat = true;
}
void DrawGame()//绘制蛇
{
    
    
    //蛇有几节
    cleardevice();//清屏
    setfillcolor(RED);//蛇头
    fillrectangle(snake.xy[0].x, snake.xy[0].y,
        snake.xy[0].x + SIZE, snake.xy[0].y + SIZE);
    setfillcolor(GREEN);
    for (int i = 1; i < snake.n; i++)//蛇身
    {
    
    
        fillrectangle(snake.xy[i].x, snake.xy[i].y,
            snake.xy[i].x + SIZE, snake.xy[i].y + SIZE);
    }
    //绘制食物
    setfillcolor(RGB(253, 168, 198));
    fillroundrect(food.x, food.y, food.x + SIZE, food.y + SIZE, SIZE / 2, SIZE / 2);
}
void SnakeMove()//蛇的移动
{
    
    
    //先遍历整条蛇,把前一节蛇的坐标赋值给后一节蛇的坐标
    for (int i = snake.n - 1; i > 0; i--)//最后一组是第1组和第0组,不会越界
    {
    
    
        snake.xy[i].x = snake.xy[i - 1].x;
        snake.xy[i].y = snake.xy[i - 1].y;
    }
    //根据方向!蛇头的移动
    switch (snake.ch)
    {
    
    
    case up:
        snake.xy[0].y -= SIZE;
        break;
    case down:
        snake.xy[0].y += SIZE;
        break;
    case left:
        snake.xy[0].x -= SIZE;
        break;
    case right:
        snake.xy[0].x += SIZE;
        break;
    }
}
void ChangeCh()//改变蛇的方向
{
    
    
    char key;
    key = _getch();
    switch (key)
    {
    
    
    case up:
        if (snake.ch != down)
        {
    
    
            snake.ch = up;
        }
        break;
    case down:
        if (snake.ch != up)
        {
    
    
            snake.ch = down;
        }
        break;
    case left:
        if (snake.ch != right)
        {
    
    
            snake.ch = left;
        }
        break;
    case right:
        if (snake.ch != left)
        {
    
    
            snake.ch = right;
        }
        break;
    }
}
void CreateFood()
{
    
    
    bool flag;//有没有在蛇身上

    if (food.iseat == true)//保证食物被吃掉
    {
    
    
        while (1)//保证食物不在蛇身上
        {
    
    
            flag = false;//不在蛇身上
            //食物的x,y必须是SIZE的整数倍
            food.x = rand() % 64 * 10;//0-63 0-630
            food.y = rand() % 48 * 10;//0-48 0-470
            //判断在不在蛇身上蛇的身体上
            for (int i = 0; i < snake.n; i++)
            {
    
    
                if (food.x == snake.xy[i].x && food.y == snake.xy[i].y)
                {
    
    
                    flag = true;//在蛇身上
                    break;
                }
            }
            if (!flag)//如果不在蛇身上
            {
    
    
                food.iseat = false;
                break;
            }
        }
    }
}
void EatFood()
{
    
    
    //蛇头的坐标和食物的坐标重合
    if (snake.xy[0].x == food.x && snake.xy[0].y == food.y)
    {
    
    
        food.iseat = true;
        snake.n++;
    }
}
int GameOver()
{
    
    
    //跟墙碰撞
    if (snake.xy[0].x == 0-SIZE || snake.xy[0].x > WIDTH || snake.xy[0].y==0-SIZE || snake.xy[0].y==HEIGHT)
    {
    
    
        if (snake.xy[0].x == 0-SIZE)
        {
    
    
            snake.xy[0].x = WIDTH;
        }
        if (snake.xy[0].x == WIDTH)
        {
    
    
            snake.xy[0].x = 0;
        }
        if (snake.xy[0].y == 0-SIZE)
        {
    
    
            snake.xy[0].y = HEIGHT;
        }
        if (snake.xy[0].x == HEIGHT)
        {
    
    
            snake.xy[0].y = 0;
        }
    }
    //跟自己碰撞
    for (int i = 1; i < snake.n; i++)
    {
    
    
        if (snake.xy[0].x == snake.xy[i].x && snake.xy[0].y == snake.xy[i].y)
            return 1;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_61777209/article/details/124830642
今日推荐