Linux 环境下C语言编译实现贪吃蛇游戏(二)

2014-11-24 10:53:34 ? 作者: ? 浏览: 1
ead->next->cx+dir.cx, head->next->cy+dir.cy);
//蛇吃了一个“食物”
if(head->next->cx==food.cx && head->next->cy==food.cy)
{
lenChange
= true;
length
++;
//恭喜你,通关了
if(length >= 50)
{
over(
3);
return;
}
//重新设置食物的位置
food.cx = rand() % COLS;
food.cy
= rand() % (LINES-2) + 2;
}
if(!lenChange)
{
move(tail
->back->cy, tail->back->cx);
printw(
" ");
deleteNode();
}
move(head
->next->cy, head->next->cx);
printw(
"*");
}

void show()
{
signal(SIGALRM, show);
//设置中断信号
showInformation();
showSnake();
refresh();
//刷新真实屏幕
}

void getOrder()
{
//建立一个死循环,来读取来自键盘的命令
while(1)
{
ch
= getch();
if(KEY_LEFT == ch)
{
dir.cx
= -1;
dir.cy
= 0;
}
else if(KEY_UP == ch)
{
dir.cx
= 0;
dir.cy
= -1;
}
else if(KEY_RIGHT == ch)
{
dir.cx
= 1;
dir.cy
= 0;
}
else if(KEY_DOWN == ch)
{
dir.cx
= 0;
dir.cy
= 1;
}
setTicker(
20);
}
}

void over(int i)
{
//显示结束原因
move(0, 0);
int j;
for(j=0;j<COLS;j++)
addstr(
" ");
move(
0, 2);
if(1 == i)
addstr(
"Crash the wall. Game over");
else if(2 == i)
addstr(
"Crash itself. Game over");
else if(3 == i)
addstr(
"Mission Complete");
setTicker(
0); //关闭计时器
deleteLink(); //释放链表的空间
}

//创建一个双向链表
void creatLink()
{
node
*temp = (node *)malloc( sizeof(node) );
head
= (node *)malloc( sizeof(node) );
tail
= (node *)malloc( sizeof(node) );
temp
->cx = 5;
temp
->cy = 10;
head
->back = tail->next = NULL;
head
->next = temp;
temp
->next = tail;
tail
->back = temp;
temp
->back = head;
}

//在链表的头部(非头结点)插入一个结点
void insertNode(int x, int y)
{
node
*temp = (node *)malloc( sizeof(node) );
temp
->cx = x;
temp
->cy = y;
temp
->next = head->next;
head
->next = temp;
temp
->back = head;
temp
->next->back = temp;
}

//删除链表的(非尾结点的)最后一个结点
void deleteNode()
{
node
*temp = tail->back;
node
*bTemp = temp->back;
bTemp
->next = tail;
tail
->back = bTemp;
temp
->next = temp->back = NULL;
free(temp);
temp
= NULL;
}

//删除整个链表
void deleteLink()
{
while(head->next != tail)
deleteNode();
head
->next = tail->back = NULL;
free(head);
free(tail);
}


-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: