|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
int i=0,j=0;
int snakey[100]={5,4,3,2,1};
int snakex[100]={1,1,1,1,1};
int life=0;
int lenght=5;
char map[12][24]={"**********************",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"**********************"};
void put_money(int i,int j)
{
int x=0,y=0;
srand((unsigned)time(NULL));
while((map[y][x]==003)||(map[y][x]==002||map[y][x]=='*')||((x==i)&&(y==j)))
{
x=rand()%21+1;
y=rand()%10+1;
}
map[y][x]='$';
return;
}
void output()
{
system("cls");
for(i=0;i<12;i++)
{
for(j=0;j<23;j++)
printf("%c",map[i][j]);
printf("\n");
}
return;
}
void gameover()
{
life=1;
printf("笨蛋,输了吧!!!\n");
return;
}
void turn_up()
{
system("cls");
if( (snakex[0]==1)||(map[snakex[0]-1][snakey[0]]==003) )
gameover();
else
{
if(map[snakex[0]-1][snakey[0]]=='$');
{
put_money(snakey[0],snakex[0]-1);
lenght++;
map[snakex[lenght-1]][snakey[lenght-1]]=003;
}
for(i=lenght;i>0;i--)
{
snakex[i]=snakex[i-1];
snakey[i]=snakey[i-1];
}
map[snakex[lenght]][snakey[lenght]]=' ';
snakex[0]--;
for(i=lenght-1;i>0;i--)
map[snakex[i]][snakey[i]]=003;
map[snakex[0]][snakey[0]]=002;
output();
}
return;
}
void turn_down()
{
system("cls");
if((snakex[0]==10)||(map[snakex[0]+1][snakey[0]]==003))
gameover();
else
{
if(map[snakex[0]+1][snakey[0]=='$'])
{
put_money(snakey[0],snakex[0]+1);
lenght++;
map[snakex[lenght-1]][snakey[lenght-1]]=003;
}
for(i=lenght;i>0;i--)
{
snakex[i]=snakex[i-1];
snakey[i]=snakey[i-1];
}
snakex[0]++;
map[snakex[lenght]][snakey[lenght]]=' ';
for(i=lenght-1;i>0;i--)
map[snakex[i]][snakey[i]]=003;
map[snakex[0]][snakey[0]]=002;
output();
}
return;
}
void turn_left()
{
system("cls");
if ((snakey[0]==1)||(map[snakex[0]][snakey[0]-1]==003))
gameover();
else
{
if(map[snakex[0]][snakey[0]-1]=='$')
{
put_money(snakey[0]-1,snakex[0]);
lenght++;
map[snakex[lenght-1]][snakey[lenght-1]]=003;
}
for(i=lenght;i>0;i--)
{
snakex[i]=snakex[i-1];
snakey[i]=snakey[i-1];
}
map[snakex[lenght]][snakey[lenght]]=' ';
snakey[0]--;
for(i=lenght-1;i>0;i--)
map[snakex[i]][snakey[i]]=003;
map[snakex[0]][snakey[0]]=002;
output();
}
return;
}
void turn_right()
{
system("cls");
if ((snakey[0]==21)||(map[snakex[0]][snakey[0]+1]==003))
gameover();
else
{
if(map[snakex[0]],snakex[0]+1=='$')
lenght++;
map[snakex[lenght-1]][snakey[lenght-1]]=003;
}
for(i=lenght;i>0;i--)
{
snakex[i]=snakex[i-1];
snakey[i]=snakey[i-1];
}
map[snakex[lenght]][snakey[lenght]]=' ';
snakey[0]++;
for(i=lenght-1;i>0;i--)
map[snakex[i]][snakey[i]]=003;
map[snakex[0]][snakey[0]]=002;
output();
return;
}
int main()
{
int i,timeover,hard;
long start;
char name,direcation;
printf("\n 向上移动:W;向下移动:S;向左移动:A;向右移动:D\n");
printf("\t 请选择难度(数字)\n\t分1-5级,分别代表\n\t1难,2中上,3中,4中下,5易:\n");
scanf("%d",&hard);
system("cls");
for(i=1;i<5;i++)
map[1][i]=003;
map[1][5]=002;
put_money(0,0);
output();
while(life!=1)
{
timeover=1;
start=clock();
while((timeover=(clock()-start<=hard*100))&&!kbhit());
if(timeover)
{
direcation=getch();
}
switch(direcation)
{
case'W':turn_up();break;
case'S':turn_down();break;
case'A':turn_left();break;
case'D':turn_right();break;
}
}
system("pause");
return 0;
}
|
|