c++实现双人见缝插针

c++实现双人见缝插针,第1张

 实现的要求:

(1)随着游戏的进行,针的速度越来越快,游戏难度越大;

(2)中间显示为“旋转蛇”的图像,每被针击中一次,换一次颜色;

(3)在右边新增一位玩家,并分别制作出计两位玩家的合作版和两个分别算分的竞争版。


代码:

合作版:

#include
#include
#include
#include


int linenum = 0;
int hy()
{
	float PI = 3.1415926;

	int x, y, r, i;
	float h = rand() % 180;
	x = 400, y = 300;
	float offset, totaloffset = 0;
	COLORREF color1 = HSVtoRGB(linenum*5, 0.9, 0.8);
	COLORREF color2 = HSVtoRGB(linenum *5+ 180, 0.9, 0.7);
	
			for (r =60; r > 0; r = r - 10) {
				int left = x - r;
				int top = y - r;
				int right = x + r;
				int bottom = y + r;
				for (i = 0; i < 20; i++) {
					offset = i * PI / 10 + totaloffset;
					setfillcolor(color1);
					solidpie(left, top, right, bottom, offset, offset + 2 * PI / 60);
					setfillcolor(RGB(255, 255, 255));
					solidpie(left, top, right, bottom, offset + 2 * PI / 60, offset + 3 * PI / 60);
					setfillcolor(color2);
					solidpie(left, top, right, bottom, offset + 3 * PI / 60, offset + 5 * PI / 60);
					setfillcolor(RGB(0, 0, 0));
					solidpie(left, top, right, bottom, offset + 5 * PI / 60, offset + 6 * PI / 60);

				}
				totaloffset = totaloffset + PI / 20;
	}
	return 0;
}
int main() {
	const float pi = 3.1415926;
	int height, width;
	height = 600, width = 800;
	initgraph(width, height);
	setbkcolor(RGB(255, 255, 255));
	setlinestyle(PS_SOLID, 3);
	float lineLength = 150, xend, yend, rotatespeeed = pi / 360;
	
	float angles[1000];
	int score = 0;
	int i;
	BeginBatchDraw();
	while (1) {
		cleardevice();
		setlinecolor(RGB(0, 0, 0));
		line(0, height / 2, lineLength, height / 2);
		line(650, height / 2, 800, height / 2);
		for (i = 0; i < linenum; i++) {
			angles[i] = angles[i] + rotatespeeed;
			if (angles[i] > 2 * pi) {
				angles[i] = angles[i] - 2 * pi;
			}
			xend = lineLength * cos(-angles[i]) + width / 2;
			yend = lineLength * sin(-angles[i]) + height / 2;
			setlinecolor(RGB(0, 0, 255));
			if (i == linenum - 1) {
				setlinecolor(RGB(255, 0, 0));
			}
			line(xend, yend, width / 2, height / 2);
		}
		if (_kbhit() && rotatespeeed != 0) {
			char input = _getch();
			if (input == ' ') {
				linenum++;
				rotatespeeed += pi / 7200;
				angles[linenum - 1] = pi;
				xend = lineLength * cos(-angles[i - 1]) + width / 2;
				yend = lineLength * sin(-angles[i - 1]) + height / 2;
				line(xend, yend, width / 2, height / 2);
				for (i = 0; i < linenum - 1; i++) {
					if (fabs(angles[linenum - 1] - angles[i]) < pi / 60) {
						rotatespeeed = 0;
						break;
					}
				}
				
				score = score + 1;
			}
			if (input == 'a') {
				linenum++;
				rotatespeeed += pi / 7200;
				angles[linenum - 1] = 0;
				xend = lineLength * cos(-angles[i - 1]) + width / 2;
				yend = lineLength * sin(-angles[i - 1]) + height / 2;
				line(xend, yend, width / 2, height / 2);
				for (i = 0; i < linenum - 1; i++) {
					if (fabs(angles[linenum - 1] - angles[i]) < pi / 60) {
						rotatespeeed = 0;
						break;
					}
				}

				score = score + 1;
			}
		}
		hy();
		setlinecolor(HSVtoRGB(0, 0.9, 0.8));
		TCHAR s[20];
		_swprintf(s, _T("%d"), score);
		settextstyle(50, 0, _T("TIMES"));
		settextcolor(RGB(50, 50, 50));
		outtextxy(65, 200, s);
		FlushBatchDraw();
		Sleep(10);
	}
	closegraph();
	return  0;
}

 

 

 

 

 竞技版:

#include
#include
#include
#include


int linenum = 0;
int hy()
{
	float PI = 3.1415926;

	int x, y, r, i;
	float h = rand() % 180;
	x = 400, y = 300;
	float offset, totaloffset = 0;
	COLORREF color1 = HSVtoRGB(linenum*5, 0.9, 0.8);
	COLORREF color2 = HSVtoRGB(linenum *5+ 180, 0.9, 0.7);
	
			for (r =60; r > 0; r = r - 10) {
				int left = x - r;
				int top = y - r;
				int right = x + r;
				int bottom = y + r;
				for (i = 0; i < 20; i++) {
					offset = i * PI / 10 + totaloffset;
					setfillcolor(color1);
					solidpie(left, top, right, bottom, offset, offset + 2 * PI / 60);
					setfillcolor(RGB(255, 255, 255));
					solidpie(left, top, right, bottom, offset + 2 * PI / 60, offset + 3 * PI / 60);
					setfillcolor(color2);
					solidpie(left, top, right, bottom, offset + 3 * PI / 60, offset + 5 * PI / 60);
					setfillcolor(RGB(0, 0, 0));
					solidpie(left, top, right, bottom, offset + 5 * PI / 60, offset + 6 * PI / 60);

				}
				totaloffset = totaloffset + PI / 20;
	}
	return 0;
}
int main() {
	const float pi = 3.1415926;
	int height, width;
	height = 600, width = 800;
	initgraph(width, height);
	setbkcolor(RGB(255, 255, 255));
	setlinestyle(PS_SOLID, 3);
	float lineLength = 150, xend, yend, rotatespeeed = pi / 360;
	
	float angles[1000];
	int score1= 0,score2=0;
	int i;
	BeginBatchDraw();
	while (1) {
		cleardevice();
		setlinecolor(RGB(0, 0, 0));
		line(0, height / 2, lineLength, height / 2);
		line(650, height / 2, 800, height / 2);
		for (i = 0; i < linenum; i++) {
			angles[i] = angles[i] + rotatespeeed;
			if (angles[i] > 2 * pi) {
				angles[i] = angles[i] - 2 * pi;
			}
			xend = lineLength * cos(-angles[i]) + width / 2;
			yend = lineLength * sin(-angles[i]) + height / 2;
			setlinecolor(RGB(0, 0, 255));
			if (i == linenum - 1) {
				setlinecolor(RGB(255, 0, 0));
			}
			line(xend, yend, width / 2, height / 2);
		}
		if (_kbhit() && rotatespeeed != 0) {
			char input = _getch();
			if (input == ' ') {
				linenum++;
				rotatespeeed += pi / 7200;
				angles[linenum - 1] = pi;
				xend = lineLength * cos(-angles[i - 1]) + width / 2;
				yend = lineLength * sin(-angles[i - 1]) + height / 2;
				line(xend, yend, width / 2, height / 2);
				for (i = 0; i < linenum - 1; i++) {
					if (fabs(angles[linenum - 1] - angles[i]) < pi / 60) {
						rotatespeeed = 0;
						break;
					}
				}
				
				score1 = score1 + 1;
			}
			if (input == 'a') {
				linenum++;
				rotatespeeed += pi / 7200;
				angles[linenum - 1] = 0;
				xend = lineLength * cos(-angles[i - 1]) + width / 2;
				yend = lineLength * sin(-angles[i - 1]) + height / 2;
				line(xend, yend, width / 2, height / 2);
				for (i = 0; i < linenum - 1; i++) {
					if (fabs(angles[linenum - 1] - angles[i]) < pi / 60) {
						rotatespeeed = 0;
						break;
					}
				}

				score2 = score2 + 1;
			}
		}
		hy();
		setlinecolor(HSVtoRGB(0, 0.9, 0.8));
		TCHAR s[20];
		_swprintf(s, _T("%d"), score1);
		settextstyle(50, 0, _T("TIMES"));
		settextcolor(RGB(50, 50, 50));
		outtextxy(65, 200, s);
		setlinecolor(HSVtoRGB(0, 0.9, 0.8));
		TCHAR s1[20];
		_swprintf(s1, _T("%d"), score2);
		settextstyle(50, 0, _T("TIMES"));
		settextcolor(RGB(50, 50, 50));
		outtextxy(735, 200, s1);
		FlushBatchDraw();
		Sleep(10);
	}
	closegraph();
	return  0;
}

运行截图:

 

作者的一些话:我设置的左边玩家是空格发针,右边是a,这个可以随便设置,看过我之前文章的小伙伴应该知道我这期就是个缝合怪,不过不要紧啦,第一次做了双人游戏还是很高兴。


然后是基本每期基本都说说的安全性(说是安全性不如说是vs的bug):如果不设置的很多正常代码其实也无法运行,安全性对于个人开发者和学者其实必要性不是很大,初学者建议关闭,关闭方法是在调试按钮上选择最后一项,在选择c/c+,在将警告视为错误设置为否,在点击c/c++小三角,在代码生成的安全检测中选择禁用安全检查。


本学渣写文章也不易,希望大家点个赞,大家一起加油学习啊。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/562376.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-01
下一篇 2022-04-01

发表评论

登录后才能评论

评论列表(0条)

保存