void keytouch();
void gotoxy(int x,int y);
static int height = 20;//边界高度
static int weight = 20;//边界宽度
static int position_x=height/2;
static int position_y=weight/2;
static int bullet_x = -1;//激光的位置变量
static int bullet_y = -1;//激光的位置变量
//初始位置为(-1,-1),使得激光在坐标轴之外不显示出来,如果设置成(0,0),则一开始会出现在窗口的左上角处
void main()
{
char key = 0;
keytouch();
while(1)
{
Sleep(500);
system("cls");//清屏函数
gotoxy(0,0);//光标移动函数
for(int i=0;i<height;i++)
{
for(int j=0;j<weight;j++)
{
if((i == position_x) && (j == position_y))
printf("*");
else
printf(" ");
}
printf("\n");
}
if(kbhit())
{
key = getch();
if(key == ' ')//激光发射
{
bullet_y = position_y;
bullet_x = position_x-1;
}
}
if(bullet_x > -1)//激光移动
bullet_x --;
}
}
void gotoxy(int x,int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(out,pos);
}
void keytouch()
{
char key = 0;
if(kbhit())
{
key = getch();
if(key == 'a')
position_y --;
if(key == 's')
position_x ++;
if(key == 'd')
position_y ++;
if(key == 'w')
position_x --;
}
}