TC学习记录之-贪吃蛇,暂时每次只有一个拐点,该如何解决
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();} } }