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;
}
相关推荐
子春一10 小时前
Flutter for OpenHarmony:构建一个 Flutter 四色猜谜游戏,深入解析密码逻辑、反馈算法与经典益智游戏重构
算法·flutter·游戏
前端不太难11 小时前
HarmonyOS 游戏上线前必做的 7 类极端场景测试
游戏·状态模式·harmonyos
微祎_12 小时前
Flutter for OpenHarmony:构建一个 Flutter 重力弹球游戏,2D 物理引擎、手势交互与关卡设计的工程实现
flutter·游戏·交互
不穿格子的程序员17 小时前
从零开始刷算法——贪心篇1:跳跃游戏1 + 跳跃游戏2
算法·游戏·贪心
微祎_18 小时前
Flutter for OpenHarmony:构建一个 Flutter 镜像绘图游戏,对称性认知、空间推理与生成式交互设计
flutter·游戏·交互
前端不太难19 小时前
HarmonyOS 游戏项目,从 Demo 到可上线要跨过哪些坑
游戏·状态模式·harmonyos
子春一19 小时前
Flutter for OpenHarmony:色彩捕手:基于 CIELAB 色差模型与人眼感知的高保真色彩匹配游戏架构解析
flutter·游戏·架构
前端不太难1 天前
在 HarmonyOS 上,游戏状态该怎么“死而复生”
游戏·状态模式·harmonyos
ujainu1 天前
Flutter + OpenHarmony 游戏开发进阶:用户输入响应——GestureDetector 实现点击发射
flutter·游戏·openharmony
ujainu1 天前
Flutter + OpenHarmony 实现无限跑酷游戏开发实战—— 对象池化、性能优化与流畅控制
flutter·游戏·性能优化·openharmony·endless runner