C语言躲避球游戏
#include<stdio.h>
#include<Windows.h>
#include<time.h>
#include<conio.h>
#define SIZE 20
#define stone -1
#define fly 1
void initialization(int *site,int map[SIZE][SIZE])
{
map[0][0]=1;
*site=0;
}
int getCommand()
{
int command=0;
if(_kbhit())
{
switch (_getch())
{
case 'F': case 'f': command = 1; break; //表示向上
case 'J': case 'j': command = -1; break; //表示向下
}
}
return command;
}
int move(int command,int map[SIZE][SIZE],int *site)
{
if((*site)-command>-1&&(*site)-command<SIZE)//移动飞机
{
if(command==1)
{
map[*site][0]=0;
map[*site-1][0]=fly;
*site =*site -1;
}
if(command==-1)
{
map[*site][0]=0;
map[*site+1][0]=fly;
*site =*site +1;
}
}
return 0 ;
}
create(int map[SIZE][SIZE],int nor)//生成石头 并移动
{
int i,j;
if(nor%3==0)
{
i = rand() % SIZE;
if (map[i][SIZE-1] == 0)
{
map[i][SIZE-1] = -1;
}
}
for(i=SIZE;i>=0;i--)
{
for(j=0;j<=SIZE;j++)
{
if(i!=0&&j!=0)
{
if(map[i][j]==stone)
{
map[i][j]=0;
map[i][j-1]=stone;
}
}
}
}
}
draw(int map[SIZE][SIZE],int score)
{
int i, j,num=0;
system("cls"); //清除屏幕
for (i = 0; i <= SIZE; i++) //输出上边框
{
if (i == 0)
{
printf("┏");
}
else if (i == SIZE)
{
printf(" ━━┓");
}
else
{
printf(" ━");
}
}
printf("\n");
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
if (j == 0) //输出左边框
{
printf("┃ ");
}
if (map[i][j] == fly) //输出飞机
{
num++;
printf("○");
}else if (map[i][j] == stone) //输出石头
{
printf("★");
}else{
printf(" ");
}
if (j == SIZE - 1) //输出右边框
{
printf("┃ ");
}
}
printf("\n");
}
for (i = 0; i <= SIZE; i++) //输出下边框
{
if (i == 0)
{
printf("┗");
}
else if (i == SIZE)
{
printf(" ━━┛");
}
else
{
printf(" ━");
}
}
printf("\n");
if(num==0)
{
printf("你输了\n最终得分:%d",score);
exit(1);
}
}
int main(int argc, char *argv[])
{
int command=0,map[SIZE][SIZE]={0},site=0,result;
int score=0,nor=1;
initialization(&site,map);
while (1)
{
command = getCommand(command); //获取玩家输入的命令
result = move(command,map,&site); //根据命令移动飞机
if (result == 1) //分析移动的结果
{
break;
}
else
{
draw(map,score);
create(map,nor);
prin
tf("command:%d\n",command);
printf("score:%d",score/3);
}
nor++;
score++;
Sleep(50);
}
}