当前位置: 首页 > backend >正文

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;

}

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

http://www.xdnf.cn/news/5815.html

相关文章:

  • 本地的ip实现https访问-OpenSSL安装+ssl正式的生成(Windows 系统)
  • 职坐标AIoT开发技能精讲培训
  • Tomcat的调优
  • 【用「概率思维」重新理解生活】
  • RabbitMQ 核心概念与消息模型深度解析(二)
  • 开源模型应用落地-qwen模型小试-Qwen3-8B-融合VLLM、MCP与Agent(七)
  • 六、Hive 分桶
  • OpenHarmony平台驱动开发(十五),SDIO
  • tomcat与nginx之间实现多级代理
  • DeepSeek、B(不是百度)AT、科大讯飞靠什么坐上中国Ai牌桌?
  • css iconfont图标样式修改,js 点击后更改样式
  • 哈希表:数据世界的超级索引
  • 基于深度学习的工业OCR数字识别系统架构解析
  • 机器学习 --- 特征工程(一)
  • Spring Boot 使用 OSHI 实现系统运行状态监控接口
  • Conda在powershell终端中无法使用conda activate命令
  • docker及docker-compose安装及使用
  • mac 10.15.7 svn安装
  • 设计模式系列(02):设计原则(一):SRP、OCP、LSP
  • Visual Studio 2022 跨网络远程调试
  • 多线程(二)
  • 【2025年前端高频场景题系列】使用同一个链接,如何实现PC打开是web应用、手机打是-个H5 应用?
  • 免费Office图片音频高效提取利器
  • ik 分词器 设置自定义词典
  • @Component 注解:Spring 组件扫描与管理的基石
  • 如何使用 WebBrowserPassView 查看所有浏览器密码?
  • 【WordPress博客AI内容辅助生成/优化工具箱插件下载无标题】
  • 语义分割模型部署到嵌入式终端的通用操作流程
  • journalctl 日志查看工具介绍
  • istringstream的简化源码详解