C语言—用EasyX实现反弹球消砖块游戏

代码效果如下

cpp 复制代码
#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;
}
相关推荐
DisonTangor6 小时前
Ruffle 继续在开源软件中支持 Adobe Flash Player
游戏·adobe
VMOS云手机9 小时前
《仙境传说RO:新启航》游戏攻略,VMOS云手机辅助高效挂机助攻!
游戏·云手机·游戏辅助·黑科技·免费云手机
PC端游爱好者10 小时前
战神诸神黄昏9月19日登录PC端! 手机怎么玩战神诸神黄昏
游戏·智能手机·电脑·远程工作·玩游戏
疑惑的杰瑞1 天前
[C语言]连子棋游戏
c语言·开发语言·游戏
文宇炽筱1 天前
《黑神话:悟空》:中国游戏界的新篇章
游戏·游戏程序·玩游戏
星毅要努力1 天前
【C语言编程】【小游戏】【俄罗斯方块】
c语言·开发语言·学习·游戏
宇宙第一小趴菜1 天前
中秋节特别游戏:给玉兔投喂月饼
python·游戏·pygame·中秋节
这是我581 天前
C++掉血迷宫
c++·游戏·visual studio·陷阱·迷宫·生命·
zhooyu1 天前
C++和OpenGL实现3D游戏编程【目录】
开发语言·游戏·游戏程序
niaoma2 天前
剑灵服务端源码(c#版本+数据库+配套客户端+服务端)
游戏·c#·游戏程序·游戏策划