循环遍历数组
{
if(a[i][j]==0) //为0输出空格
printf(" ");
else //为1输出#
printf("#");
}
printf("\n"); //别忘了换行
}
}
void move()
{
int i;
gotoxy(s[sLength-1][0],s[sLength-1][1]);
printf(" "); //在尾巴上面画空格以擦除尾巴
if(eated) //如果吃到了食物
{
sLength++;
eated=false; //设置为false,不然无限变长
}
for(i=sLength-1; i>0; i--) //从尾巴开始,每一个点的位置等于它前面一个点的位置
{
s[i][0]=s[i-1][0];
s[i][1]=s[i-1][1];
}
switch(direction)
{
case UP:
s[0][0]--;
break;
case DOWN:
s[0][0]++;
break;
case LEFT:
s[0][1]--;
break;
case RIGHT:
s[0][1]++;
break;
}
}
void drawSnake() //画蛇
{
int i;
for(i=0; i<sLength; i++)
{
gotoxy(s[i][0],s[i][1]); //移动关标到蛇的坐标
printf("@"); //在这个位置画蛇
}
}
void key()
{
if(kbhit()!=0) //如果有键盘输入
{
char in;
while(!kbhit()==0) //如果玩家输入了多个按键,以最后一个按键为准
in=getch();
switch(in)
{
case 'w':
case 'W':
if(direction!=DOWN) //不能缩头吧。。。。
direction=UP;
break;
case 's':
case 'S':
if(direction!=UP)
direction=DOWN;
break;
case 'a':
case 'A':
if(direction!=RIGHT)
direction=LEFT;
break;
case 'd':
case 'D':
if(direction!=LEFT)
direction=RIGHT;
break;
case 'p':
case 'P':
gotoxy(H,0); //将光标移动到下面
system("pause");
gotoxy(H,0);
printf(" "); //消去下面的按任意键继续
break;
}
}
}
int main()
{
init(); //程序开始时的初始化操作
drawMap(); //画地图
food();
while(1)
{
drawSnake(); //画蛇
Sleep(WAIT_TIME); //等待一段时间
key();
move(); //移动蛇(主要是修改蛇身数组的数据)
if(a[s[0][0]][s[0][1]]==-1) //如果蛇头碰到食物,就重新投放食物,并且把食物点重置为0
{
eated=true; //标记已经吃到食物
food();
a[s[0][0]][s[0][1]]=0; //去掉食物
}
}
getchar();
return 0;
}
|