• 手机版

    扫码体验手机版

  • 微信公众号

    扫码关注公众号

国内首家协议开发

软芯音视解码保护平台

在线
客服

发布
需求

在线
聊天

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

这是一段同学写的贪吃蛇代码 请大神帮忙写一下详细注释 随便看看有什么

#define mal (snake*)malloc(sizeof(snake))
#define max 21
#include
#include
#include
#include
#include
#include


char map[max+1][max+1];
char moodword[6][50]={{"我觉得明珠真的没有我能吃。。。"},
                                                {"好开心吃吃吃!!!"},
                                                {"明珠都没我能吃哈哈哈!!!"},
                                                {"看我今天胃口好大!!!"},
                                                {"顺丰和真乐是真爱。。。真的。。。"},
                                                {"顺丰和真乐是真爱!!!!!"}};
char badmoodword[5][40]={{"痛痛痛痛痛痛痛痛痛!!!"},
                                                {"痛~~~我今天一定是感冒了。。。"},
                                                {"痛~~~越痛越快乐yeah!yeah!yeah!"},
                                                {"疼不要紧,关键是短了。。。"},
                                                {"QAQ。。。。。。"}};
int direct[4][2]={-1,0,1,0,0,-1,0,1};
int grades;
int guoguo;
struct snake
{
        int x;
        int y;
        snake *next;
        snake *prev;
}*stail,*shead;


void gotoxy(int x, int y)    //输出坐标
{
        COORD pos;
        pos.X = x;
        pos.Y = y;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}


void color(int b)         //颜色函数
{
    HANDLE hConsole = GetStdHandle((STD_OUTPUT_HANDLE)) ;
    SetConsoleTextAttribute(hConsole,b) ;
}


void gameover();


void screenon()
{
        int i;
        for(i=0;iy]='s';
                gotoxy(2*head->y,head->x);
                printf("#");
                head=head->next;
        }
        gotoxy(2*shead->y,shead->x);
        printf("#");
}


void createsnake()
{
        stail=mal;
        shead=mal;
        stail->next=shead;
        shead->prev=stail;
        stail->prev=shead->next=NULL;
        stail->x=stail->y=2;
        shead->x=2,shead->y=3;
}


void getguoguo()
{
        int x=rand()%max+1;
        int y=rand()%max+1;
        for(;map[x][y]!=' ';)
        {
                x=rand()%max+1;
                y=rand()%max+1;
        }
        map[x][y]='a';
        gotoxy(2*y,x);
        printf("¤");
}


void mood()
{
        gotoxy(0,max+2);
        printf("                               ");
        gotoxy(0,max+2);
        printf("%s",moodword[rand()%6]);
        gotoxy(0,max+2);
}


void badmood()
{
        gotoxy(0,max+2);
        printf("                               ");
        gotoxy(0,max+2);
        printf("%s",badmoodword[rand()%5]);
        gotoxy(0,max+2);
}


void move(char ch)
{
        int i;
        snake *head=stail;
        switch(ch)
        {
                case 'W':
                case 'w':i=0;break;
                case 'S':
                case 's':i=1;break;
                case 'A':
                case 'a':i=2;break;
                case 'D':
                case 'd':i=3;break;
                default:return;
        }
        int x=shead->x+direct[0];
        int y=shead->y+direct[1];
        if(xmax)
                gameover();


        if(guoguo)
        {
                guoguo--;
                head=mal;
                head->next=stail;
                stail->prev=head;
                head->x=stail->x;
                head->y=stail->y;
                stail=head;
        }
        else
        {
                map[stail->x][stail->y]=' ';
                gotoxy(2*stail->y,stail->x);
                printf("  ");
        }
        while(head->next!=NULL)
        {
                head->x=head->next->x;
                head->y=head->next->y;
                head=head->next;
        }
        gotoxy(2*shead->y,shead->x);
        printf("#");
        shead->x+=direct[0];
        shead->y+=direct[1];
        if(map[shead->x][shead->y]=='a')
        {
                guoguo++;//吃到果子
                grades+=10;
                mood();
        }
        else if(map[shead->x][shead->y]=='s')
        {
                for(head=stail;head->x!=shead->x||head->y!=shead->y;)
                {
                        gotoxy(2*head->y,head->x);
                        printf("  ");
                        map[head->x][head->y]=' ';
                        grades-=3;
                        head=head->next;
                }
                gotoxy(2*head->y,head->x);
                printf("  ");
                map[head->x][head->y]=' ';
                head=head->next;
                stail=head;
                badmood();
        }//咬自己
        map[shead->x][shead->y]='s';
        gotoxy(2*shead->y,shead->x);
        printf("#");
        if(guoguo)
                getguoguo();
        Sleep(120-grades/4);
}


void game()
{
        grades=guoguo=0;
        memset(map,' ',sizeof(map));
        srand((unsigned)time(NULL));
        createsnake();
        screenon();
        gotoxy(2*max+5,0);
        printf("wsad上下左右 p暂停 esc结束");
        snakeon();
        getguoguo();
        gotoxy(0,max+2);
        char ch='p',c;
        while(ch!=27)
        {
                c=ch;
                if(kbhit())
                {
                        ch=getch();
                        switch(ch)
                        {
                        case 27:exit(0);
                        case 'P':
                        case 'p':break;
                        case 'W':
                        case 'w':if(c=='s'||c=='S')ch='s';break;
                        case 'S':
                        case 's':if(c=='w'||c=='W')ch='w';break;
                        case 'A':
                        case 'a':if(c=='d'||c=='D')ch='d';break;
                        case 'D':
                        case 'd':if(c=='a'||c=='A')ch='a';break;
                        default:ch=c;break;
                        }
                }
                move(ch);
        }
}


void gameover()
{
        stail=stail->next;
        for(;stail->next!=NULL;)
        {
                free(stail->prev);
                stail=stail->next;
        }
        free(stail);
        shead=stail=NULL;
        char ch;
        gotoxy(2*max+6,2);
        printf("你的蛇蛇撞死了 QAQ\n");
        gotoxy(2*max+6,4);
        printf("它吃到了分数为%d的果果\n",grades);
        gotoxy(2*max+6,5);
        printf("简直死不瞑目。。。\n");
        gotoxy(2*max+6,7);
        //yanse
        printf("要重来咩?(按y重新开始,esc结束)");
        for(;;)
        {
                ch=getch();
                if(ch=='y')
                        system("cls"),
                        game();
                else if(ch==27)
                {
                        gotoxy(2*max+6,9);
                        system("pause");
                        gotoxy(2*max+6,10);
                        exit(0);
                }
        }
}
int main()
{
        game();
        return 0;
}

免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。

版权声明:作者保留权利,不代表天盟立场。

使用道具 举报

发新帖

发布任务需求已有1031169位用户正在使用天盟网服务

发布分类: *
任务预算: *
需求内容: *
手机号码: *
任务商家报价为
  • 预算价 :
  • 成交价 :
  • 完工期 :
  • 质保期 :

* 最终任务项目以服务商报价、双方协商为准!