贪吃蛇项目(让贪吃蛇向右移动)

1.蛇身如何向右移动
在这里插入图片描述
2.代码实现

#include  <curses.h>
#include  <stdlib.h>

struct   snake
{
      int  hang;
      int  lie;
      struct  snake *next;
};

struct  snake  *head;//蛇头
struct  snake  *tail;//蛇尾

void  initNcurse()
{

            initscr();
            keypad(stdscr,1);
}

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;
}

void  gamePic()
{
      int  hang;
      int  lie;


      move(0,0);//移动节点的原始位置设置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
                    {
                          printw("  ");
                    }
              }
                  
             printw("\n");
        }
             
        
                if(hang==19)
        {
                 
                 for(lie=0;lie<20;lie++)
               {
    
                   printw("--");
    
               }
                
                 printw("\n");
          
                 printw("by shuai ge");
        }
}  

}



void    addNode()
{

        struct  snake *new=(struct  snake *)malloc(sizeof(struct snake));
        
        new->hang=tail->hang;/*当在尾巴中增加一个节点的时候,列值加1*/
        new->lie=tail->lie+1;
        new->next=NULL;

        tail->next=new;//尾巴的下一个节点指向新节点
        tail=new;//让新节点变成尾巴
}

void   initsnake()
{
        head=(struct  snake *)malloc(sizeof(struct snake));
        head->hang=2;
        head->lie=2;
        head->next=NULL;

        tail=head;
            
        addNode();
}

void  deleteNode()//删除节点
{
        
          struct  snake  *p;
          p=head;//定义p为链表头
 
          head=head->next;//链表向后移动

          free(p);//链表向后移动,把p释放掉
}

void  movesnake()
{
         
          
          addNode();
          deleteNode();
          
}

int   main()
{
            int  con;
           
            initNcurse();

            initsnake();
           
            gamePic();

            while(1)
            {
                   con=getch();
                   if(con==KEY_RIGHT)//向右移动
                   {
                          movesnake();
                          gamePic();
                   }
            }
           
            getch();
            endwin();
            return  0; 

}

运行效果
起始位置:
在这里插入图片描述
键盘按向右键
在这里插入图片描述

——@上官可编程

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值