代码效果如下
#undef UNICODE
#undef _UNICODE
#include<graphics.h>
#include<conio.h>
#include<time.h>
#include<stdio.h>
#define width 640
#define high 480
#define brick_num 10
int ball_x, ball_y;
int ball_vx, ball_vy;
int radius;
int bar_x, bar_y;
int bar_high, bar_width;
int bar_left, bar_right, bar_top, bar_bottom;
int isbrickexisted[brick_num];
int brick_high, brick_width;
int score;
void startup()
{
srand(time(NULL));
ball_x = rand()%(width/3)+width/3;
ball_y = rand()%(high/4)+high/6;
ball_vx = 2;
ball_vy = 1;
radius = 20;
bar_high = high / 20;
bar_width = width / 5;
bar_x = width / 2;
bar_y = high - bar_high / 2;
bar_left = bar_x - bar_width / 2;
bar_right = bar_x + bar_width / 2;
bar_top = bar_y - bar_high / 2;
bar_bottom = bar_y + bar_high / 2;
brick_width = width / brick_num;
brick_high = high / brick_num;
int i;
for (i = 0; i < brick_num; i++)
isbrickexisted[i] = 1;
initgraph(width, high, SHOWCONSOLE);
BeginBatchDraw();
score = 0;
}
void clean()
{
setcolor(BLACK);
setfillcolor(BLACK);
fillcircle(ball_x, ball_y, radius);
bar(bar_left, bar_top, bar_right, bar_bottom);
int i, brick_left, brick_right, brick_top, brick_bottom;
for (i = 0; i < brick_num; i++)
{
brick_left = i * brick_width;
brick_right = brick_left + brick_width;
brick_top = 0;
brick_bottom = brick_high;
if (!isbrickexisted[i])
fillrectangle(brick_left, brick_top, brick_right, brick_bottom);
}
}
void show()
{
setbkcolor(RGB(0, 200, 200));
cleardevice();
setcolor(YELLOW);
setfillcolor(GREEN);
fillcircle(ball_x, ball_y, radius);
bar(bar_left, bar_top, bar_right, bar_bottom);
int i, brick_left, brick_right, brick_top, brick_bottom;
for (i = 0; i < brick_num; i++)
{
brick_left = i * brick_width;
brick_right = (i + 1) * brick_width;
brick_top = 0;
brick_bottom = brick_high;
if (isbrickexisted[i])
{
setcolor(GREEN);
setfillcolor(YELLOW);
fillrectangle(brick_left, brick_top, brick_right, brick_bottom);
}
}
char s[5];
sprintf_s(s, "%d", score);
settextcolor(YELLOW);
settextstyle(30, 0, s);
outtextxy(0, high-high/6, "消的砖块数:");
outtextxy(30*6, high-high/6, s);
FlushBatchDraw();
Sleep(1);
}
void updatewithoutinput()
{
if (ball_y + radius >= bar_top && ball_x >= bar_left && ball_x <= bar_right)
{
ball_vy = 4;
ball_vy = -ball_vy;
int sign = 1;
if (ball_vx < 0)
sign = -1;
ball_vx = (rand() % 3 + 2) * sign;
}
ball_x += ball_vx;
ball_y += ball_vy;
float distant_right,distant_left;
distant_right = (ball_x - bar_right) * (ball_x - bar_right) + (ball_y - bar_top) * (ball_y - bar_top);
distant_left= (ball_x - bar_left) * (ball_x - bar_left) + (ball_y - bar_top) * (ball_y - bar_top);
if (distant_right <= radius * radius + 1 || distant_left <= radius * radius + 1)
{
ball_vx = -ball_vx;
ball_vy = -ball_vy;
}
if (ball_x - radius <= 0 || ball_x + radius >= width)
ball_vx = -ball_vx;
if (ball_y - radius <= 0)
ball_vy = -ball_vy;
if (ball_y + radius >= high)
{
printf("\n游戏失败!\n");
Sleep(1000);
EndBatchDraw();
closegraph();
exit(0);
}
int i, brick_left, brick_right, brick_top, brick_bottom;
for (i = 0; i < brick_num; i++)
{
if (isbrickexisted[i])
{
brick_left = brick_width * i;
brick_right = brick_width * (i + 1);
brick_top = 0;
brick_bottom = brick_high;
if (ball_y - radius <= brick_bottom && ball_x >= brick_left && ball_x <= brick_right)
{
score++;
printf("\a");
isbrickexisted[i] = 0;
ball_vy = -ball_vy;
}
}
}
int flag = 0;
for (i = 0; i < brick_num; i++)
{
if (isbrickexisted[i] == 1)
flag = 1;
}
if (flag == 0)
{
for (i = 0; i < brick_num; i++)
{
isbrickexisted[i] = 1;
}
}
}
void updatewithinput()
{
char input;
if (_kbhit())
{
input = _getch();
if (input == 'a'&&bar_left>0)
{
bar_x = bar_x - 15;
bar_left = bar_x - bar_width/2;
bar_right = bar_x + bar_width/2;
}
if (input == 'd'&&bar_right<width)
{
bar_x += 15;
bar_left = bar_x - bar_width/2;
bar_right = bar_x + bar_width/2;
}
}
}
void gameover()
{
EndBatchDraw();
closegraph();
}
int main()
{
system("pause");
startup();
while (1)
{
clean();
updatewithoutinput();
updatewithinput();
show();
}
gameover();
return 0;
}