#include <termios.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#define U 1
#define D 2
#define L 3 
#define R 4
struct termios tm_old;
typedef struct SNAKE{
    int x;
    int y;
    struct SNAKE *next;
 }snake;
snake *head,*food,*q;
int status;
int sleeptime;
int speed,score,add;
void Index();
void clear();
int Getch();
void Getch_2();
int kbhit(void);
void Initmap();
void Initmsgb();
void Initsnake();
void Createfood();
void Snakemove();
int biteself();
void Cantcrosswall();
void Startgame();
void Gamestate();
void Gameset();
void Exit();
void Pause();
void Pause_2();
void Endgame();
void Index(){
 clear();
    printf(" __________________________________________________________ \n");
    printf("|                                                          |\n");
    printf("|                                                          |\n");
    printf("|                                                          |\n");
    printf("|                                                          |\n");
    printf("|                        1. 开始游戏                       |\n");
    printf("|                                                          |\n");
    printf("|                                                          |\n");
    printf("|                                                          |\n");
    printf("|                        2. 游戏说明                       |\n");
    printf("|                                                          |\n");
    printf("|                                                          |\n");
    printf("|                                                          |\n");
    printf("|                        3. 游戏设置                       |\n");
    printf("|                                                          |\n");
    printf("|                                                          |\n");
    printf("|                                                          |\n");
    printf("|                        4. 退出游戏                       |\n");
    printf("|                                                          |\n");
    printf("|                                                          |\n");
    printf("|                                                          |\n");
    printf("|                                                          |\n");
    printf("|__________________________________________________________|\n");
}
void clear(){
    printf("\033[2J");
    printf("\033[0;0H");
    printf("\033[?25l"); 
}
int Getch()
{
    struct termios tm;
    tcgetattr(0,&tm_old);
    cfmakeraw(&tm);
    tcsetattr(0,0,&tm);
    int ch = getchar();
    tcsetattr(0,0,&tm_old);
    return ch;
}
void Getch_2(){
    int ch;
    ch=Getch();
   // printf("%d",ch);
    if(ch==104||ch==72){
        clear();
        Index();
    }
    else{
        printf("输入有误,请重新输入\n");
        Getch_2();
    }
}
int kbhit(void)
{
    struct termios oldt, newt;
    int ch;
    int oldf;
    tcgetattr(STDIN_FILENO, &oldt);
    newt = oldt;
    newt.c_lflag &= ~(ICANON | ECHO);
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);
    oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
    fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
    ch = getchar();
    tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    fcntl(STDIN_FILENO, F_SETFL, oldf);
 if(ch != EOF)
    {
            ungetc(ch, stdin);
            return 1;
    }
    return 0;
}
void Initmap(){
    int x,y;
    printf("\033[2J");
    printf("\033[1;1H");
    for(y=1;y<24;y ){
     printf("\033[%d;1H",y);
            printf("■");
            printf("\033[%d;61H",y);
            printf("■");
    }
    for(x=1;x<61;x=x 2){
        printf("\033[1;%dH",x);
        printf("■");
        printf("\033[24;%dH",x);
        printf("■");
    }
    printf(" ■");
}
void Initmsgb(){
 printf("\033[5;65H         ");
 printf("\033[5;65H当前速度:%d",speed);
        printf("\033[7;65H              ");
        printf("\033[7;65H当前得分:%d",score);
 printf("\033[17;62H【按H键返回主界面】");
 printf("\033[19;62H 【按P键暂停游戏】");
 printf("\033[21;64H");
}
void Initsnake(){
    int i;
    snake *tail;
    tail = (snake*) malloc(sizeof(snake));
    tail->x=17;
    tail->y=5;
    tail->next=NULL;
    for(i=1;i<4;i ){
        head =(snake*) malloc(sizeof(snake));
        head->next=tail;
        head->x=17 2*i;
        head->y=5;
        tail = head;
    }
    while(tail!=NULL){
    printf("\033[%d;%dH■",tail->y,tail->x);
    tail=tail->next;
    }
    status=R;
}
void Createfood(){
 snake *food_1;
 srand((unsigned)time(NULL));
 food_1=(snake*)malloc(sizeof(snake));
 food_1->x=rand()%28*2 3;
 food_1->y=rand()%21 2;
 q=head;
 while(q!=NULL){
 if(q->x==food_1->x&&q->y==food_1->y){
 free(food_1);
 food_1=NULL;
 Createfood();
 return;
 }
 q=q->next;
 }
 printf("\033[%d;%dH●",food_1->y,food_1->x);
 food=food_1;
}
void Snakemove(){
 snake *nexthead;
 nexthead = (snake*)malloc(sizeof(snake));
 if(status==U)
        {
                nexthead->x=head->x;
                nexthead->y=head->y-1;
 if(nexthead->x==food->x&&nexthead->y==food->y){
 nexthead->next=head;
 head = nexthead;
 q=head;
 while(q!=NULL){
 printf("\033[%d;%dH■",q->y,q->x);
 q=q->next;
 }
 score = score add;
 Createfood(); 
 }
 else{
                 nexthead->next=head;
                 head=nexthead;
                 q=head;
                 while(q->next->next!=NULL){
                         printf("\033[%d;%dH■",q->y, q->x);
                         q = q->next;
                 }
                 printf("\033[%d;%dH ",q->next->y,q->next->x);
                 free(q->next);
                 q->next = NULL;
 }
        }
        if(status==D)
        {
                nexthead->x=head->x;
                nexthead->y=head->y 1;
 if(nexthead->x==food->x&&nexthead->y==food->y){
                        nexthead->next=head;
                        head = nexthead;
                        q=head;
                        while(q!=NULL){
                                printf("\033[%d;%dH■",q->y,q->x);
                                q=q->next;
                        }
                        score = score add;
                        Createfood();
                }
                else{
                nexthead->next=head;
                 head=nexthead;
                 q=head;
                 while(q->next->next!=NULL){
                         printf("\033[%d;%dH■",q->y, q->x);
                         q = q->next;
                 }
                 printf("\033[%d;%dH ",q->next->y,q->next->x);
                 free(q->next);
                 q->next = NULL;
 }
        }
        if(status==L)
        {
                nexthead->x=head->x-2;
                nexthead->y=head->y;
 if(nexthead->x==food->x&&nexthead->y==food->y){
 nexthead->next=head;
 head = nexthead;
 q=head;
 while(q!=NULL){
 printf("\033[%d;%dH■",q->y,q->x);
 q=q->next;
 }
 score = score add;
 Createfood(); 
 }
 else{
                 nexthead->next=head;
                 head=nexthead;
                 q=head;
                 while(q->next->next!=NULL){
                         printf("\033[%d;%dH■",q->y, q->x);
                         q = q->next;
                 }
                 printf("\033[%d;%dH ",q->next->y,q->next->x);
                 free(q->next);
                 q->next = NULL;
 }
        }
 if(status==R)
 {
 nexthead->x=head->x 2;
 nexthead->y=head->y;
 if(nexthead->x==food->x&&nexthead->y==food->y){
 nexthead->next=head;
 head = nexthead;
 q=head;
 while(q!=NULL){
 printf("\033[%d;%dH■",q->y,q->x);
 q=q->next;
 }
 score = score add;
 Createfood(); 
 }
 else{
                 nexthead->next=head;
                 head=nexthead;
                 q=head;
                 while(q->next->next!=NULL){
                         printf("\033[%d;%dH■",q->y, q->x);
                         q = q->next;
                 }
                 printf("\033[%d;%dH ",q->next->y,q->next->x);
                 free(q->next);
                 q->next = NULL;
 }
 }
}
int biteself(){
 snake *self;
 self=head->next;
 while(self!=NULL){
 if(self->x==head->x&&self->y==head->y)
 return 1;
 self=self->next;
 }
 return 0;
}
void Cantcrosswall(){
 if(head->x<3||head->x>59||head->y<2||head->y>22)
 Endgame();
}
void Startgame(){
 sleeptime=240000;
 speed=1;
 score=0;
 add=1;
     clear();
     Initmap();
 Initmsgb();
    Initsnake();
 Createfood();
 while(1){
 Cantcrosswall();
 if(kbhit()){
 int ch;
 ch = Getch();
 if((ch==119||ch==87)&&status!=D)
 status=U;
 else if((ch==115||ch==83)&&status!=U)
 status=D;
 else if((ch==97||ch==65)&&status!=R)
 status=L;
 else if((ch==100||ch==68)&&status!=L)
 status=R;
 else if(ch==61){
 sleeptime=sleeptime-50000;
 add ;
 speed ;
 }
 else if(ch==45){
 sleeptime=sleeptime 50000;
 add--;
 speed--;
 }
 else if(ch==104||ch==72){
 clear();
 Index();
 Pause_2();
 return;
 }
 else if(ch==112||ch==80){
 Pause();
 }
 if(biteself()==1){
 Endgame();
 }
 }
 Snakemove();
 Initmsgb();
 usleep(sleeptime);
 }
}        

 
  
					
				
评论