www.pudn.com > snake.zip > snake.c
#include#include #include #include"utilwin32.cpp" #define Up 0x48 #define Down 0x50 #define Left 0x4b //オ #define Right 0x4d // struct node{ int x; int y; struct node *next; }; typedef struct node NODE; NODE *first,*current,*previous,*newnode; int food_x=0,food_y=0; int snake_x,snake_y; int score=0; int snake; int speed=150; int add_speed_1=0,add_speed_2=0,add_speed_3=0,add_speed_4=0; char w2=Right; char temp_w2; char w; void freelist(NODE *); void newgame(); void playgame(); void randfood(); int check_key_down(); void check_eat_food(); void move_snake(); void print_snake(); void checkspeed(); void printbackground(); void clear_snake(); int check_hit_wall(); int check_hit_snake(); void init(); int main(){ playgame(); //freelist(first); } void newgame(){ //﹍て僣3snake int i; for(i=0;i<=2;i++){ current=(NODE *)malloc(sizeof(NODE)); //穝糤儶呺娀 current->x=20-i; //畒囜 current->y=10; if(i==0) first=current; else previous->next=current; current->next=NULL; previous=current; } current=first; } void playgame(){ //璶祘Α伴┮ init(); //﹍て办岪囒,﹍て僣3劖矰 clrscr(); char quit; randfood(1); //叝囒 printbackground(); //璉春 for(;;){ checkspeed(); //変诡硉 10,20,25,30常呇硉 print_snake(); //劖矰 check_eat_food(); //変琩琌 check_key_down(); //変琩妦よ move_snake(); //簿凁劖矰 if(check_hit_wall()){ //変琩琌疾夣 break; } if(check_hit_snake()){ //変琩琌疾劖矰セō 1痷 0安 break; } } clrscr(); gotoxy(10,10); printf("村壼匔!!!,眤だ囒琌%dだ,q置厈祘Α,r穝厈﹍\n",score); quit=getch(); while(1){ if(quit==0x71 || quit==0x51){ //q break; }else if(quit==0x72 || quit==0x52){ //r quit=NULL; playgame(); }else{ quit=getch(); } } system("exit"); } void init(){ food_x=0; food_y=0; snake_x=0; snake_y=0; score=0; snake=0; speed=150; add_speed_1=0; add_speed_2=0; add_speed_3=0; add_speed_4=0; w2=Right; w=NULL; newgame(); srand(time(NULL)); //﹍て叝囒销 } void randfood(int status){ //叝囒 if(status==1){ gotoxy(food_x,food_y); printf(" "); food_x=rand() % 55 + 12; //12~66 food_y=rand() % 17 + 4; //4~20 } gotoxy(food_x,food_y); printf("@"); } void check_eat_food(){ //娛琌 if(snake_x==food_x && snake_y==food_y){ newnode=(NODE *)malloc(sizeof(NODE)); newnode->x=food_x; newnode->y=food_y; newnode->next=first; first=newnode; snake_x=food_x; snake_y=food_y; score+=10; randfood(1); } randfood(2); gotoxy(1,1); printf(" "); gotoxy(1,1); printf(" だ囒:%d だ,僣:%d,硉:%d",score,snake,speed); } int check_key_down(){//変琩妦よ temp_w2=w2; //char w; if(kbhit()){ w=getch(); } if(w==0xffffffe0){ //E0 w=NULL; w2=getch(); if(w2==Right && temp_w2==Left){ //娛劖矰┕オ兒,ぃ妦 w2=temp_w2; }else if(w2==Left && temp_w2==Right){ w2=temp_w2; }else if(w2==Up && temp_w2==Down){ w2=temp_w2; }else if(w2==Down && temp_w2==Up){ w2=temp_w2; } } switch(w2){ case Up: snake_y--; break; case Down: snake_y++; break; case Left: snake_x--; break; case Right: snake_x++; } } void print_snake(){ //娮對呺娀,劖矰X,Y current=first; snake=0; while(current !=NULL){ gotoxy(current->x,current->y); printf("#"); if(current==first){ snake_x=current->x; snake_y=current->y; } current=current->next; snake++; } } void move_snake(){ delay(speed); clear_snake();//睲埃劖矰匄 int temp_x,temp_y,temp_n_x,temp_n_y; current=first; while(current != NULL){ if(current==first){ //–儶呺娀X,Y簿儶呺娀,┮呇┕玡簿凁 temp_x=current->x; temp_y=current->y; current->x=snake_x; current->y=snake_y; }else{ temp_n_x=current->x; temp_n_y=current->y; current->x=temp_x; current->y=temp_y; temp_x=temp_n_x; temp_y=temp_n_y; } current=current->next; } } void checkspeed(){ //変琩硉10,20,25,30呇硉 if(snake==10){ if(add_speed_1==0) speed=120; add_speed_1=1; }else if(snake==20){ if(add_speed_2==0) speed=90; add_speed_2=1; }else if(snake==25){ if(add_speed_3==0) speed=60; add_speed_3=1; }else if(snake==30){ if(add_speed_4==0) speed=30; add_speed_4=1; } } void printbackground(){ //璉春 int i; for(i=1;i<=60;i++){ gotoxy(9+i,2); printf("="); gotoxy(9+i,23); printf("="); } for(i=1;i<=22;i++){ gotoxy(10,1+i); printf("|"); gotoxy(70,1+i); printf("|"); } } void clear_snake(){ //睲埃匄劖矰 current=first; while(current !=NULL){ gotoxy(current->x,current->y); printf(" "); current=current->next; } } int check_hit_wall(){ //贝娛琌疾夣 int i; int wall_x[2]={10,70}; //オ int wall_y[2]={2,23}; // // for(i=0;i<2;i++){ if(snake_x==wall_x[i] || snake_y==wall_y[i]) return 1; } return 0; } int check_hit_snake(){ //娛材儶呺娀X,Y琌僶劖矰セō current=first; current=current->next; while(current !=NULL){ if(snake_x==current->x && snake_y==current->y){ return 1; } current=current->next; } return 0; } //void freelist(NODE *first){ //NODE *current,*temp; // current=first; // while(current !=NULL){ // temp=current; // current=current->next; // free(temp); //} //}