#include #include #include #include #include
#define up 'w' #define down 's' #define left 'a' #define right 'd' #define space 'q' #define slow 'e'
void gotoxy(int x, int y); int ClickControl(); void moveobject(); void food(); int color(int c); void border(); void wall(); //srand((unsigned)time(0));
int j,i,k,click,length=5; int _time=100000000; typedef struct Snake { int x; int y; struct Snake *next; }snake; snake s={15,15}; snake *head; snake ss[100];
main() {
int c; ss[0]=s; snake temp[2]; for(i=1;i { ss[i].x=ss[0].x-2*i; ss[i].y=ss[0].y; } head=ss; while(1) { wall(); food(); temp[0]=ss[0]; ClickControl(); moveobject(); border(); for(i=1;i { //交换temp[0]和ss[i]的值 temp[1]=ss[i]; ss[i]=temp[0]; temp[0]=temp[1]; } srand((unsigned)time(0)); color(2); for(i=0;i { if(i==0) { gotoxy(ss[i].x,ss[i].y); printf("¤");
// printf(" 0"); }
else{ gotoxy(ss[i].x,ss[i].y); c=rand()%13+1;
// printf(""); printf("⊙"); } } gotoxy(68,3); printf("你的得分是:%d",length*100-500); // system("cls"); for(i=0;i<_time;i++); for(i=0;i
void gotoxy(int x, int y) { COORD pos; HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE); pos.X = x; pos.Y = y; SetConsoleCursorPosition(hOutput, pos);
CONSOLE_CURSOR_INFO cursor; cursor.bVisible = FALSE; cursor.dwSize = sizeof(cursor); SetConsoleCursorInfo(hOutput, &cursor);
}
void moveobject() { int x,y; x=ss[0].x; y=ss[0].y;
switch (click) { case up: y -= 1; break; case down: y += 1; break; case left: x -= 2; break; case right: x += 2; break; case space: _time=4000000;break; case slow: _time=100000000;break; default: break; } ss[0].x=x; ss[0].y=y;
}
int ClickControl() { char c; while (1) { if (_kbhit() == 0) return 0; if (_kbhit()) { click = _getch(); } moveobject(); } return 1; }
void food() { int static foodx,foody,h_food=0; srand((unsigned)time(0)); if(!h_food) { foodx=rand()%29*2+3; foody=rand()%25+1; gotoxy(foodx,foody); // printf("■"); h_food=1; } gotoxy(foodx,foody); color(4); printf("■"); for(i=0;i
int color(int c) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); //更改文字颜色 return 0; }
void border() { if(head->x<=1) head->x=61; else if(head->x>=61) head->x=1; if(head->y<=0) head->y=28; else if(head->y>=28) head->y=0; }
void wall() { color(5); gotoxy(0,0); for(i=0;i<=61;i+=2) { printf("■"); } gotoxy(0,28); for(i=0;i<=61;i+=2) { printf("■"); } for(i=0;i<=28;i++) { gotoxy(0,i); printf("■"); } for(i=0;i<=28;i++) { gotoxy(62,i); printf("■"); } }