e[i][0],snake[i][1]);color(14);cout<<"★"<<endl;}if(kbhit()){gotoxy(0,N+2);ch=getche();}switch(ch){case'w':snake[0][1]--;break;case's':snake[0][1]++;break;case'a':snake[0][0]--;break;case'd':snake[0][0]++;break;default:break;}gotoxy(snake[0][0],snake[0][1]);color(14);cout<<"★"<<endl;Sleep(abs(200-0.5*score));if(snake[0][0]==apple[0]&&snake[0][1]==apple[1])//吃掉苹果后蛇分数加1,蛇长加1{score++;len++;snake=(int**)realloc(snake,sizeof(int*)*len);snake[len-1]=(int*)malloc(sizeof(int)*2);apple[0]=rand()%N+1;apple[1]=rand()%N+1;gotoxy(apple[0],apple[1]);color(12);cout<<"●"<<endl;gotoxy(N+5,3);color(20);cout<<score<<endl;}if(snake[0][1]==0||snake[0][1]==N||snake[0][0]==0||snake[0][0]==N)//撞到围墙后失败{gotoxy(N/2,N/2);color(30);cout<<"失败!!!"<<endl;for(i=0;i<len;i++)free(snake[i]);Sleep(INFinitE);exit(0);}}return0;}