我是靠谱客的博主 怕孤单发夹,最近开发中收集的这篇文章主要介绍easyx反弹小球,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#include<graphics.h>
#include<conio.h>
#include<time.h>
#define high 480                    //游戏画面尺寸
#define width 640               
#define maxnum 30                   //小球最多个数

int main()
{
	srand(time(0));
	float ball_x[maxnum], ball_y[maxnum];         //小球坐标
	float ball_vx[maxnum], ball_vy[maxnum];         //小球速度
	float radius;                                  //小球半径
	int i, j;
	int ballnum = 15;                               //目前小球数量
	//数据初始化
	radius = 20;
	for (i = 0; i < ballnum; i++)
	{
		ball_x[i] = rand() % int(width - 4 * radius) + 2 * radius;
		ball_y[i] = rand() % int(high - 4 * radius) + 2 * radius;
		ball_vx[i] = (rand() % 2) * 2 - 1;          //求余法
		ball_vy[i] = (rand() % 2) * 2 - 1;
	}

	initgraph(width, high);         //初始化图形环境
	BeginBatchDraw();          //开始批量绘制

	while (1)
	{
		//绘制黑线、黑色填充的圆,消除之前的圆
		setcolor(BLACK);
		setfillcolor(BLACK);
		for (i = 0; i < ballnum; i++)
			fillcircle(ball_x[i], ball_y[i], radius);

		//更新圆的坐标
		for (i = 0; i < ballnum; i++)
		{
			ball_x[i] += ball_vx[i];
			ball_y[i] += ball_vy[i];
		}

		//判断圆是否和墙相撞
		for (i = 0; i < ballnum; i++)
		{
			if ((ball_x[i] <= radius) || (ball_x[i] >= width - radius))
				ball_vx[i] = -ball_vx[i];
			if ((ball_y[i] <= radius) || (ball_y[i] >= high - radius))
				ball_vy[i] = -ball_vy[i];
		}

		float minDistances2[maxnum][2];        //记录某个小球距离最近的小球的距离平方
											   //以及这个小球的下标
		for (i = 0; i < ballnum; i++)
		{
			minDistances2[i][0] = 9999999;
			minDistances2[i][1] = -1;
		}

		//求所有小球两两之间距离平方
		for (i = 0; i < ballnum; i++)
		{
			for (j = 0; j < ballnum; j++)
			{
				if (i != j)             //相同小球之间不需要计算
				{
					float dist2;
					dist2 = (ball_x[i] - ball_x[j]) * (ball_x[i] - ball_x[j]) + (ball_y[i] - ball_y[j]) * (ball_y[i] - ball_y[j]);
					if (dist2 < minDistances2[i][0])
					{
						minDistances2[i][0] = dist2;
						minDistances2[i][1] = j;
					}
				}
			}
		}
		//判断小球之间是否相撞
		for (i = 0; i < ballnum; i++)
		{
			if (minDistances2[i][0] < 4 * radius * radius)      //若最小距离小于阈值,发生碰撞
			{
				j = minDistances2[i][1];
				//交换速度
				int temp;
				temp = ball_vx[i]; ball_vx[i] = ball_vx[j]; ball_vx[j] = temp;
				temp = ball_vy[i]; ball_vy[i] = ball_vy[j]; ball_vy[j] = temp;

				minDistances2[i][0] = 9999999;          //距离重新计算
				minDistances2[i][1] = -1;

			}
		}

		//绘制黄线、绿色填充的圆
		setcolor(YELLOW);
		setfillcolor(GREEN);
		for (i = 0; i < ballnum; i++)
			fillcircle(ball_x[i], ball_y[i], radius);

		FlushBatchDraw();           //执行未完成的绘图任务

		//延时
		Sleep(3);
		if (_kbhit())
		{
			char input;
			input = _getch();
			if (input == ' ')
				if (ballnum < maxnum)
				{
					ball_x[ballnum] = rand() % int(width - 4 * radius) + 2 * radius;
					ball_y[ballnum] = rand() % int(high - 4 * radius) + 2 * radius;
					ball_vx[ballnum] = (rand() % 2) * 2 - 1;
					ball_vy[ballnum] = (rand() % 2) * 2 - 1;
					ballnum++;
				}
		}
	}

	EndBatchDraw();                 //结束批量绘制
	closegraph();                   //关闭图形环境
	return 0;
}

最后

以上就是怕孤单发夹为你收集整理的easyx反弹小球的全部内容,希望文章能够帮你解决easyx反弹小球所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部