0
点赞
收藏
分享

微信扫一扫

c++实现“见缝插圆”游戏

勇敢乌龟 2022-04-02 阅读 111
c++

要求:1利用数组记录多个圆;

           2控制圆圈的最大和最小半径,且圆和圆之间不相交;

           3按空格键实现风格变化,分别是黄色,随机色,随机叠加色,随机色圈。

代码:

          


#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<time.h>
float Dist2point(float x1, float y1, float x2, float y2) {
	float result1;
	result1 = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
	return result1;
}

int isTwociclesIntersect(float x1, float y1, float x2, float y2, float r1, float r2) {
	if (Dist2point(x1, y1, x2, y2) < r1 + r2) {
		return 1;
	}
	return 0;
}
void DrawCircles1(float x1, float y1, float r) {
	setlinecolor(RGB(0, 0, 0));
	setfillcolor(RGB(255, 255, 0));
	fillcircle(x1, y1, r);
}
void DrawCircles2(float x, float y, float r) {
	srand((INT)time(0));
	float h = rand() % 360;
	COLORREF COLOR = HSVtoRGB(h, 0.6, 0.7);
	setfillcolor(COLOR);
	fillcircle(x, y, r);
}
void DrawCircles3(float x, float y, float r) {
	srand((INT)time(0));
	while (r > 0) {
		float h = rand() % 360;
		COLORREF COLOR = HSVtoRGB(h, 0.6, 0.7);
		setfillcolor(COLOR);
		fillcircle(x, y, r);
		r -= 5;
	}
}
void DrawCircles4(float x1, float y1, float r) {
	srand((INT)time(0));
	while (r> 0) {
		float h = rand() % 360;
		COLORREF COLOR = HSVtoRGB(h, 0.6, 0.7);
		setlinecolor(COLOR);
		circle(x1,y1,r);
		r -= 5;
	}
}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

int main()
{
	int width = 600, height = 800;
	initgraph(width, height);
	setbkcolor(RGB(255, 255, 255));
	cleardevice();
	srand(time(0));
	int xarray[1000], yarray[1000], rarray[1000];
	int min = 8, max = 50;
	float x, y, r;
	int circlenum = 0;
	int i, j, drawMode = 2;
	int isNewcircleok = 0;

	while (circlenum < 1000) {
		isNewcircleok = 0;
		while (isNewcircleok == 0) {
			if (kbhit()) {
				char input = _getch();
				if (input == ' ') {
					circlenum = 0;
					cleardevice();
					drawMode += 1;
				}
				if (drawMode > 4) {
					drawMode = 1;
				}
			}
			x = rand () % width;
			y = rand() % height;
			r = min;
			for ( i = 0; i < circlenum; i++) {
				if(isTwociclesIntersect(xarray[i], yarray[i], x, y, rarray[i], r))
				break;
			}
			if (circlenum == i) {
				isNewcircleok = 1;
			}
		}
		isNewcircleok = 0;
		while (isNewcircleok == 0 && r < max) {
			r++;
			for ( j= 0; j<circlenum;j++) {
				if (isTwociclesIntersect(xarray[j],yarray[j], x, y, rarray[j], r)) {
					isNewcircleok = 1;
					break;
				}
			}
		}
		xarray[i] = x;
		yarray[i] = y;
		rarray[i] = r;
		circlenum++;
		if (drawMode == 1)
			DrawCircles1(x, y, r);
		if (drawMode == 2)
			DrawCircles2(x,y, r);
		if (drawMode == 3)
			DrawCircles3(x, y, r);
		if (drawMode == 4)
			DrawCircles4(x, y, r);
		Sleep(10);
	}
	_getch();
	closegraph();
	return 0;
}

运行结果:

风格1:

 

 风格2:

 风格3:

 风格4:

 

举报

相关推荐

0 条评论