贪吃蛇
C++#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<windows.h>
#include<conio.h>
using namespace std;
char map[60][60];
int ewsn[5][3]={{1,0},{-1,0},{0,1},{0,-1}};
int time_=500;
int a[1000],b[1000],a1[1000],b1[1000];
int count=0;
void put()
{
int i;
system("cls");
for(i=1;i<=15;i++)
puts(map[i]);
printf("\n 你的速度:%.3lf秒一格 你吃了%d颗豆豆",1.0*time_/1000,count);
Sleep(time_);
}
int main()
{
int i,j,n,x,t=1,x1,x2;
char color[10]={"color "};
system(color);
printf("按任意键继续");
while(!kbhit());
getchar();
system("cls");
printf("“!”与边框不可以触碰哦\n按任意键继续");
while(!kbhit());
getchar();
system("cls");
printf("“*”是豆豆,尽情吃豆豆吧!\n按任意键开始游戏");
while(!kbhit());
getchar();
for(i=0;i<=15;i++) for(j=0;j<=35;j++) map[i][j]=' ';
for(i=1;i<=35;i++) map[1][i]='-';
for(i=1;i<=35;i++) map[15][i]='-';
for(i=1;i<=15;i++) map[i][1]='|';
for(i=1;i<=15;i++) map[i][35]='|';
put();
srand(time(0));
for(i=0;i<3;i++)
map[rand()%13+2][rand()%33+2]='!';
for(i=0;i<3;i++)
{
do{
x1=rand()%13+2;
x2=rand()%33+2;
}while(map[x1][x2]!=' ');
map[x1][x2]='*';
}
a[0]=rand()%9+4;
b[0]=rand()%29+4;
n=rand()%4;
map[a[0]][b[0]]='@';
put();
map[a[0]][b[0]]='O';
t++;
a[1]=a[0];
b[1]=b[0];
while(1)
{
for(i=0;i<t;i++)
{
a1[i]=a[i];
b1[i]=b[i];
}
a[0]+=ewsn[n][0];
b[0]+=ewsn[n][1];
for(i=1;i<t;i++)
{
a[i]=a1[i-1];
b[i]=b1[i-1];
map[a[i]][b[i]]='O';
}
map[a1[t-1]][b1[t-1]]=' ';
if(rand()%(60-time_/10)==0)
{
do
{
x1=rand()%13+2;
x2=rand()%33+2;
}while(map[x1][x2]!=' ');
map[x1][x2]='!';
}
if(map[a[0]][b[0]]=='*')
{
t++;
count++;
time_-=rand()%(time_/15+1);
do
{
x1=rand()%13+2;
x2=rand()%33+2;
}while(map[x1][x2]!=' ');
map[x1][x2]='*';
}
if(map[a[0]][b[0]]!=' '&&map[a[0]][b[0]]!='*')
{
for(j=0;j<10;j++)
{
system("color 40");
Sleep(5);
system("color 04");
Sleep(5);
}
system("cls");
system(color);
printf("你死了\n你的最高速度:%.3lf秒一格\n你吃了%d颗豆豆\n按任意键退出游戏",1.0*time_/1000,count);
while(!kbhit());
getchar();
return 0;
}
map[a[0]][b[0]]='@';
put();
if(kbhit())
{
x=getch();
if(x==224) x=getch();
if((x=='a'||x==75)&&n!=2) n=3;
if((x=='d'||x==77)&&n!=3) n=2;
if((x=='w'||x==72)&&n!=0) n=1;
if((x=='s'||x==80)&&n!=1) n=0;
}
time_-=rand()%(time_/120+1);
}
return 0;
}