C语言 -- EGE 贪吃蛇简单制作及ege库安装

1.首先,我们需要在code::blocks 上安装ege环境,需要下载相关的文件。

百度网盘链接:链接: https://pan.baidu.com/s/1l1MD8hfbr410DcvOV2XM7Q 密码: ssda 如果无法搭建环境,可以在下方留言。

2.熟悉ege简单用法。

在下载的安装包中,有相关ege的库函数使用介绍,可以自己去查看。

 3.贪吃蛇的基本要求。

熟悉,并会运用链表等相关知识点,ege函数的使用,调试能力。

 4.游戏资源准备

相关图片 蛇身  食物   开始 结束 图片 

4.coding

#include <graphics.h>
#include <stdio.h>
#include<time.h>

#define Sn_len 1
#define IX 800
#define IY 600
#define H 30
#define W 30


typedef struct SNAKE
{
    int x;                                         ///横坐标
    int y;                                         ///纵坐标
    struct SNAKE *NEXT;
} snake;

typedef struct FOOD
{
    int x;
    int y;
} Food;

int status = 2  ;
char score = 'D';
int cnt=0;
int FPS = 100;
int run = 1;
int flag  = 1;
snake *Snake_Head;
Food *food = (Food *)malloc(sizeof(Food));
MUSIC mus; // 定义一个音乐对象
int musicstatus = 0;    //音乐状态值
int Food_Flag = 6;
int head_dirction = 1;
int talk_flag = 13;
int man_flag = 15;

int state();    // 左右上下移动
void drawsnake();   // 画蛇
int main(); //主函数
void move();    //获取键盘值
void drawsaNAKE(int x,int y,int flag);  // 画出一个蛇身
snake *SNAKE_INIT();    //蛇的初始化
void gameloop();    //游戏循环
int createfood();   // 创建食物
int Pding();    // 判断
void Pscore();  //等级
void gametext(int x,int y,char text[]);

void gametext(int x,int y,char text[])
{
    cleardevice();
    setfont(24,1,"幼圆");
    outtextxy(x,y,text);
}
void Pscore()
{
    setfont(20,0,"幼圆");
    outtextxy(0,0,"Level : ");
    outtextxy(80,0,score);
}

int Pding()
{
    if(food->x == Snake_Head->x&&food->y == Snake_Head->y)
    {
        mus.OpenFile("sounds\\eat.wav"); // 打开文件
        mus.Play();
        snake *node = (snake *)malloc(sizeof(snake));
        snake *temp = Snake_Head;

        while(temp!=NULL)
        {
            temp = temp->NEXT;
            cnt++;
        }

        if(cnt == 5)    // C
        {
            mus.OpenFile("sounds\\levelup.wav"); // 打开文件
            mus.Play();
            score -= 1;
            FPS -= 20;
        }
        else if(cnt == 10) //B
        {
            mus.OpenFile("sounds\\levelup.wav"); // 打开文件
            mus.Play();
            score -= 1;
            FPS -= 15;
        }
        else if (cnt == 15) //A
        {
            mus.OpenFile("sounds\\levelup.wav"); // 打开文件
            mus.Play();
            score -= 1;
            FPS -= 10;
        }

        talk_flag = 11+random(4);
        Pscore();
        createfood();

        if(status == 1)     //a
        {
            node->x = Snake_Head->x-H;
            node->y = Snake_Head->y;
        }
        else if( status == 2)   //d
        {
            node->x = Snake_Head->x + H;
            node->y = Snake_Head->y;
        }
        else if (status == 3)  //w
        {
            node->x = Snake_Head->x;
            node->y = Snake_Head->y - H ;
        }
        else if(status == 4)  //s
        {
            node->x = Snake_Head->x;
            node->y = Snake_Head->y + H;
        }
        node->NEXT = Snake_Head;
        Snake_Head = node;
    }
    if(Snake_Head->x<H || Snake_Head->x >IX || Snake_Head->y <H || Snake_Head->y >IY)       // 边界判断
    {
        run = 0;
    }
    cnt = 0;
    return run;
}
int createfood()
{
    food->x = 10+random(440);//rand()%440+10;
    food->y = 10+random(440);//rand()%440 + 40;

    while(food->x%30!=0)/*食物随机出现后必须让食物能够在整格内,这样才可以让蛇吃到*/
        food->x++;
    while(food->y%30!=0)
        food->y++;
    Food_Flag = 6+random(5);
    drawsaNAKE(food->x,food->y,Food_Flag);

    snake *temp;
    temp=Snake_Head;
    while(temp->NEXT!=NULL)//条件测试 :如果生成的食物位置在蛇身上,则重新生成食物
    {
        if(temp->x==food->x&&temp->y==food->y)
        {
            cleardevice();
            drawsnake();
            createfood();
        }
        else
        {
            temp=temp->NEXT;
        }
    }
    return 0;
}

void move()
{
    char ch;

    while(kbhit())
    {
        ch=getch();
        switch(ch)
        {
        case's': //4
        {
            status = 4;
            break;
        }
        case 'w':   //3
        {
            status = 3;
            break;
        }
        case 'a': //1
        {
            status = 1;
            break;
        }
        case 'd':  //2
        {
            status = 2;
            break;
        }
        }
    }
    if(GetAsyncKeyState(VK_SPACE))
    {
        run = 1;
    }

}
void drawsaNAKE(int x,int y,int flag)
{
    if(flag == 1)   // up
    {
        PIMAGE img=newimage();
        getimage(img,"img\\up.png",x,y);
        putimage(x,y,W,H,img,0,0,20,20);
        delimage(img);
    }
    else if(flag == 2) // dowm
    {
        PIMAGE img=newimage();
        getimage(img,"img\\down.png",x,y);
        putimage(x,y,W,H,img,0,0,20,20);
        delimage(img);
    }
    else if(flag == 3)  // left
    {
        PIMAGE img=newimage();
        getimage(img,"img\\left.png",x,y);
        putimage(x,y,W,H,img,0,0,20,20);
        delimage(img);
    }
    else if(flag == 4)  //right
    {
        PIMAGE img=newimage();
        getimage(img,"img\\right.png",x,y);
        putimage(x,y,W,H,img,0,0,20,20);
        delimage(img);
    }
    else if(flag == 5)  // 身体
    {
        PIMAGE img=newimage();
        getimage(img,"img\\body.png",x,y);
        putimage(x,y,W,H,img,0,0,20,20);
        delimage(img);
    }
    else if(flag == 6)  // 超人
    {
        PIMAGE img=newimage();
        getimage(img,"img\\human.png",x,y);
        putimage(x,y,W,H,img,0,0,20,20);
        delimage(img);
    }
    else if(flag == 7)  // 驴
    {
        PIMAGE img=newimage();
        getimage(img,"img\\lv.png",x,y);
        putimage(x,y,W,H,img,0,0,20,20);
        delimage(img);
    }
    else if(flag == 8)  //  巫师
    {
        PIMAGE img=newimage();
        getimage(img,"img\\wishu.png",x,y);
        putimage(x,y,W,H,img,0,0,20,20);
        delimage(img);
    }
    else if(flag == 9)  //  蜗牛
    {
        PIMAGE img=newimage();
        getimage(img,"img\\woniu.png",x,y);
        putimage(x,y,W,H,img,0,0,20,20);
        delimage(img);
    }
    else if(flag == 10)  //  僵尸
    {
        PIMAGE img=newimage();
        getimage(img,"img\\zanbi.png",x,y);
        putimage(x,y,W,H,img,0,0,20,20);
        delimage(img);
    }
    else if(flag == 11) //
    {
        PIMAGE img=newimage();
        getimage(img,"img\\nice.jpg",x,y);
        putimage(x,y,50,20,img,0,0,50,20);
        delimage(img);
    }
    else if(flag == 12) //
    {
        PIMAGE img=newimage();
        getimage(img,"img\\good.jpg",x,y);
        putimage(x,y,50,20,img,0,0,50,20);
        delimage(img);
    }
    else if(flag == 13) //
    {
        PIMAGE img=newimage();
        getimage(img,"img\\emm.jpg",x,y);
        putimage(x,y,50,20,img,0,0,50,20);
        delimage(img);
    }
    else if(flag == 14)  //
    {
        PIMAGE img=newimage();
        getimage(img,"img\\omg.jpg",x,y);
        putimage(x,y,50,20,img,0,0,50,20);
        delimage(img);
    }
    else if(flag == 15)  //blue
    {
        PIMAGE img=newimage();
        getimage(img,"img\\man.jpg",x,y);
        putimage(x,y,50,20,img,0,0,50,20);
        delimage(img);
    }
    else if(flag == 16)  //brown
    {
        PIMAGE img=newimage();
        getimage(img,"img\\man1.jpg",x,y);
        putimage(x,y,50,20,img,0,0,50,20);
        delimage(img);
    }
    else if(flag == 17)  //pink
    {
        PIMAGE img=newimage();
        getimage(img,"img\\man2.jpg",x,y);
        putimage(x,y,50,20,img,0,0,50,20);
        delimage(img);
    }else if(flag == 18)  //red
    {
        PIMAGE img=newimage();
        getimage(img,"img\\man3.jpg",x,y);
        putimage(x,y,50,20,img,0,0,50,20);
        delimage(img);
    }

}
int state()
{
    int flag = 1;
    snake *temp,*prear;
    snake *node = (snake *)malloc(sizeof(snake));
    node -> NEXT = Snake_Head;
    if(status == 1)     //a
    {
        node->x = Snake_Head->x-H;
        node->y = Snake_Head->y;
        head_dirction = 3;
    }
    else if( status == 2)   //d
    {
        node->x = Snake_Head->x + H;
        node->y = Snake_Head->y;
        head_dirction = 4;
    }
    else if (status == 3)  //w
    {
        node->x = Snake_Head->x;
        node->y = Snake_Head->y - H ;
        head_dirction = 1;
    }
    else if(status == 4)  //s
    {
        node->x = Snake_Head->x;
        node->y = Snake_Head->y + H;
        head_dirction = 2;
    }
    Snake_Head=node;
    temp=Snake_Head;
    while(temp->NEXT!=NULL)
    {
        prear=temp;
        temp=temp->NEXT;
    }
    free(temp);
    prear->NEXT=NULL;
    return flag;
}

snake *SNAKE_INIT()
{
    int i;
    snake *temp,*prear;
    Snake_Head = (snake *)malloc(sizeof(snake));
    Snake_Head->x= W;           ///设置随机生成蛇的位置
    Snake_Head->y= H;
    Snake_Head->NEXT=NULL;
    prear = Snake_Head;
    for(i=0; i<Sn_len; i++)            ///蛇身
    {
        temp = (snake *)malloc(sizeof(snake));
        temp->x=prear->x;
        temp->y=prear->y+30;
        prear->NEXT=temp;
        prear=temp;
    }
    prear ->NEXT = NULL;
    return Snake_Head;
}

void drawsnake()
{
    int x;
    drawsaNAKE(Snake_Head->x,Snake_Head->y,head_dirction);
    snake * temp = Snake_Head->NEXT;
    while(temp!=NULL)
    {
        x = 5;
        drawsaNAKE(temp->x,temp->y,x);
        temp = temp->NEXT;
    }
}

void game_strat_over(int flag)
{
    if(flag == 1)
    {
        PIMAGE img=newimage();
        getimage(img,"img\\bks.jpg",800,600);
        putimage(0,0,800,600,img,0,0,800,600);
        delimage(img);
        getch();
    }
    else if(flag == 0)
    {
        PIMAGE img=newimage();
        getimage(img,"img\\dead.jpg",800,600);
        putimage(0,0,800,600,img,0,0,800,600);
        delimage(img);
        Sleep(50);
    }
    else if(flag == 2)
    {
        PIMAGE img=newimage();
        getimage(img,"img\\dead.jpg",800,600);
        putimage(0,0,800,600,img,0,0,800,600);
        delimage(img);
    }

}
void gameloop()
{

    initgraph(IX,IY);
    long long time=0;

    MUSIC bkmusic;  // 背景音乐
    bkmusic.OpenFile("sounds\\bk.wav"); // 打开文件
    bkmusic.Play();

    setbkcolor(BLACK);
    game_strat_over(1);
    SNAKE_INIT();
    createfood();

    while(run)
    {
        man_flag = 15+random(4);
        cleardevice();
        drawsaNAKE(IX-175,0,man_flag);//blue
        drawsaNAKE(IX-150,0,talk_flag);
        drawsaNAKE(food->x,food->y,Food_Flag);
        drawsnake();
        run = Pding();
        Pscore();
        move();
        if(clock()-time>FPS)
        {
            state();
            time=clock();
        }
        delay_fps(60);
    }

    mus.OpenFile("sounds\\gg.wav"); // 打开文件
    mus.Play();
    game_strat_over(0);
    game_strat_over(2);
    getch();
    closegraph();
}

int main()
{
    gameloop();
}

相关的 路径需要更改,背景音乐被注释掉了,相关注释懒得打,自己看,不会的留言。

5.实际运行效果

  • 13
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值