user-0 提问: 渣渣写的半成品贪吃蛇，为什么用键盘输入总是会延迟一步呢？

回复:
user: user-0
content: 引用 6 楼 zxh707wk 的回复:仅供参考 //#define WIN32_LEAN_AND_MEAN #define HEIGHT 20 #define WIDE 76 #define ICO "*"  int flage; int time_interval;  struct node {     int x;     int y;     struct node* next; };  void getpos(HANDLE hOut,COORD *pos) {     CONSOLE_SCREEN_BUFFER_INFO info;     GetConsoleScreenBufferInfo( hOut , &info );     *pos = info.dwCursorPosition; } void gotopos(HANDLE hOut,COORD pos) {     SetConsoleCursorPosition(hOut, pos); }  void put_logo() {     int i;     for(i=0; i<8; i++)     {         printf("\n");     }     for(i=0; i<35; i++)     {         printf(" ");     }     printf("snake");     Sleep(2000);     system("cls"); }  void start_map() {     int i;     int j;     for(i=0; i<HEIGHT+3; i++)     {         for(j=0; j<WIDE+3; j++)         {             if(i==0||i==HEIGHT+2)             {                 printf("=");             }             else if(j==0||j==WIDE+2)             {                 printf("|");             }             else             {                 printf(" ");             }         }         printf("\n");     } }  struct node* clear_last(HANDLE hOut,COORD pos,struct node* p,int x,int y) {     pos.X = p->x;     pos.Y = p->y;     gotopos(hOut,pos);     printf(" ");     p->x=x;     p->y=y;     return p->next; }  int check_ways(struct node* q,int x,int y,int len) {     int i;     if(x<1||x>WIDE+1)     {         return 1;     }     if(y<1||y>HEIGHT+1)     {         return 1;     }     for(i=0; i<len; i++)     {         if(q->x==x&&q->y==y)         {             return 1;         }         q=q->next;     }     return 0; }  DWORD WINAPI  snake() {     struct node snakes;     struct node* head;     struct node* p;     struct node* q;     HANDLE hOut;     COORD pos;     int i;     int len;     len=10;     snakes.x=1;     snakes.y=1;     snakes.next=NULL;     pos.X=snakes.x;     pos.Y=snakes.y;     start_map();     hOut = GetStdHandle(STD_OUTPUT_HANDLE);     gotopos(hOut,pos);     head=p=q=malloc(sizeof(struct node));     p->x=snakes.x++;     p->y=snakes.y;     printf(ICO);     p->next=q;     for(i=0; i<len-1; i++)     {         q=p;         p=malloc(sizeof(struct node));         p->x=snakes.x++;         p->y=snakes.y;         printf(ICO);         p->next=head;         q->next=p;     }     printf(ICO);     snakes.x=snakes.x-1;     p=head;     for(;; Sleep(time_interval*50))     {         switch(flage)         {         case 1:             snakes.y--;             break;         case 2:             snakes.y++;             break;         case 3:             snakes.x--;             break;         case 4:             snakes.x++;             break;         default :             goto _NEXT;             break;         }         if(check_ways(head,snakes.x,snakes.y,len))         {             goto _END;         }         p=clear_last(hOut,pos,p,snakes.x,snakes.y);         pos.X=snakes.x;         pos.Y=snakes.y;         gotopos(hOut,pos);         printf(ICO);         printf("\b"); _NEXT:         ;     } _END:     ;     pos.X=25;     pos.Y=10;     gotopos(hOut,pos);     printf("game over!you have %d long snakes\b",len); } E:\编程\snake\snake.c|17|error: unknown type name 'HANDLE'|

user: user-1
content: 引用 9 楼 aa2303632 的回复:大写字母的变量都没有定义你用什么编译器的？

user: user-0
content: 引用 8 楼 zxh707wk 的回复:[quote=引用 7 楼 aa2303632 的回复:]E:\编程\snake\snake.c|17|error: unknown type name 'HANDLE'| 忘给main.c了 #define WIN32_LEAN_AND_MEAN  #include <stdio.h> #include <stdlib.h> #include <windows.h> #include "snake.h"  int main() {     HANDLE hThrd;     DWORD threadId;     flage=4;     time_interval=10;     hThrd=CreateThread(NULL,0,snake,(LPVOID)0,0,&threadId);     for(;;)     {         switch(getch())         {         case 224:             switch(getch())             {             case 72:                 if(flage!=2&&flage>0)                 {                     flage=1;                 }                 break;             case 80:                 if(flage!=1&&flage>0)                 {                     flage=2;                 }                 break;             case 75:                 if(flage!=4&&flage>0)                 {                     flage=3;                 }                 break;             case 77:                 if(flage!=3&&flage>0)                 {                     flage=4;                 }                 break;             }             break;         case 32:             flage=-flage;             break;         case 'w':             if(time_interval<200)             {                 time_interval=time_interval+1;             }             break;         case 'e':             if(time_interval>1)             {                 time_interval=time_interval-1;             }             break;         }         Sleep(100);     }     getchar();     return 0; } [/quote]大写字母的变量都没有定义

user: user-1
content: 引用 7 楼 aa2303632 的回复:E:\编程\snake\snake.c|17|error: unknown type name 'HANDLE'| 忘给main.c了 #define WIN32_LEAN_AND_MEAN  #include <stdio.h> #include <stdlib.h> #include <windows.h> #include "snake.h"  int main() {     HANDLE hThrd;     DWORD threadId;     flage=4;     time_interval=10;     hThrd=CreateThread(NULL,0,snake,(LPVOID)0,0,&threadId);     for(;;)     {         switch(getch())         {         case 224:             switch(getch())             {             case 72:                 if(flage!=2&&flage>0)                 {                     flage=1;                 }                 break;             case 80:                 if(flage!=1&&flage>0)                 {                     flage=2;                 }                 break;             case 75:                 if(flage!=4&&flage>0)                 {                     flage=3;                 }                 break;             case 77:                 if(flage!=3&&flage>0)                 {                     flage=4;                 }                 break;             }             break;         case 32:             flage=-flage;             break;         case 'w':             if(time_interval<200)             {                 time_interval=time_interval+1;             }             break;         case 'e':             if(time_interval>1)             {                 time_interval=time_interval-1;             }             break;         }         Sleep(100);     }     getchar();     return 0; }

user: user-1
content: 仅供参考 //#define WIN32_LEAN_AND_MEAN #define HEIGHT 20 #define WIDE 76 #define ICO "*"  int flage; int time_interval;  struct node {     int x;     int y;     struct node* next; };  void getpos(HANDLE hOut,COORD *pos) {     CONSOLE_SCREEN_BUFFER_INFO info;     GetConsoleScreenBufferInfo( hOut , &info );     *pos = info.dwCursorPosition; } void gotopos(HANDLE hOut,COORD pos) {     SetConsoleCursorPosition(hOut, pos); }  void put_logo() {     int i;     for(i=0; i<8; i++)     {         printf("\n");     }     for(i=0; i<35; i++)     {         printf(" ");     }     printf("snake");     Sleep(2000);     system("cls"); }  void start_map() {     int i;     int j;     for(i=0; i<HEIGHT+3; i++)     {         for(j=0; j<WIDE+3; j++)         {             if(i==0||i==HEIGHT+2)             {                 printf("=");             }             else if(j==0||j==WIDE+2)             {                 printf("|");             }             else             {                 printf(" ");             }         }         printf("\n");     } }  struct node* clear_last(HANDLE hOut,COORD pos,struct node* p,int x,int y) {     pos.X = p->x;     pos.Y = p->y;     gotopos(hOut,pos);     printf(" ");     p->x=x;     p->y=y;     return p->next; }  int check_ways(struct node* q,int x,int y,int len) {     int i;     if(x<1||x>WIDE+1)     {         return 1;     }     if(y<1||y>HEIGHT+1)     {         return 1;     }     for(i=0; i<len; i++)     {         if(q->x==x&&q->y==y)         {             return 1;         }         q=q->next;     }     return 0; }  DWORD WINAPI  snake() {     struct node snakes;     struct node* head;     struct node* p;     struct node* q;     HANDLE hOut;     COORD pos;     int i;     int len;     len=10;     snakes.x=1;     snakes.y=1;     snakes.next=NULL;     pos.X=snakes.x;     pos.Y=snakes.y;     start_map();     hOut = GetStdHandle(STD_OUTPUT_HANDLE);     gotopos(hOut,pos);     head=p=q=malloc(sizeof(struct node));     p->x=snakes.x++;     p->y=snakes.y;     printf(ICO);     p->next=q;     for(i=0; i<len-1; i++)     {         q=p;         p=malloc(sizeof(struct node));         p->x=snakes.x++;         p->y=snakes.y;         printf(ICO);         p->next=head;         q->next=p;     }     printf(ICO);     snakes.x=snakes.x-1;     p=head;     for(;; Sleep(time_interval*50))     {         switch(flage)         {         case 1:             snakes.y--;             break;         case 2:             snakes.y++;             break;         case 3:             snakes.x--;             break;         case 4:             snakes.x++;             break;         default :             goto _NEXT;             break;         }         if(check_ways(head,snakes.x,snakes.y,len))         {             goto _END;         }         p=clear_last(hOut,pos,p,snakes.x,snakes.y);         pos.X=snakes.x;         pos.Y=snakes.y;         gotopos(hOut,pos);         printf(ICO);         printf("\b"); _NEXT:         ;     } _END:     ;     pos.X=25;     pos.Y=10;     gotopos(hOut,pos);     printf("game over!you have %d long snakes\b",len); }

user: user-0
content: 谁来告诉我下吧。。

user: user-0
content: 引用 3 楼 zxh707wk 的回复:snake.c //#define WIN32_LEAN_AND_MEAN #define HEIGHT 20 #define WIDE 76 #define ICO "*"  int flage; int time_interval;  struct node {     int x;     int y;     struct node* next; };  void getpos(HANDLE hOut,COORD *pos) {     CONSOLE_SCREEN_BUFFER_INFO info;     GetConsoleScreenBufferInfo( hOut , &info );     *pos = info.dwCursorPosition; } void gotopos(HANDLE hOut,COORD pos) {     SetConsoleCursorPosition(hOut, pos); }  void put_logo() {     int i;     for(i=0; i<8; i++)     {         printf("\n");     }     for(i=0; i<35; i++)     {         printf(" ");     }     printf("snake");     Sleep(2000);     system("cls"); }  void start_map() {     int i;     int j;     for(i=0; i<HEIGHT+3; i++)     {         for(j=0; j<WIDE+3; j++)         {             if(i==0||i==HEIGHT+2)             {                 printf("=");             }             else if(j==0||j==WIDE+2)             {                 printf("|");             }             else             {                 printf(" ");             }         }         printf("\n");     } }  struct node* clear_last(HANDLE hOut,COORD pos,struct node* p,int x,int y) {     pos.X = p->x;     pos.Y = p->y;     gotopos(hOut,pos);     printf(" ");     p->x=x;     p->y=y;     return p->next; }  int check_ways(struct node* q,int x,int y,int len) {     int i;     if(x<1||x>WIDE+1)     {         return 1;     }     if(y<1||y>HEIGHT+1)     {         return 1;     }     for(i=0; i<len; i++)     {         if(q->x==x&&q->y==y)         {             return 1;         }         q=q->next;     }     return 0; }  DWORD WINAPI  snake() {     struct node snakes;     struct node* head;     struct node* p;     struct node* q;     HANDLE hOut;     COORD pos;     int i;     int len;     len=10;     snakes.x=1;     snakes.y=1;     snakes.next=NULL;     pos.X=snakes.x;     pos.Y=snakes.y;     start_map();     hOut = GetStdHandle(STD_OUTPUT_HANDLE);     gotopos(hOut,pos);     head=p=q=malloc(sizeof(struct node));     p->x=snakes.x++;     p->y=snakes.y;     printf(ICO);     p->next=q;     for(i=0; i<len-1; i++)     {         q=p;         p=malloc(sizeof(struct node));         p->x=snakes.x++;         p->y=snakes.y;         printf(ICO);         p->next=head;         q->next=p;     }     printf(ICO);     snakes.x=snakes.x-1;     p=head;     for(;; Sleep(time_interval*50))     {         switch(flage)         {         case 1:             snakes.y--;             break;         case 2:             snakes.y++;             break;         case 3:             snakes.x--;             break;         case 4:             snakes.x++;             break;         default :             goto _NEXT;             break;         }         if(check_ways(head,snakes.x,snakes.y,len))         {             goto _END;         }         p=clear_last(hOut,pos,p,snakes.x,snakes.y);         pos.X=snakes.x;         pos.Y=snakes.y;         gotopos(hOut,pos);         printf(ICO);         printf("\b"); _NEXT:         ;     } _END:     ;     pos.X=25;     pos.Y=10;     gotopos(hOut,pos);     printf("game over!you have %d long snakes\b",len); }  snake.h #include "snake.c"  void put_logo(); void getpos(HANDLE hOut,COORD *pos); void gotopos(HANDLE hOut,COORD pos); DWORD WINAPI  snake();  main.c #define WIN32_LEAN_AND_MEAN  #include <stdio.h> #include <stdlib.h> #include <windows.h> #include "snake.h"  int main() {     HANDLE hThrd;     DWORD threadId;     flage=4;     time_interval=10;     hThrd=CreateThread(NULL,0,snake,(LPVOID)0,0,&threadId);     for(;;)     {         switch(getch())         {         case 224:             switch(getch())             {             case 72:                 if(flage!=2&&flage>0)                 {                     flage=1;                 }                 break;             case 80:                 if(flage!=1&&flage>0)                 {                     flage=2;                 }                 break;             case 75:                 if(flage!=4&&flage>0)                 {                     flage=3;                 }                 break;             case 77:                 if(flage!=3&&flage>0)                 {                     flage=4;                 }                 break;             }             break;         case 32:             flage=-flage;             break;         case 'w':             if(time_interval<200)             {                 time_interval=time_interval+1;             }             break;         case 'e':             if(time_interval>1)             {                 time_interval=time_interval-1;             }             break;         }         Sleep(100);     }     getchar();     return 0; } 那我到底哪里有问题？

user: user-1
content: snake.c //#define WIN32_LEAN_AND_MEAN #define HEIGHT 20 #define WIDE 76 #define ICO "*"  int flage; int time_interval;  struct node {     int x;     int y;     struct node* next; };  void getpos(HANDLE hOut,COORD *pos) {     CONSOLE_SCREEN_BUFFER_INFO info;     GetConsoleScreenBufferInfo( hOut , &info );     *pos = info.dwCursorPosition; } void gotopos(HANDLE hOut,COORD pos) {     SetConsoleCursorPosition(hOut, pos); }  void put_logo() {     int i;     for(i=0; i<8; i++)     {         printf("\n");     }     for(i=0; i<35; i++)     {         printf(" ");     }     printf("snake");     Sleep(2000);     system("cls"); }  void start_map() {     int i;     int j;     for(i=0; i<HEIGHT+3; i++)     {         for(j=0; j<WIDE+3; j++)         {             if(i==0||i==HEIGHT+2)             {                 printf("=");             }             else if(j==0||j==WIDE+2)             {                 printf("|");             }             else             {                 printf(" ");             }         }         printf("\n");     } }  struct node* clear_last(HANDLE hOut,COORD pos,struct node* p,int x,int y) {     pos.X = p->x;     pos.Y = p->y;     gotopos(hOut,pos);     printf(" ");     p->x=x;     p->y=y;     return p->next; }  int check_ways(struct node* q,int x,int y,int len) {     int i;     if(x<1||x>WIDE+1)     {         return 1;     }     if(y<1||y>HEIGHT+1)     {         return 1;     }     for(i=0; i<len; i++)     {         if(q->x==x&&q->y==y)         {             return 1;         }         q=q->next;     }     return 0; }  DWORD WINAPI  snake() {     struct node snakes;     struct node* head;     struct node* p;     struct node* q;     HANDLE hOut;     COORD pos;     int i;     int len;     len=10;     snakes.x=1;     snakes.y=1;     snakes.next=NULL;     pos.X=snakes.x;     pos.Y=snakes.y;     start_map();     hOut = GetStdHandle(STD_OUTPUT_HANDLE);     gotopos(hOut,pos);     head=p=q=malloc(sizeof(struct node));     p->x=snakes.x++;     p->y=snakes.y;     printf(ICO);     p->next=q;     for(i=0; i<len-1; i++)     {         q=p;         p=malloc(sizeof(struct node));         p->x=snakes.x++;         p->y=snakes.y;         printf(ICO);         p->next=head;         q->next=p;     }     printf(ICO);     snakes.x=snakes.x-1;     p=head;     for(;; Sleep(time_interval*50))     {         switch(flage)         {         case 1:             snakes.y--;             break;         case 2:             snakes.y++;             break;         case 3:             snakes.x--;             break;         case 4:             snakes.x++;             break;         default :             goto _NEXT;             break;         }         if(check_ways(head,snakes.x,snakes.y,len))         {             goto _END;         }         p=clear_last(hOut,pos,p,snakes.x,snakes.y);         pos.X=snakes.x;         pos.Y=snakes.y;         gotopos(hOut,pos);         printf(ICO);         printf("\b"); _NEXT:         ;     } _END:     ;     pos.X=25;     pos.Y=10;     gotopos(hOut,pos);     printf("game over!you have %d long snakes\b",len); }  snake.h #include "snake.c"  void put_logo(); void getpos(HANDLE hOut,COORD *pos); void gotopos(HANDLE hOut,COORD pos); DWORD WINAPI  snake();  main.c #define WIN32_LEAN_AND_MEAN  #include <stdio.h> #include <stdlib.h> #include <windows.h> #include "snake.h"  int main() {     HANDLE hThrd;     DWORD threadId;     flage=4;     time_interval=10;     hThrd=CreateThread(NULL,0,snake,(LPVOID)0,0,&threadId);     for(;;)     {         switch(getch())         {         case 224:             switch(getch())             {             case 72:                 if(flage!=2&&flage>0)                 {                     flage=1;                 }                 break;             case 80:                 if(flage!=1&&flage>0)                 {                     flage=2;                 }                 break;             case 75:                 if(flage!=4&&flage>0)                 {                     flage=3;                 }                 break;             case 77:                 if(flage!=3&&flage>0)                 {                     flage=4;                 }                 break;             }             break;         case 32:             flage=-flage;             break;         case 'w':             if(time_interval<200)             {                 time_interval=time_interval+1;             }             break;         case 'e':             if(time_interval>1)             {                 time_interval=time_interval-1;             }             break;         }         Sleep(100);     }     getchar();     return 0; }

user: user-2
content: 给楼主分享一个开源中国社区中的一个贪吃蛇游戏的代码链接，可以参考学习： http://www.oschina.net/code/snippet_261347_26002 下面是运行界面：

user: user-2
content: 我把你的程序粘到vs2010里运行，运行的屏幕为啥是一闪一闪的？