1、// 二维数组的刷屏综合练习 编制于2012.9.23
#include
2、 void movebul(char scr[WIDTH][WIDTH]);//子弹移动 void bump(char scr[WIDTH][WIDTH]);//碰撞函数 /********************全局变量************************************************/ //全局变量 char scr[WIDTH][WIDTH]; int flagbul = 0 ,flagpla = 0;//在函数体外赋初值 int high,wide;//飞机的行和列 /*********************数组操作函数*****
3、/ //数组赋原始值 void init() { int i,j; for(i = 0; i < WIDTH ; i++) { for(j = 0; j < WIDTH; j++) { scr[i][j] = 0; } printf("\n"); } return ; } //显示当前数组动画 void print() { int i,j; for(i = 0; i < WIDTH ; i++) { for(j
4、 = 0; j < WIDTH ; j++) { if(scr[i][j] == 0) printf(" "); if(scr[i][j] == 1) printf("A");//飞机 if(scr[i][j] == 2) printf("@");//炸弹 if(scr[i][j] == 3) printf(".");//子弹 if(j == WIDTH-2 ) printf("|");//围栏 if(j == WIDTH-1 && i == 0)
5、 printf("得分:%d",flagbul*10);//得分信息 if(j == WIDTH-1 && i ==1) printf("死亡:%d",flagpla);//死亡信息 } printf("\n"); } return ; } //炸弹移动函数(实质是改变数组值) void movebom(char scr[WIDTH][WIDTH]) { int i,j; for(i = WIDTH-1; i >= 0; i--) {for(j = 0; j < WID
6、TH; j++)
{
if(scr[i][j] == 2)
{
scr[i][j] = 0;
scr[i+1][j] = 2;
}
}
}
return;
}
//子弹移动函数(实质是改变数组值)
void movebul(char scr[WIDTH][WIDTH])
{
int i,j;
for(i=1; i 7、i][j] = 0;
scr[i-1][j] = 3;
}
if(scr[0][j] == 3)//用于消除0行的死角
scr[0][j] = 0;
}
}
return;
}
//碰撞函数(实质是数组里的数据呈现某种状态)
void bump(char scr[WIDTH][WIDTH])
{
int i,j;
for(i = 0; i < WIDTH -2; i++)
{for(j = 0; j < WIDTH; j++)
{
if(scr[i][j] == 2 && 8、scr[i+2][j] == 3)//炸弹和子弹相遇(奇数行的情况)
{
scr[i][j] = 0;
scr[i+2][j] = 0;
flagbul++;
}
if(scr[i][j] == 2 && scr[i+1][j] == 3)//炸弹和子弹相遇(偶数行的情况)
{
scr[i][j] = 0;
scr[i+1][j] = 0;
flagbul++;
}
if(scr[i][j] == 9、2 && scr[i+2][j] == 1)//炸弹和飞机相遇(奇数行的情况)
{
scr[i][j] = 0;
scr[i+2][j] = 0;
high = WIDTH-1;//飞机行初值
wide = WIDTH-2;//飞机列初值
scr[high][wide] = 1;//飞机复位
flagpla++;
}
if(scr[i][j] == 2 && scr[i+1][j] == 1)//炸弹和飞机相遇(偶数行的 10、情况)
{
scr[i][j] = 0;
scr[i+1][j] = 0;
high = WIDTH-1;//飞机行初值
wide = WIDTH-2;//飞机列初值
scr[high][wide] = 1;//飞机复位
flagbul++;
}
}
}
return;
}
/*********************************主函数****************************** 11、/
//主函数
int main()
{
int i;
high = WIDTH-1;//飞机行初值
wide = WIDTH-2;//飞机列初值
init();//数组初值函数
scr[high][wide] = 1;//设置数组这个位置为1
srand(time(NULL));//设置随机种子
while(1)
{
if(kbhit())//有按键kbhit()返回1,执行switch()语句,否则返回0,不执行switch()语句
switch(getch())
{
cas 12、e 'a':
case 'A'://像左移动飞机
if(wide > 0)
{
scr[high][wide] = 0;
scr[high][--wide] = 1;
}
break;
case 'd':
case 'D'://像右移动飞机
if(wide < WIDTH-2)//这里是WIDTH-2
{
scr[high][wide] = 0;
scr[high][++wide] = 1;
}
13、 break;
case 'w':
case 'W'://像上移动飞机
if(high > 0)
{
scr[high][wide] = 0;
scr[--high][wide] = 1;
}
break;
case 's':
case 'S'://像下移动飞机
if(high < WIDTH-2)//这里是WIDTH-2
{
scr[high][wide] = 0;
scr[++high][wide] = 1; 14、
}
break;
case 'o':
case 'O'://发出子弹
if(high < WIDTH)//这里是WIDTH
scr[high-1][wide] = 3;
break;
}
//设置炸弹的改变速度是子弹和飞机的一半
if(++i % 2 == 0)
{if(i == 3000)//防止i越界
i = 0;
scr[0][rand()%(WIDTH-1)] = 2; //用随机函数设置炸弹起始位置
movebom(scr); //炸弹移动函数(用于更改数组值)
}
movebul(scr); //子弹移动函数(用于更改数组值)
bump(scr);//碰撞函数
system("CLS"); //清屏函数
print(); //显示数组函数
}
return 0;
}
//本程序为飞机射击程序的雏形
//本程序的思想是每改变一次数组,刷新一次屏幕,再输出一次数组
//使用幻灯片原理,最终形成视觉动画,其实很简单(原来普通的二位数组能做出这么美妙的东西)






