欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > c语言第一个小游戏:贪吃蛇小游戏08(贪吃蛇完结)

c语言第一个小游戏:贪吃蛇小游戏08(贪吃蛇完结)

2025/5/15 14:46:39 来源:https://blog.csdn.net/dhdhshshw2/article/details/147934383  浏览:    关键词:c语言第一个小游戏:贪吃蛇小游戏08(贪吃蛇完结)

贪吃蛇撞墙和想不开咬死自己

#include <curses.h>

#include <stdlib.h>

struct snake{

        int hang;

        int lie;

        struct snake *next;

};

struct snake food;

struct snake *head;

struct snake *tail;

int key;

int dir;

#define UP     1

#define DOWN  -1

#define LEFT   2

#define RIGHT -2

void initNcurse()

{

        initscr();

        keypad(stdscr,1);

        noecho();

}

int  hasSnakeNode(int i,int j)

{

        struct snake *p;

        p = head;

        while(p != NULL){

                if(p->hang==i && p->lie==j){

                        return 1;

                }

                p=p->next;

        }

        return 0;

}

int  hasSnakeFood(int i,int j)

{

          if(food.hang==i && food.lie==j){

                 return 1;

           }

          return 0;

}

void gamepic()

{

        int hang;

        int lie;

        move(0,0);

        for(hang=0;hang<20;hang++){

                if(hang == 0){

                        for(lie=0;lie<20;lie++){

                                printw("--");

                        }

                 printw("\n");

                }

                if(hang>=0 && hang<=19){

                        for(lie=0;lie<=20;lie++){

                                 if(lie==0||lie==20){

                                         printw("|");

                                 }else if(hasSnakeNode(hang,lie)){

                                        printw("[]");

                                 }else if(hasSnakeFood(hang,lie)){

                                        printw("##");

                                 }

                                 else{

                                         printw("  ");

                                 }

                        }

                        printw("\n");

                }

                if(hang == 19){

                        for(lie=0;lie<20;lie++){

                                 printw("--");

                        }

                        printw("\n");

                }

          }

          printw("by shijintao\n");

          printw("key =%d\n",key);

   }

void addNode()

{

        struct snake *new;

        new =(struct snake *)malloc(sizeof(struct snake));

        new->next=NULL;

                switch(dir){

                case UP:

                        new->hang=tail->hang-1;

                        new->lie=tail->lie;

                        tail->next=new;

                        tail = new;

                        break;

                case DOWN:

                        new->hang=tail->hang+1;

                        new->lie=tail->lie;

                        tail->next=new;

                        tail = new;

                        break;

                case LEFT:

                        new->lie=tail->lie-1;

                        new->hang=tail->hang;

                        tail->next=new;

                        tail = new;

                        break;

                case RIGHT:

                        new->lie=tail->lie+1;

                        new->hang=tail->hang;

                        tail->next=new;

                        tail = new;

                        break;

                 }

}

void initFood()

{

      int x=rand()%20;

      int y=rand()%20;

      food.hang=x;

      food.lie=y;

}

void  initSnake()

{

        struct snake *p;

        while(head != NULL){

             p=head;

             head=head->next;

             free(p);

        }

        initFood();

        head = (struct snake *)malloc(sizeof(struct snake));

        dir = RIGHT;

        head->hang=2;

        head->lie=2;

        head->next=NULL;

        tail = head;

        addNode();

        addNode();

}

void deleteNode()

{

        struct snake *p;

        p = head;

        head = head->next;

        free(p);

}

int ifsnakeDie()

{

        struct snake *p; //为了遍历链表,看有没有咬自己

        p=head;

        if(tail->hang<0||tail->hang==20||tail->lie==20||tail->lie==0){

                return 1;

        }

        while(p->next!=NULL){

//尾节点和其中一个节点 的行列都相同时就是发生自己咬自己死的情况,也就是每次移动一下每个坐标都会进来判断,链表也都会遍历一遍,如果尾节点的坐标与节点坐标相同 ,那么就是死!!

                if(p->hang == tail->hang && p->lie == tail->lie){

                        return 1;

                }

                p=p->next;

        }

}

void moveSnake()

{

        addNode();

        if(hasSnakeFood(tail->hang,tail->lie)){

                initFood();

        }else{

                deleteNode();

        }

        if(ifsnakeDie()){

                initSnake();

        }

}

void turn(int direction)

{

        if(abs(dir) != abs(direction)){

                dir = direction;

        }

}

void *  changeDir()

{

        while(1){

                key =getch();

                switch(key){

                        case KEY_DOWN:

                                turn(DOWN);

                                break;

                        case KEY_UP:

                                turn(UP);

                                break;

                        case KEY_RIGHT:

                                turn(RIGHT);

                                break;

                        case KEY_LEFT:

                                turn(LEFT);

                                break;

                }

        }

}

void * gamerefresh()

{

        while(1){

                moveSnake();

                gamepic();

                refresh();

                usleep(100000);

        }

}

int main()

{

                pthread_t th1;

                pthread_t th2;

                initNcurse();

                initSnake();

                gamepic();

                pthread_create(&th2,NULL,gamerefresh,NULL);

                pthread_create(&th1,NULL,changeDir,NULL);

                while(1);

                getch();

                endwin();

                return 0;

}

终于完结了噶,贪吃蛇笔记分享就到这里了。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词