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

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;

}

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

相关文章:

  • ros2-node
  • Win11 + Visual Studio 2022 + FLTK 1.4.3 + Gmsh 4.13.1 源码编译指南
  • Ubuntu 18.04.6下OpenSSL与OpenSSH版本升级
  • 2025-05-13 Unity 网络基础12——大小端模式
  • 项目中遇到的问题
  • WSL 安装 Debian 12 后,Linux 如何安装 vim ?
  • AI时代,如何实现人机共舞?
  • 计算机网络防火墙技术介绍:包过滤、应用代理网关、状态监测
  • c# 数据结构 树篇 入门树与二叉树的一切
  • 最大子段和(递推)
  • 物理:人的记忆是由基本粒子构成的吗?
  • 【AI News | 20250513】每日AI进展
  • Ubuntu 22.04搭建OpenStreeMap地址解析服务(保姆级教程)
  • 跨境电商定价革命:亚马逊“逆向提价“策略背后的价值重构逻辑
  • Readiris PDF:高效文档管理与OCR识别工具
  • CMake基础
  • Linux上的rm和srm 命令
  • Springboot实现重试机制
  • ebook2audiobook开源程序使用动态 AI 模型和语音克隆将电子书转换为带有章节和元数据的有声读物。支持 1,107+ 种语言
  • 从新手到高手:全面解析 AI 时代的「魔法咒语」——Prompt
  • 人民日报仲音:大力纠治违规吃喝顽瘴痼疾
  • 中国-拉共体成员国重点领域合作共同行动计划(2025-2027)
  • 安徽省委副秘书长、省委政研室主任余三元调任省社科院院长
  • 日本广岛大学一处拆迁工地发现疑似未爆弹
  • 英媒:英国首相住所起火,目前无人伤亡
  • 美元指数上涨超1%重返101上方,创1个月新高