#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <conio.h>
#define Length 18
#define Width 15
char block='#';
char food='*';
char map[Width][Length];
int Error(char x,char y)
{
switch(x-y)
{
case 3:case -3:case 4:case -4:case 0: return 1;
}
return 0;
}
void Print()
{
printf("\n\n\n\n");
for(int i=0;i<Width;i++)
{
printf("\t\t\t");
for(int j=0;j<Length;j++)
{
switch(map[i][j])
{
case 'b':
case 'w':
case 'a':
case 's':
case 'd':
case 'h':
printf("%c ",block);
break;
case 'm':
printf("%c ",food);
break;
case ' ':
printf(" ");
break;
}
}
printf("\n");
}
}
int main()
{
char Key,Keys;
long xh,yh,xt,yt,xm,ym;
long start,timeover,level;
long X,Y;
memset(map,' ',sizeof(map));
for(long i=0;i<Width;i++)
{
if((i==0)||(i==Width-1))
for(long j=0;j<Length;j++)
map[i][j]='b';
else
map[i][0]=map[i][Length-1]='b';
}
xh=7,yh=9;
xt=7,yt=8;
map[xt][yt]='d';
map[xh][yh]='h';
srand(time(0));
do{
xm=rand()%(Width-1)+1;
ym=rand()%(Length-1)+1;
}while(map[xm][ym]!=' '||(xm!=xh&&ym!=yh));
map[xm][ym]='m';
printf("Please input the level (1-8): ");
scanf("%d",&level);
system("cls");
Print();
Key='d';
while(1)
{
timeover=1;
start=clock();
while((timeover=(clock()-start<=(900-level*100)))&&!kbhit());
{
Keys=Key;
if(timeover)
{
Key=getch();
if(Error(Key,Keys))
{
Key=Keys;
continue;
}
}
switch(Key)
{
case 'W':case 'w':map[xh][yh]='w';xh--;break;
case 'S':case 's':map[xh][yh]='s';xh++;break;
case 'A':case 'a':map[xh][yh]='a';yh--;break;
case 'D':case 'd':map[xh][yh]='d';yh++;break;
}
if(map[xh][yh]!=' '&&map[xh][yh]!='m')
{
printf("\t\t\tGame Over\n");
return 0;
}
if(map[xh][yh]=='m')
{
srand(time(0));
do{
xm=rand()%(Width-1)+1;
ym=rand()%(Length-1)+1;
}while(map[xm][ym]!=' '||(xm!=xh&&ym!=yh));
map[xm][ym]='m';
}
else
{
switch(map[xt][yt])
{
case 'w':map[xt][yt]=' ';xt--;break;
case 's':map[xt][yt]=' ';xt++;break;
case 'a':map[xt][yt]=' ';yt--;break;
case 'd':map[xt][yt]=' ';yt++;break;
}
}
map[xh][yh]='h';
}
system("cls");
Print();
}
}
精简版贪吃蛇
最新推荐文章于 2022-11-14 08:22:34 发布