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

#define mal (snake*)malloc(sizeof(snake))

#define max 21

#include <windows.h>

#include <stdlib.h>

#include <time.h>

#include <stdio.h>

#include <string.h>

#include <conio.h>


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;i<=max+1;i++)

{

gotoxy(0,i);

printf("■");

Sleep(4);

gotoxy(2*i,0);

printf("■");

Sleep(4);

}

for(i=1;i<=max+1;i++)

{

gotoxy(2*i,max+1);

printf("■");

Sleep(4);

gotoxy(2*max+2,i);

printf("■");

Sleep(4);

}

}


void snakeon()

{

snake *head=stail;

while(head!=NULL)

{

map[head->x][head->y]='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[i][0];

int y=shead->y+direct[i][1];

if(x<=0||y<=0||x>max||y>max)

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[i][0];

shead->y+=direct[i][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;

}


qq_乄朩_04191119
浏览 1658回答 0
0回答
打开App,查看更多内容
随时随地看视频慕课网APP