读书人

TC学习记录之-贪吃蛇,暂时每次只有一个

发布时间: 2012-05-24 11:55:41 作者: rapoo

TC学习记录之---贪吃蛇,暂时每次只有一个拐点

C/C++ code
#include "stdio.h"#include "stdlib.h"#define esckey 27#define upkey 72#define downkey 80#define leftkey 75#define rightkey 77#define inceptline 10#define inceptrow 1#define graphicswidth 60#define graphicshigh 25#define linelimit (inceptline + graphicswidth)#define old_orientation 0#define orientation_up 1#define orientation_down 2#define orientation_left 3#define orientation_right 4#define she_long0 3#define wall_x '#'#define wall_y '#'#define tu_x '#'#define tu_y '#'#define food '$'main(){    int i,key_ls,she_0,she_long=she_long0,she_newlong=0,she_oldlong=0,new_orientation=old_orientation,orientation;    int inflexion_x,inflexion_y,snake_x,snake_y,snake_x_ls,snake_y_ls,food_randx,food_randy;    system("cls");    srand(time(NULL)+clock());    snake_x=rand()%(graphicswidth-4-5)+inceptline+5;    snake_y=rand()%(graphicshigh-4-5)+5;    orientation=rand()%4+1;    food_randx=rand()%(graphicswidth-4-5)+inceptline+5;    food_randy=rand()%(graphicshigh-4-5)+5;while(1){    gotoxy(food_randx,food_randy);    printf("%c",food);    gotoxy(inceptline,inceptrow);for (i=inceptline;i<=linelimit;i++)    {printf("%c",wall_x);}    gotoxy(inceptline,graphicshigh);for (i=inceptline;i<=linelimit;i++)    {printf("%c",wall_x);}for (i=inceptrow+1;i<graphicshigh;i++)    {gotoxy(inceptline,i);    printf("%c",wall_y);}for (i=inceptrow+1;i<graphicshigh;i++)    {gotoxy(inceptline+graphicswidth,i);    printf("%c",wall_y);}while ((snake_x==food_randx)&(snake_y==food_randy)||(((snake_x_ls==food_randx)&(inflexion_y==food_randy))||((snake_y_ls==food_randy)&(inflexion_x==food_randx)))){    she_long++;    food_randx=rand()%(graphicswidth-4-5)+inceptline+5;    food_randy=rand()%(graphicshigh-4-5)+5;    gotoxy(food_randx,food_randy);    printf("%c",food);}snake_x_ls=snake_x;snake_y_ls=snake_y;if (new_orientation!=0){    snake_x_ls=inflexion_x;    snake_y_ls=inflexion_y;}switch(orientation){case orientation_up:switch(new_orientation){    case orientation_left: /*up->left*/if(she_newlong++<she_long-1){    for(she_0=0;she_0<she_newlong;she_0++)    {    gotoxy(--snake_x_ls,inflexion_y);    printf("%c",tu_x);    }she_oldlong=she_long-she_newlong;if(she_oldlong>0){    for(she_0=0;she_0<she_oldlong;she_0++)    {    gotoxy(inflexion_x,snake_y_ls++);    printf("%c",tu_y);    }}    }if(she_newlong==she_long-1)    {    snake_x=inflexion_x-she_long;    she_newlong=0;    she_oldlong=0;    new_orientation=0;    orientation=orientation_left;    }    break;case orientation_right: /*up->right*/if(she_newlong++<she_long){    for(she_0=0;she_0<she_newlong;she_0++)    {    gotoxy(++snake_x_ls,inflexion_y);    printf("%c",tu_x);    }she_oldlong=she_long-she_newlong;if(she_oldlong>0){    for(she_0=0;she_0<she_oldlong;she_0++)    {    gotoxy(inflexion_x,snake_y_ls++);    printf("%c",tu_y);    }}    }if(she_newlong==she_long)    {    snake_x=inflexion_x+she_long;    she_newlong=0;    she_oldlong=0;    new_orientation=0;    orientation=orientation_right;    }    break;case old_orientation:    for (she_0=0;she_0<she_long;she_0++)    {gotoxy(snake_x_ls,snake_y_ls++);    printf("%c",tu_y);}}    break;case orientation_down:switch(new_orientation){    case orientation_left: /*down->left*/if(she_newlong++<she_long){    for(she_0=0;she_0<she_newlong;she_0++)    {    gotoxy(--snake_x_ls,inflexion_y);    printf("%c",tu_x);    }she_oldlong=she_long-she_newlong;if(she_oldlong>0){    for(she_0=0;she_0<she_oldlong;she_0++)    {    gotoxy(inflexion_x,snake_y_ls--);    printf("%c",tu_y);    }}    }if(she_newlong==she_long)    {    snake_x=inflexion_x-she_long;    she_newlong=0;    she_oldlong=0;    new_orientation=0;    orientation=orientation_left;    }    break;case orientation_right: /*down->right*/if(she_newlong++<she_long){    for(she_0=0;she_0<she_newlong;she_0++)    {    gotoxy(++snake_x_ls,inflexion_y);    printf("%c",tu_x);    }she_oldlong=she_long-she_newlong;if(she_oldlong>0){    for(she_0=0;she_0<she_oldlong;she_0++)    {    gotoxy(inflexion_x,snake_y_ls--);    printf("%c",tu_y);    }}    }if(she_newlong==she_long)    {    snake_x=inflexion_x+she_long;    she_newlong=0;    she_oldlong=0;    new_orientation=0;    orientation=orientation_right;    }    break;case old_orientation:    for (she_0=0;she_0<she_long;she_0++)    {gotoxy(snake_x_ls,snake_y_ls--);    printf("%c",tu_y);}}    break;case orientation_left:switch(new_orientation){    case orientation_up: /*left->up*/if(she_newlong++<she_long){    for(she_0=0;she_0<she_newlong;she_0++)    {    gotoxy(inflexion_x,--snake_y_ls);    printf("%c",tu_y);    }she_oldlong=she_long-she_newlong;if(she_oldlong>0){    for(she_0=0;she_0<she_oldlong;she_0++)    {    gotoxy(snake_x_ls++,inflexion_y);    printf("%c",tu_x);    }}    }if(she_newlong==she_long)    {    snake_y=inflexion_y-she_long;    she_newlong=0;    she_oldlong=0;    new_orientation=0;    orientation=orientation_up;    }    break;case orientation_down: /*left->down*/if(she_newlong++<she_long){    for(she_0=0;she_0<she_newlong;she_0++)    {    gotoxy(inflexion_x,++snake_y_ls);    printf("%c",tu_y);    }she_oldlong=she_long-she_newlong;if(she_oldlong>0){    for(she_0=0;she_0<she_oldlong;she_0++)    {    gotoxy(snake_x_ls++,inflexion_y);    printf("%c",tu_x);    }}    }if(she_newlong==she_long)    {    snake_y=inflexion_y+she_long;    she_newlong=0;    she_oldlong=0;    new_orientation=0;    orientation=orientation_down;    }    break;case old_orientation: /*old_orientation: left->*/    for (she_0=0;she_0<she_long;she_0++)    {gotoxy(snake_x_ls++,snake_y_ls);    printf("%c",tu_x);}}    break;case orientation_right:switch(new_orientation){    case orientation_up: /*right->up*/if(she_newlong++<she_long){    for(she_0=0;she_0<she_newlong;she_0++)    {    gotoxy(inflexion_x,--snake_y_ls);    printf("%c",tu_y);    }she_oldlong=she_long-she_newlong;if(she_oldlong>0){    for(she_0=0;she_0<she_oldlong;she_0++)    {    gotoxy(snake_x_ls--,inflexion_y);    printf("%c",tu_x);    }}    }if(she_newlong==she_long)    {    snake_y=inflexion_y-she_long;    she_newlong=0;    she_oldlong=0;    new_orientation=0;    orientation=orientation_up;    }    break;case orientation_down: /*right->down*/if(she_newlong++<she_long){    for(she_0=0;she_0<she_newlong;she_0++)    {    gotoxy(inflexion_x,++snake_y_ls);    printf("%c",tu_y);    }she_oldlong=she_long-she_newlong;if(she_oldlong>0){    for(she_0=0;she_0<she_oldlong;she_0++)    {    gotoxy(snake_x_ls--,inflexion_y);    printf("%c",tu_x);    }}    }if(she_newlong==she_long)    {    snake_y=inflexion_y+she_long;    she_newlong=0;    she_oldlong=0;    new_orientation=0;    orientation=orientation_down;    }    break;case old_orientation: /*old_orientation: right->*/    for (she_0=0;she_0<she_long;she_0++)    {gotoxy(snake_x_ls--,snake_y_ls);    printf("%c",tu_x);}}}sleep(1);system("cls");while (kbhit()){gotoxy(1,1);printf("Esc=menu");key_ls=getch();if (key_ls==upkey||key_ls==downkey||key_ls==leftkey||key_ls==rightkey){    inflexion_x=snake_x;    inflexion_y=snake_y;}if (new_orientation==0){switch (key_ls){    case upkey:    if (!(orientation==orientation_down||orientation==orientation_up))    {new_orientation=orientation_up;}    break;    case downkey:    if (!(orientation==orientation_up||orientation==orientation_down))    {new_orientation=orientation_down;}    break;    case leftkey:    if (!(orientation==orientation_right||orientation==orientation_left))    {new_orientation=orientation_left;}    break;    case rightkey:    if (!(orientation==orientation_left||orientation==orientation_right))    {new_orientation=orientation_right;}    break;}}switch(key_ls){    case esckey:    gotoxy(35,15);    printf("exit game (y or n)");    key_ls=getch();while (!(key_ls==esckey||key_ls=='n'||key_ls=='N'))    {while (key_ls=='y'||key_ls=='Y')    {exit();}    key_ls=getch();    }    break;    default : ;}}if (new_orientation==0){switch(orientation){case orientation_up:    snake_y--;    break;case orientation_down:    snake_y++;    break;case orientation_left:    snake_x--;    break;case orientation_right:    snake_x++;    break;}}if (snake_x<=10||snake_x>=70||snake_y<=1||snake_y>=25)    {gotoxy (35,13);    printf("game over");    getch();    exit();}}} 



[解决办法]
C/C++ code
#include <windows.h>#include <stdio.h>void ConPrint(char *CharBuffer, int len);void ConPrintAt(int x, int y, char *CharBuffer, int len);void gotoXY(int x, int y);void ClearConsole(void);void ClearConsoleToColors(int ForgC, int BackC);void SetColorAndBackground(int ForgC, int BackC);void SetColor(int ForgC);void HideTheCursor(void);void ShowTheCursor(void);int main(int argc, char* argv[]){   HideTheCursor();   ClearConsoleToColors(15, 1);   ClearConsole();   gotoXY(1, 1);   SetColor(14);   printf("This is a test...\n");   Sleep(5000);   ShowTheCursor();   SetColorAndBackground(15, 12);   ConPrint("This is also a test...\n", 23);   SetColorAndBackground(1, 7);   ConPrintAt(22, 15, "This is also a test...\n", 23);   gotoXY(0, 24);   SetColorAndBackground(7, 1);   return 0;}//This will clear the console while setting the forground and//background colors.void ClearConsoleToColors(int ForgC, int BackC){   WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);   //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   SetConsoleTextAttribute(hStdOut, wColor);   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);   }}//This will clear the console.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);   }}//This will set the position of the cursorvoid gotoXY(int x, int y){   //Initialize the coordinates   COORD coord = {x, y};   //Set the position   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);}//This will set the forground color for printing in a console window.void SetColor(int ForgC){   WORD wColor;   //We will need this handle to get the current background attribute   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);   CONSOLE_SCREEN_BUFFER_INFO csbi;   //We use csbi for the wAttributes word.   if(GetConsoleScreenBufferInfo(hStdOut, &csbi))   {      //Mask out all but the background attribute, and add in the forgournd color      wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);      SetConsoleTextAttribute(hStdOut, wColor);   }}//This will set the forground and background color for printing in a console window.void SetColorAndBackground(int ForgC, int BackC){   WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);}//Direct console outputvoid ConPrint(char *CharBuffer, int len){   DWORD count;   WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), CharBuffer, len, &count, NULL);}//Direct Console output at a particular coordinate.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);}//Hides the console cursorvoid HideTheCursor(){   CONSOLE_CURSOR_INFO cciCursor;   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);   if(GetConsoleCursorInfo(hStdOut, &cciCursor))   {      cciCursor.bVisible = FALSE;      SetConsoleCursorInfo(hStdOut, &cciCursor);   }}//Shows the console cursorvoid ShowTheCursor(){   CONSOLE_CURSOR_INFO cciCursor;   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);   if(GetConsoleCursorInfo(hStdOut, &cciCursor))   {      cciCursor.bVisible = TRUE;      SetConsoleCursorInfo(hStdOut, &cciCursor);   }} 


[解决办法]
计算机组成原理→DOS命令→汇编语言→C语言(不包括C++)、代码书写规范→数据结构、编译原理、操作系统→计算机网络、数据库原理、正则表达式→其它语言(包括C++)、架构……

C/C++ code
#include <stdio.h>#include <stdlib.h>#define   esckey 27#define    upkey 72#define  downkey 80#define  leftkey 75#define rightkey 77#define inceptline 10#define inceptrow   1#define graphicswidth 60#define graphicshigh  25#define linelimit (inceptline + graphicswidth)#define old_orientation       0#define     orientation_up    1#define     orientation_down  2#define     orientation_left  3#define     orientation_right 4#define she_long0 3#define wall_x '#'#define wall_y '#'#define   tu_x '#'#define   tu_y '#'#define   food '$'void main() {    int i;    int key_ls;    int she_0;    int she_long=she_long0;    int she_newlong=0;    int she_oldlong=0;    int new_orientation=old_orientation;    int orientation;    int inflexion_x;    int inflexion_y;    int snake_x;    int snake_y;    int snake_x_ls;    int snake_y_ls;    int food_randx;    int food_randy;    system("cls");    srand(time(NULL)+clock());    snake_x    =rand()%(graphicswidth-4-5)+inceptline+5;    snake_y    =rand()%(graphicshigh -4-5)           +5;    orientation=rand()%4+1;    food_randx =rand()%(graphicswidth-4-5)+inceptline+5;    food_randy =rand()%(graphicshigh -4-5)           +5;    while(1) {        gotoxy(food_randx,food_randy);        printf("%c",food);        gotoxy(inceptline,inceptrow);        for (i=inceptline;i<=linelimit;i++) {            printf("%c",wall_x);        }        gotoxy(inceptline,graphicshigh);        for (i=inceptline;i<=linelimit;i++) {            printf("%c",wall_x);        }        for (i=inceptrow+1;i<graphicshigh;i++) {            gotoxy(inceptline,i);            printf("%c",wall_y);        }        for (i=inceptrow+1;i<graphicshigh;i++) {            gotoxy(inceptline+graphicswidth,i);            printf("%c",wall_y);        }        while (            (snake_x==food_randx)          &&(snake_y==food_randy)          ||(                (snake_x_ls ==food_randx)              &&(inflexion_y==food_randy)              ||(                  (snake_y_ls ==food_randy)                &&(inflexion_x==food_randx)              )          )        ) {            she_long++;            food_randx=rand()%(graphicswidth-4-5)+inceptline+5;            food_randy=rand()%(graphicshigh -4-5)           +5;            gotoxy(food_randx,food_randy);            printf("%c",food);        }        snake_x_ls=snake_x;        snake_y_ls=snake_y;        if (new_orientation!=0) {            snake_x_ls=inflexion_x;            snake_y_ls=inflexion_y;        }        switch(orientation) {        case orientation_up:            switch(new_orientation) {            case orientation_left: /*up->left*/                if (she_newlong++<she_long-1) {                    for(she_0=0;she_0<she_newlong;she_0++) {                        gotoxy(--snake_x_ls,inflexion_y);                        printf("%c",tu_x);                    }                    she_oldlong=she_long-she_newlong;                    if (she_oldlong>0) {                        for(she_0=0;she_0<she_oldlong;she_0++) {                            gotoxy(inflexion_x,snake_y_ls++);                            printf("%c",tu_y);                        }                    }                }                if (she_newlong==she_long-1) {                    snake_x=inflexion_x-she_long;                    she_newlong=0;                    she_oldlong=0;                    new_orientation=0;                    orientation=orientation_left;                }            break;            case orientation_right: /*up->right*/                if (she_newlong++<she_long) {                    for(she_0=0;she_0<she_newlong;she_0++) {                        gotoxy(++snake_x_ls,inflexion_y);                        printf("%c",tu_x);                    }                    she_oldlong=she_long-she_newlong;                    if (she_oldlong>0) {                        for(she_0=0;she_0<she_oldlong;she_0++) {                            gotoxy(inflexion_x,snake_y_ls++);                            printf("%c",tu_y);                        }                    }                }                if (she_newlong==she_long) {                    snake_x=inflexion_x+she_long;                    she_newlong=0;                    she_oldlong=0;                    new_orientation=0;                    orientation=orientation_right;                }            break;            case old_orientation:                for (she_0=0;she_0<she_long;she_0++) {                    gotoxy(snake_x_ls,snake_y_ls++);                    printf("%c",tu_y);                }            break;            }        break;        case orientation_down:            switch(new_orientation) {            case orientation_left: /*down->left*/                if (she_newlong++<she_long) {                    for(she_0=0;she_0<she_newlong;she_0++) {                        gotoxy(--snake_x_ls,inflexion_y);                        printf("%c",tu_x);                    }                    she_oldlong=she_long-she_newlong;                    if (she_oldlong>0) {                        for(she_0=0;she_0<she_oldlong;she_0++) {                            gotoxy(inflexion_x,snake_y_ls--);                            printf("%c",tu_y);                        }                    }                }                if (she_newlong==she_long) {                    snake_x=inflexion_x-she_long;                    she_newlong=0;                    she_oldlong=0;                    new_orientation=0;                    orientation=orientation_left;                }            break;//未完待续 

读书人网 >C语言

热点推荐