贪吃蛇部分代码,吃了食物不能就不行了,求教大神!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#include <windows.h>#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#define S 100
#define HEIGHT 20
#define WIDTH 80
int m,n;
void gotoxy(int x ,int y)
{
COORD pos={x,y};
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut,pos);
}
void block()
{
int i=0,j=0;
gotoxy(i,j);
while (i<WIDTH)
{
printf("@");
i++;
}
i=0;j=1;
while (j<HEIGHT-1)
{
gotoxy(i,j);
printf("@");
gotoxy(i+WIDTH-1,j);
printf("@");
j++;
}
while (i<WIDTH)
{
gotoxy(i,j);
printf("@");
i++;
}
}
food()
{
int x=(int)(rand()%79)+1;
int y=(int)(rand()%19)+1;
m=x;n=y;
gotoxy(x,y);
printf("*");
}
void start()
{
int i,j;
int count=0;
char ch;
int a[WIDTH*HEIGHT]={5,6,7,8};
int b[WIDTH*HEIGHT]={5,5,5,5};
food();
for (i=0;;i++)
{
gotoxy(a[i],b[i]);
printf(" ");
for (j=1;j<count+3;j++)
{
gotoxy(a[i+j],b[i+j]);
printf("*");
}
gotoxy(a[i+j],b[i+j]);
printf("#");
if (kbhit())
{
ch=getch();
if (ch=='W'||ch=='w')
{
a[i+j+1]=a[i+j];b[i+j+1]=b[i+j]-1;
}
else if (ch=='S'||ch=='s')
{
a[i+j+1]=a[i+j];b[i+j+1]=b[i+j]+1;
}
else if (ch=='A'||ch=='a')
{
a[i+j+1]=a[i+j]-1;b[i+j+1]=b[i+j];
}
else if (ch=='D'||ch=='d')
{
a[i+j+1]=a[i+j]+1;b[i+j+1]=b[i+j];
}
}
else if (b[i+j]==b[i+j-1]-1)
{
a[i+j+1]=a[i+j];b[i+j+1]=b[i+j]-1;
}
else if (b[i+j]==b[i+j-1]+1)
{
a[i+j+1]=a[i+j];b[i+j+1]=b[i+j]+1;
}
else if (a[i+j]==a[i+j-1]-1)
{
a[i+j+1]=a[i+j]-1;b[i+j+1]=b[i+j];
}
else if (a[i+j]==a[i+j-1]+1)
{
a[i+j+1]=a[i+j]+1;b[i+j+1]=b[i+j];
}
if (m==a[i+j+1]&&n==b[i+j+1])
{
count++;
food();
}
Sleep(S);
}
}
int main()
{
srand((unsigned)time(NULL));
block();
start();
}