#include <STDIO.H>
#include <WINDOWS.H>
#include <CONIO.H>
#define CPRTBLK pos(control_block.x,control_block.y);printf(" ")
#define BPRTBLK pos(ball.x,ball.y);printf(" ")
#define NUM_TARGET 12
void pos(int pos_x,int pos_y){
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos={pos_x,pos_y};
SetConsoleCursorPosition(hOut,pos);
}
void HideCursor(){
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursor={1,0};
SetConsoleCursorInfo(hOut,&cursor);
}
//定义一个方块的结构体
typedef struct block{
int x;
int y;
}BLK;
//定义一个小球的结构体
typedef struct ball{
int x;
int y;
}BALL;
int main(){
int i;
//初始化控制体
BLK control_block={10,20};
pos(control_block.x,control_block.y);
printf("000");
//目标结构体
BLK target[NUM_TARGET];
//初始化小球
BALL ball={11,19};
pos(ball.x,ball.y);
printf("x");
//目标阵列
for(i=0;i<NUM_TARGET;i++){
target[i].y=2;
target[i].x=i*6+2;
pos(target[i].x,target[i].y);
printf("000");
}
//隐藏光标
HideCursor();
//消息循环
while(1){
switch(getch()){
case 'a':
CPRTBLK;
BPRTBLK;
control_block.x--;
ball.x--;
break;
case 'd':
CPRTBLK;
BPRTBLK;
control_block.x++;
ball.x++;
break;
//当按下空格发射子弹
case ' ':
while(ball.y>=target[0].y){
if(ball.y==target[0].y){
pos(ball.x,ball.y);
printf(" ");
ball.y=19;
break;
}
BPRTBLK;
ball.y--;
pos(ball.x,ball.y);
printf("x");
pos(control_block.x,control_block.y);
printf("000");
for(i=0;i<NUM_TARGET;i++){
if(ball.x>=target[i].x && ball.x<=target[i].x+2 && ball.y==target[i].y){
pos(target[i].x,target[i].y);
printf(" ");
}
}
Sleep(50);
}
break;
}
pos(control_block.x,control_block.y);
printf("000");
pos(ball.x,ball.y);
printf("x");
}
return 0;
}