user-0 提问: 用C写了一个贪吃蛇

回复:
user: user-0
content: 引用 10楼赵4老师 的回复:仅供参考：#include <conio.h> #include <windows.h>  void ConPrintAt(int x, int y, char *CharBuffer, int len) {    DWORD count;    COORD coord = {x, y};    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);    SetConsoleCursorPosition(hStdOut, coord);    WriteConsole(hStdOut, CharBuffer, len, &count, NULL); } void HideTheCursor() {    CONSOLE_CURSOR_INFO cciCursor;    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);     if(GetConsoleCursorInfo(hStdOut, &cciCursor))    {       cciCursor.bVisible = FALSE;       SetConsoleCursorInfo(hStdOut, &cciCursor);    } }  void ShowTheCursor() {    CONSOLE_CURSOR_INFO cciCursor;    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);     if(GetConsoleCursorInfo(hStdOut, &cciCursor))    {       cciCursor.bVisible = TRUE;       SetConsoleCursorInfo(hStdOut, &cciCursor);    } } void GetWH(int *w,int *h) {     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);     CONSOLE_SCREEN_BUFFER_INFO csbi;     if (GetConsoleScreenBufferInfo(hStdOut, &csbi)) {         *w=csbi.srWindow.Right;         *h=csbi.srWindow.Bottom;     } else {         *w=80;         *h=25;     } } void ClearConsole() {    //Get the handle to the current output buffer...    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);    //This is used to reset the carat/cursor to the top left.    COORD coord = {0, 0};    //A return value... indicating how many chars were written    //   not used but we need to capture this since it will be    //   written anyway (passing NULL causes an access violation).    DWORD count;    //This is a structure containing all of the console info    // it is used here to find the size of the console.    CONSOLE_SCREEN_BUFFER_INFO csbi;    //Here we will set the current color    if(GetConsoleScreenBufferInfo(hStdOut, &csbi))    {       //This fills the buffer with a given character (in this case 32=space).       FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);       FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);       //This will set our cursor position for the next print statement.       SetConsoleCursorPosition(hStdOut, coord);    } }  int main() {     unsigned short k;     int x,y,w,h;     char d;      SetConsoleOutputCP(437);     ClearConsole();     GetWH(&w,&h);     x=w/2;y=h/2;     HideTheCursor();     ConPrintAt(x,y,"O",1);     d='o';     while (1) {         Sleep(50);         if (kbhit()) {             k=getch();             if (27==k) break;//按Esc键退出             if (0==k||0xe0==k) k|=getch()<<8;//非字符键             switch (k) {                 case 0x48e0:case 0x04800://上                     d='u';                     if (y>0) {                         ConPrintAt(x,y," ",1);                         y--;                         ConPrintAt(x,y,"O",1);                     }                 break;                 case 0x50e0:case 0x05000://下                     d='d';                     if (y<h) {                         ConPrintAt(x,y," ",1);                         y++;                         ConPrintAt(x,y,"O",1);                     }                 break;                 case 0x4be0:case 0x04b00://左                     d='l';                     if (x>0) {                         ConPrintAt(x,y," ",1);                         x--;                         ConPrintAt(x,y,"O",1);                     }                 break;                 case 0x4de0:case 0x04d00://右                     d='r';                     if (x<w-1) {                         ConPrintAt(x,y," ",1);                         x++;                         ConPrintAt(x,y,"O",1);                     }                 break;             } //          cprintf("%04x pressed.\r\n",k);         } else {             switch (d) {                 case 'u':                     if (y>0) {                         ConPrintAt(x,y," ",1);                         y--;                         ConPrintAt(x,y,"O",1);                     }                 break;                 case 'd':                     if (y<h) {                         ConPrintAt(x,y," ",1);                         y++;                         ConPrintAt(x,y,"O",1);                     }                 break;                 case 'l':                     if (x>0) {                         ConPrintAt(x,y," ",1);                         x--;                         ConPrintAt(x,y,"O",1);                     }                 break;                 case 'r':                     if (x<w-1) {                         ConPrintAt(x,y," ",1);                         x++;                         ConPrintAt(x,y,"O",1);                     }                 break;             }         }     }     ClearConsole();     ShowTheCursor();     return 0; } 这..

user: user-0
content: 引用 9楼会飞的爱吃薄荷的猪 的回复:很好呀，继续努力。 我完了一会，最后结束的时候是一直往上走的，但蛇的身体不是直的，而且右边界好像有问题。 把图案修改一下 黑方块和黑圆块 应该是发上来时我改图案时出错了

user: user-1
content: 仅供参考：#include <conio.h> #include <windows.h>  void ConPrintAt(int x, int y, char *CharBuffer, int len) {    DWORD count;    COORD coord = {x, y};    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);    SetConsoleCursorPosition(hStdOut, coord);    WriteConsole(hStdOut, CharBuffer, len, &count, NULL); } void HideTheCursor() {    CONSOLE_CURSOR_INFO cciCursor;    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);     if(GetConsoleCursorInfo(hStdOut, &cciCursor))    {       cciCursor.bVisible = FALSE;       SetConsoleCursorInfo(hStdOut, &cciCursor);    } }  void ShowTheCursor() {    CONSOLE_CURSOR_INFO cciCursor;    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);     if(GetConsoleCursorInfo(hStdOut, &cciCursor))    {       cciCursor.bVisible = TRUE;       SetConsoleCursorInfo(hStdOut, &cciCursor);    } } void GetWH(int *w,int *h) {     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);     CONSOLE_SCREEN_BUFFER_INFO csbi;     if (GetConsoleScreenBufferInfo(hStdOut, &csbi)) {         *w=csbi.srWindow.Right;         *h=csbi.srWindow.Bottom;     } else {         *w=80;         *h=25;     } } void ClearConsole() {    //Get the handle to the current output buffer...    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);    //This is used to reset the carat/cursor to the top left.    COORD coord = {0, 0};    //A return value... indicating how many chars were written    //   not used but we need to capture this since it will be    //   written anyway (passing NULL causes an access violation).    DWORD count;    //This is a structure containing all of the console info    // it is used here to find the size of the console.    CONSOLE_SCREEN_BUFFER_INFO csbi;    //Here we will set the current color    if(GetConsoleScreenBufferInfo(hStdOut, &csbi))    {       //This fills the buffer with a given character (in this case 32=space).       FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);       FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);       //This will set our cursor position for the next print statement.       SetConsoleCursorPosition(hStdOut, coord);    } }  int main() {     unsigned short k;     int x,y,w,h;     char d;      SetConsoleOutputCP(437);     ClearConsole();     GetWH(&w,&h);     x=w/2;y=h/2;     HideTheCursor();     ConPrintAt(x,y,"O",1);     d='o';     while (1) {         Sleep(50);         if (kbhit()) {             k=getch();             if (27==k) break;//按Esc键退出             if (0==k||0xe0==k) k|=getch()<<8;//非字符键             switch (k) {                 case 0x48e0:case 0x04800://上                     d='u';                     if (y>0) {                         ConPrintAt(x,y," ",1);                         y--;                         ConPrintAt(x,y,"O",1);                     }                 break;                 case 0x50e0:case 0x05000://下                     d='d';                     if (y<h) {                         ConPrintAt(x,y," ",1);                         y++;                         ConPrintAt(x,y,"O",1);                     }                 break;                 case 0x4be0:case 0x04b00://左                     d='l';                     if (x>0) {                         ConPrintAt(x,y," ",1);                         x--;                         ConPrintAt(x,y,"O",1);                     }                 break;                 case 0x4de0:case 0x04d00://右                     d='r';                     if (x<w-1) {                         ConPrintAt(x,y," ",1);                         x++;                         ConPrintAt(x,y,"O",1);                     }                 break;             } //          cprintf("%04x pressed.\r\n",k);         } else {             switch (d) {                 case 'u':                     if (y>0) {                         ConPrintAt(x,y," ",1);                         y--;                         ConPrintAt(x,y,"O",1);                     }                 break;                 case 'd':                     if (y<h) {                         ConPrintAt(x,y," ",1);                         y++;                         ConPrintAt(x,y,"O",1);                     }                 break;                 case 'l':                     if (x>0) {                         ConPrintAt(x,y," ",1);                         x--;                         ConPrintAt(x,y,"O",1);                     }                 break;                 case 'r':                     if (x<w-1) {                         ConPrintAt(x,y," ",1);                         x++;                         ConPrintAt(x,y,"O",1);                     }                 break;             }         }     }     ClearConsole();     ShowTheCursor();     return 0; }

user: user-2
content: 很好呀，继续努力。 我完了一会，最后结束的时候是一直往上走的，但蛇的身体不是直的，而且右边界好像有问题。

user: user-0
content: 引用 3楼user-4 的回复:引用 2 楼 qq_38347379 的回复:[quote=引用 1楼user-4 的回复:]不错不错。。请问最后的问题有解不  代码没有细看，你是用带头结点的链表，还是头指针指向第一个结点？ 我写的一个贪吃蛇，没有用动态链表，用的C++的vector（动态数组）[/quote]第二种

user: user-3
content: she=(snake *)malloc(sizeof(snake *));     f1=(food *)malloc(sizeof(food *)); 改成这样的： she=(snake *)malloc(sizeof(snake));     f1=(food *)malloc(sizeof(food));

user: user-0
content: 引用 6楼user-4 的回复:参考： 《深入理解C指针》好吧 多谢

user: user-0
content: 引用 4楼自信男孩 的回复:she=(snake *)malloc(sizeof(snake *));     f1=(food *)malloc(sizeof(food *)); 改成这样的： she=(snake *)malloc(sizeof(snake));     f1=(food *)malloc(sizeof(food));好像没什么用

user: user-4
content: 参考： 《深入理解C指针》

user: user-4
content: 引用 2 楼 qq_38347379 的回复:引用 1楼user-4 的回复:不错不错。。请问最后的问题有解不  代码没有细看，你是用带头结点的链表，还是头指针指向第一个结点？ 我写的一个贪吃蛇，没有用动态链表，用的C++的vector（动态数组）

user: user-0
content: 引用 1楼user-4 的回复:不错不错。。请问最后的问题有解不

user: user-4
content: 不错不错。。