[0]=RIGHT_EDGE; turn_point[0][1]=0; /*游戏参数初始化完毕*/ generate_food();//投下第一粒食物 gotoxy(init_position[0],init_position[1]);//将光标移动到到初始化位置 for(i=snake_length;i>0;i--){ putchar(snake[i-1]); } } /*接收键盘控制命令*/ void control(){ char command;//存放接收到命令 while(1){ command=getch(); if(command==-32){ //F11,F12:-123,-122 command=getch(); if(command=='H' && (direction==LEFT || direction==RIGHT)){//光标上移 direction=UP; turn_point[head_turn_num%TURN_NUM][0]=head[0]; turn_point[head_turn_num%TURN_NUM][1]=head[1]; head_turn_num++; }else if(command=='P' && (direction==LEFT || direction==RIGHT)){//光标下移 direction=DOWN; turn_point[head_turn_num%TURN_NUM][0]=head[0]; turn_point[head_turn_num%TURN_NUM][1]=head[1]; head_turn_num++; }else if(command=='K' && (direction==UP || direction==DOWN)){//光标左移 direction=LEFT; turn_point[head_turn_num%TURN_NUM][0]=head[0]; turn_point[head_turn_num%TURN_NUM][1]=head[1]; head_turn_num++; }else if(command=='M' && (direction==UP || direction==DOWN)){//光标右移 direction=RIGHT; turn_point[head_turn_num%TURN_NUM][0]=head[0]; turn_point[head_turn_num%TURN_NUM][1]=head[1]; head_turn_num++; }else if(command==-122 || command==-123); }else if(command==0){ command=getch();//接收Fn的下一个字符 //F1~F10:59~68 }else if(command>=1&&command<=26){ //Ctrl+a~z:1~26 if(command==3){ break; } }else{ //do nothing! } move(); if(head[0]==food[0] && head[1]==food[1]){ eat_food();//吃掉一粒事物 generate_food();//投下新食物 } } } /*蛇的身体运动*/ void move(){ if(direction==UP){ //printf("Moving up!\n"); move_up(); }else if(direction==DOWN){ //printf("Moving down!\n"); move_down(); }else if(direction==LEFT){ //printf("Moving left!\n"); move_left(); }else if(direction==RIGHT){ //printf("Moving right!\n"); move_right(); } } /*向上运动*/ void move_up(){ if(cursor[1]>=UP_EDGE){ gotoxy(head[0],head[1]); putchar('*'); gotoxy(head[0],head[1]-1); putchar(snake[0]); gotoxy(tail[0],tail[1]); //_sleep(1000);//查看尾部光标 putchar(0); update_tail_position(); head[1]-=1; cursor[0]=head[0]; cursor[1]=head[1]-1; gotoxy(cursor[0],cursor[1]); }else{ gotoxy(head[0],head[1]); } } /*向下运动*/ void move_down(){ if(cursor[1]<=DOWN_EDGE){ gotoxy(head[0],head[1]); putchar('*'); gotoxy(head[0],head[1]+1); putchar(snake[0]); gotoxy(tail[0],tail[1]); //_sleep(1000);//查看尾部光标 putchar(0); update_tail_position(); head[1]+=1; cursor[0]=head[0]; cursor[1]=head[1]+1; gotoxy(cursor[0],cursor[1]); }else{ gotoxy(head[0],head[1]); } } /*向左运动*/ void move_left(){ if(cursor[0]>=LEFT_EDGE){ gotoxy(head[0],head[1]); putchar('*'); gotoxy(head[0]-1,head[1]); putchar(snake[0]); gotoxy(tail[0],tail[1]); //_sleep(1000);//查看尾部光标 putchar(0); update_tail_position(); head[0]-=1; cursor[0]=head[0]-1; cursor[1]=head[1]; gotoxy(cursor[0],cursor[1]); }else{ gotoxy(head[0],head[1]); } } /*向右运动*/ void move_right(){ if(cursor[0]<=RIGHT_EDGE){ gotoxy(head[0],head[1]); putchar('*'); putchar(snake[0]); gotoxy(tail[0],tail[1]); //_sleep(1000);//查看尾部光标 putchar(0); update_tail_position(); head[0]+=1; cursor[0]= |