c链表基于Ncurses设计贪吃蛇项目

项目介绍

1.地图:20x20的正方形区域

2.蛇身:【】【】【】【】

3.食物:##

4.游戏规则:按下↑↓←→来操控蛇的方向,蛇尾撞到边界重新开始,吃到食物蛇身边长

Ncurses:

选择Ncurses的原因是:printw()可以快速响应不需要像printf一样按下回车才开始运行

贪吃蛇游戏需要调用的库

1.#include

2.initscr():设定并初始化ncurses要用到的一些数据的值

3.endwin():恢复终端机到使用前的状态

4.getch():等待获取用户输入

6.printw():打印,与printf()作用相似

7.keypad(stdscr,1):功能键设定

KEY_DOWN 0402
KEY_UP 0403
KEY_LEFT 0404
KEY_RIGHT 0405

8.refresh():刷新界面

9.move(0.0):每次刷新界面不移动地图

10.noecho():防止产生乱码现象

需要用到的头文件

#include
#include
#include
#include
#include
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2

完整代码如下

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

struct snake food;

struct snake *head=NULL;
struct snake *tail=NULL;
int key;
int dir;

void initncurse()
{
  initscr();
  keypad(stdscr,1);
  noecho();
}

void initfood()
{
   int x=rand()%20;
   int y=rand()%20;
  food.hang=x;
  food.lie=y;
 
}

int  printfood(int hang , int lie)
{
  if(food.hang == hang && food.lie == lie){
        return 1;
    }
        return 0;
}


int snaker(int hang,int lie)
{
 
  struct snake* p=NULL;
  p=head;
  while(p != NULL){
  if(p->hang == hang && p->lie == lie){
     return 1;
        }p=p->next;
    } 
  return 0;
}

void  addnode()
{
 struct snake *new=NULL;
 new=(struct snake *)malloc(sizeof(struct snake));
 switch(dir){
    case UP:
     new->hang=tail->hang-1;
     new->lie=tail->lie;
         break;     
    case DOWN:
     new->hang=tail->hang+1;
     new->lie=tail->lie;
         break;    
    case LEFT:
     new->hang=tail->hang;
     new->lie=tail->lie-1;
         break;    
    case RIGHT:
     new->hang=tail->hang;
     new->lie=tail->lie+1;
         break;    
  } 
 tail->next=new;
 tail=new;
}

void  initsnake()
{
 struct snake *p;
  dir=RIGHT;
  while(head != NULL){
     p=head;
     head=head->next;
     free(p);
   }
 initfood();
head=(struct snake *)malloc(sizeof(struct snake));
head->hang=2;
head->lie=1;
head->next=NULL;
tail=head;
addnode();
addnode();
addnode();
}

void gameboard()
{
 int hang,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(snaker(hang,lie)){
           printw("[]");
            }else if(printfood(hang,lie)) {
            printw("##");
            }else{
          printw("  ");
         }
      }
    }if(hang == 19){
      for(lie=0;lie<20;lie++){
       printw("--");
       }
         printw("\n");
         printw("%d",key);
     }
  }
}

void deletenode()
{

    head=head->next;

}

void movesnake()
{
  addnode();
 if(printfood(tail->hang,tail->lie)){
     initfood();}
else{
    deletenode(); 
 }  
}

int  snakedie()
{
         struct snake *p;
         p=head;
         if(tail->hang == 0 || tail->lie == 0 || tail->hang == 20|| tail->lie == 20){
           return 1;
		  }  
		 /* while(p->next != NULL){
		      if(p->hang == tail->hang && p->lie == tail->lie);
			{return 1;
			   }
			 p=p->next; 
		   } */
		       return 0;
	}

void  fresh()
	{
	  while(1){
		 movesnake();
		 gameboard();       
		 if(snakedie()){
           initsnake();
          }  
          refresh();
         usleep(100000);
           }
}

void turn(int direction)
{
  if(abs(dir) != abs(direction))
   {
       dir=direction;
   }
}


void  anjian()
{
    while(1)
     {
     key= getch();
      switch(key)
          { 
            case KEY_DOWN:
              turn(DOWN);
              break;
             case KEY_UP:
              turn(UP);
               break;
             case KEY_LEFT:
              turn(LEFT);
                break;
              case KEY_RIGHT:
              turn(RIGHT);
               break;

            }

      }
}

int main()
{ 
  pthread_t th1;  
  pthread_t th2;
  initncurse();
  initsnake();
  gameboard(); 
  pthread_create(&th1,NULL,fresh,NULL);
  pthread_create(&th2,NULL,anjian,NULL);
  while(1); 
  getch();
  endwin();
  return 0;
} 


 

你可能感兴趣的:(c语言,开发语言)