/*
* 绘制蛇身
*/
void initsnake()
{
snake *tail;//该指针表示蛇尾
int i;
tail = (snake*)malloc(sizeof(snake));
tail->x = 24;//蛇尾坐标:(24,5)
tail->y = 5;
for(i = 1;i<=4;i++)
{
head = (snake*)malloc(sizeof(snake));
head->next = tail;//蛇头坐标:(24+2*i,5) i>1&&i<4
head->x = 24+2*i;
head->y = 5;
tail = head;
}
while(tail!=NULL)
{
gotoxy(tail->x,tail->y);
color(14);//14黄色
printf("★");
tail = tail->next;
}
}
/*
* 绘制食物并随机显示
*/
void createfood()
{
snake *food_1;
food_1 = (snake*)malloc(sizeof(snake));
srand((unsigned)time(NULL));
while((food_1->x%2)!=0)//食物的x轴坐标
{
food_1->x = rand()%52+2;//52为整个游戏界面的宽
}
food_1->y = rand()%24+1;//y值坐标在1-24之间
q = head;//蛇头坐标
while(q->next == NULL)
{
if(q->x == food_1->x&&q->y == food_1->y)
{
free(food_1);
createfood();
}
q=q->next;//查找蛇身上的所有坐标,所有坐标都不能跟蛇身重叠
}
gotoxy(food_1->x,food_1->y);
food = food_1;
color(12);//red color
printf("●");
}
/*
* 判断是否咬到自己
*/
int biteself()
{
snake *self;
self = head->next;
while(self!=NULL)
{
if(self->x == head->x&&self->y == head->y)
{
return 1;//1表示蛇咬到了自己
}
self = self->next;
}
return 0;
}
/*
* 判断是否撞墙
*/
void cantcrosswall()
{
if(head->x==0||head->x==56||head->y==0||head->y==26)
{
endgamestatus = 1;
endgame();
}
}
/*
* 蛇变速前进(吃到食物加速和按F1加速)
*/
void speedup()
{
if(sleeptime>=50)
{
sleeptime = sleeptime-10;
add = add+2;
}
}