#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
int high,width;
int ball_x,ball_y;
int ball_vx,ball_vy;
int position_x,position_y;
int radius;
int left,right;
int ball_number=0;
int speed=0;
int enemy_x=0,enemy_y=12;
int score=0;
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor;
cursor.bVisible = FALSE;
cursor.dwSize = sizeof(cursor);
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(handle, &cursor);
}
void gotoxy(int x, int y)
{
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}
void startup()
{
high = 20;
width = 25;
ball_x = 1;
ball_y = width/2;
ball_vx = 1;
ball_vy = 1;
position_x=high;
position_y=width/2;
radius = 5;
left = position_y - radius;
right = position_y + radius;
HideCursor();
}
void show()
{
int i,j;
gotoxy(0,0);
for(i=0;i<=high+1;i++)
{
for(j=0;j<=width;j++)
{
if((i==ball_x)&&(j==ball_y))
printf("O");
else if(j==width)
printf("|");
else if(i==high+1)
printf("-");
else if((i==high)&&(j>=left)&&(j<=right))
printf("*");
else if((i==enemy_x)&&(j==enemy_y))
printf("B");
else
printf(" ");
}
printf("\n");
}
printf("反弹小球数 %d\n",ball_number);
printf("分数 %d\n",score);
}
void updatewithinput()
{
char input ;
if(kbhit())
{
input = getch();
if(input=='a')
{
position_y--;
left = position_y - radius;
right = position_y + radius;
}
if(input=='d')
{
position_y++;
left = position_y - radius;
right = position_y + radius;
}
}
}
void updatewithoutinput()
{
if(speed<10)
speed++;
if(speed==10)
{
if((ball_x==0)||(ball_x==high-1))
ball_vx = -ball_vx;
if((ball_y==0)||(ball_y==width-1))
ball_vy = -ball_vy;
if(ball_x==high-1)
{
if((ball_y>=left-1)&&(ball_y<=right+1))
{
ball_number++;
ball_vy = -ball_vy;
}
else
{
printf("GAME OVER !");
exit (0);
}
}
ball_x=ball_x+ball_vx;
ball_y=ball_y+ball_vy;
if((ball_x==enemy_x)&&(ball_y>=enemy_y-2)&&(ball_y<=enemy_y+2))
{
score++;
enemy_x=0;
enemy_y=rand() %width;
}
speed=0;
}
}
int main()
{
startup();
while(1)
{
show();
updatewithoutinput();
updatewithinput();
}
return 0;
}