思路来自;https://zhuanlan.zhihu.com/p/24652156
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
int main()
{
int a,b;
int c=5; //定义敌机初始位置
int d=0; //定义敌机初始位置
int x=17; //控制飞机横向位置
int y=17; //控制飞机纵向位置
int score=0;
char input; //符号变量
int isfire = 0; //通过变量检测是否发射子弹
int isKilled = 0; //记录是否击中
int velocity_x = 1;
int velocity_y = 1;
int left = 0;
int right = 15;
int top = 0;
int bottom = 10;
//战机移动相关
while(1)
{
system("cls"); //清屏
Sleep(10);
//输出敌机
c=c+velocity_x;
d=d+velocity_y;
if ((c==left)||(c==right))
velocity_x = -velocity_x;
if ((d==top)||(d==bottom))
velocity_y = -velocity_y;
if(score<=100)
{
for(b=0;b<d;b++)
printf("\n");
for(a=0;a<c;a++)
printf(" ");
printf("o\n");
}
system("cls");
//输出飞机上面的空行
if(isfire==0)
{
for(b=0;b<y;b++)
printf("\n");
}
//输出激光束线
else
{
for(b=0;b<y;b++)
{
for (a=0;a<x;a++)
printf(" ");
printf(" |\n");
}
if (x+2==c)
isKilled=1;// 击中靶子
if (x+2==c)
score++;
isfire = 0;
}
//输出飞机
for(a=0;a<x;a++)
printf(" ");
printf(" *\n");
for(a=0;a<x;a++)
printf(" ");
printf("*****\n");
for(a=0;a<x;a++)
printf(" ");
printf(" * *\n");
printf("\n");
if(kbhit()) // 判断是否有输入
{
input=getch();
if(input=='a')
x--; //左移
if(input=='d')
x++; //右移
if(input=='w')
y--; //上移
if(input=='s')
y++; //下移
if(input==' ')
isfire=1;
}
system("cls");
for(a=0;a<20;a++)
printf("\n");
printf("score=%d ",score);
/**if(score>=100)
break;**/
}
return 0;
}