#include<curses.h>
#include<stdlib.h>
#include<stdio.h>
void initNcurse()
{
initscr();
keypad(stdscr,1);
}
struct snake
{
int hang;
int lie;
struct snake *next;
};
struct snake *head=NULL;
struct snake *tail=NULL;//定义两个全局变量
int hasNode(int i,int j)//扫描位置
{
struct snake* p;
p=head;
while(p!=NULL)
{
if(p->hang==i && p->lie==j)//若找到,则返回1
{
return 1;
}
p=p->next;
}
return 0;
}
void gamePic()//打印游戏的轮廓
{
int hang;
int lie;
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(hasNode(hang,lie)==1)
{
printw("[]");
}
else
{
printw(" ");
}
}
printw("\n");
}
if(hang==19)
{
for(lie=0;lie<20;lie++)
{
printw("--");
}
printw("\n");
printw("Designed by QYY\n");
}
}
}
void addn()
{
struct snake *new=(struct snake *)malloc(sizeof(struct snake));
new->hang=tail->hang;
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;
addn();
addn();
addn();
}
int main()
{
initNcurse();
initsnake();//制造一个链表,包含位置的数据
gamePic();//打印链表中的位置
getch();
endwin();
return 0;
}
运用函数进行蛇长度变化,更高大上