C++游戏编程--游戏名称:音乐闪光圣诞树

C++游戏编程--游戏名称:音乐闪光圣诞树,第1张

C++游戏编程--游戏名称:音乐闪光圣诞树

 编译环境:Mictosoft Visual Studio 2022, EasyX_2021

作  者:代码骑士<1696297834@qq.com>

最后修改:2021-12-16

演示:

(多看一眼,图片会动……)

 ​

 ========================================================================

目录: 1、黑夜 2、画树 3、飘雪 4、灯光闪闪  5、退出

======================================================================= 

思路:

1、黑夜:非常简单,创建一个黑色画布即可。

2、画树:数组画树,颜色填充,老方法了,也很简单。

3、飘雪:创建结构体数组存储雪花,使雪花随机产生并使其向下运动。参考文档: EasyX 文档 - Stars

4、灯光闪闪:在树叶上面挂上彩灯,同样对二维数组进行 *** 作,并获取延时,能使灯光闪烁。

5、开关语句控制功能实现与退出。

========================================================================

源码:
#include
#include
#include
#include
using namespace std;

#pragma comment(lib,"Winmm.lib")

const int width = 600;

const int length = 600;

const int MAXSNOW = 200;

const int MAXLIGHT = 20;

const int BOUNDS = 20;

//const int SZ = 10;

int key = 0;//功能选择键:2下雪,3点灯

IMAGE gift;

struct SNOW
{
	int 	  x;
	double    y;//沿y轴下落
	double step;//下落速度
	int   color;
};

SNOW snow[MAXSNOW];//结构体数组(200颗雪花)

int TREE[BOUNDS][BOUNDS] = {
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//15-x
	{0,0,0,0,0,0,0,7,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,1,1,1,0,0,0,0,0,0},
	{0,0,0,0,0,7,7,7,7,7,0,0,0,0,0},
	{0,0,0,0,1,1,1,1,1,1,1,0,0,0,0},
	{0,0,0,0,0,7,7,7,7,7,0,0,0,0,0},
	{0,0,0,0,1,1,1,1,1,1,1,0,0,0,0},
	{0,0,0,7,7,7,7,7,7,7,7,7,0,0,0},
	{0,0,0,0,1,1,1,1,1,1,1,0,0,0,0},
	{0,0,0,7,7,7,7,7,7,7,7,7,0,0,0},
	{0,0,1,1,1,1,1,1,1,1,1,1,1,0,0},
	{0,0,0,0,0,0,0,2,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,2,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,2,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,2,0,0,0,0,0,0,0}//15-y
};

class christmasTree
{
public:
	void Initialize();//黑夜
	//
	void drawTree();//圣诞树
	void drawTree2();//2tree
	void drawTree3();//3tree
	//
	void initSnow(int i);//雪花初始化
	void moveSnow(int i);//下雪
	void Snow(christmasTree CT);//飞雪
	//
	void yellowLight(int i, int j, int SZ);
	void redLight(int i, int j, int SZ);
	void Light(int i, int j, int SZ);//闪灯
	//
	void Surprise();//惊喜
};

void christmasTree::Initialize()
{
	initgraph(width, length);
	//setbkcolor(WHITE);
	//cleardevice();
	loadimage(&gift, "gift.jpg", 300, 300);
	putimage(150, 150, &gift);
	_getch();
	closegraph();
}

void christmasTree::yellowLight(int i, int j, int SZ)
{
	if (TREE[i][j] == 7)
	{
		int x = ((j + 0) * SZ + (j + 1) * SZ) / 2;
		int y = ((i + 0) * SZ + (i + 1) * SZ) / 2;
		int r = SZ / 4;
		setlinecolor(YELLOW);
		setfillcolor(YELLOW);
		fillcircle(x, y, r);//圆心公式(x1+x2)/2
		Sleep(1);
		setlinecolor(RGB(9, 163, 67));
		setfillcolor(RGB(9, 163, 67));
		fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
	}
}

void christmasTree::redLight(int i, int j, int SZ)
{
	if (TREE[i][j] == 1)
	{
		int x = ((j + 0) * SZ + (j + 1) * SZ) / 2;
		int y = ((i + 0) * SZ + (i + 1) * SZ) / 2;
		int r = SZ / 4;
		setlinecolor(RED);
		setfillcolor(RED);
		fillcircle(x, y, r);//圆心公式(x1+x2)/2
		Sleep(1);
		setlinecolor(GREEN);
		setfillcolor(GREEN);
		fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
	}
}

void christmasTree::Light(int i, int j, int SZ)
{
	yellowLight(i, j, SZ);
	redLight(i, j, SZ);
}

void christmasTree::drawTree()
{
	int i, j;
	setlinestyle(PS_SOLID, 36);
	int SZ = width / BOUNDS*1.25;
	for (int i = 0; i < BOUNDS; i++)
	{
		for (int j = 0; j < BOUNDS; j++)
		{
			if (TREE[i][j] == 1)
			{
				setlinecolor(GREEN);
				setfillcolor(GREEN);
				fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
				Sleep(1);
			}
			if (TREE[i][j] == 2)
			{
				setlinecolor(RGB(95, 41, 31));
				setfillcolor(RGB(95,41,31));
				fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
				Sleep(1);
			}
			if (TREE[i][j] == 7)
			{
				setlinecolor(RGB(9, 163, 67));
				setfillcolor(RGB(9, 163, 67));
				fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
				Sleep(1);
			}
			if(key == 3)
				Light(i, j, SZ);//闪灯
		}
	}
}
void christmasTree::drawTree2()
{
	int i, j;
	int SZ = width / BOUNDS * 1.25;
	for (int i = 0; i < BOUNDS; i++)
	{
		for (int j = 0; j < BOUNDS; j++)
		{
			if (TREE[i][j] == 1)
			{
				setlinecolor(GREEN);
				setfillcolor(GREEN);
				fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
			}
			if (TREE[i][j] == 2)
			{
				setlinecolor(RGB(95, 41, 31));
				setfillcolor(RGB(95, 41, 31));
				fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
			}
			if (TREE[i][j] == 7)
			{
				setlinecolor(RGB(9, 163, 67));
				setfillcolor(RGB(9, 163, 67));
				fillrectangle(j * SZ, i * SZ, (j + 1) * SZ, (i + 1) * SZ);
			}
			if (key == 3)
				Light(i, j, SZ);//闪灯
		}
	}
}

void christmasTree::initSnow(int i)
{
	snow[i].y = 0;
	snow[i].x = rand() % width;
	snow[i].step = (rand() % 5000) / 1000.0 + 1;
	snow[i].color = (int)(snow[i].step * 255 / 6.0 + 0.5);
	snow[i].color = RGB(snow[i].color, snow[i].color, snow[i].color);
}

void christmasTree::moveSnow(int i)//下雪
{
	setlinestyle(PS_SOLID, 36);
	putpixel((int)snow[i].x, snow[i].y, 0);//画雪
	snow[i].y += snow[i].step;
	if (snow[i].y > length) initSnow(i);//生成新雪花
	putpixel((int)snow[i].x, snow[i].y, snow[i].color);//再画雪
}

void christmasTree::Snow(christmasTree CT)
{
	srand((unsigned)time(NULL));
	for (int i = 0; i < MAXSNOW; i++)
	{
		CT.initSnow(i);
		snow[i].y = rand() % length;
	}

	while (!_kbhit())
	{
			drawTree2();//画树
		
		for (int i = 0; i < MAXSNOW; i++)
			CT.moveSnow(i);
		Sleep(20);
	}
}

int main()
{
	christmasTree CT;

	CT.Initialize();
	
	initgraph(width, length);

	mciSendString(_T("open 123.mp3 alias bkmusic"), NULL, 0, NULL);//播放音乐
	mciSendString(_T("play bkmusic repeat"), NULL, 0, NULL);//循环播放


	CT.drawTree();

	while (cin>>key)
	{
		switch (key)
		{
		case 2:
			CT.Snow(CT);
			break;
		case 3:
			CT.Snow(CT);
			break;
		case 4:
			return 0;
		}
	}
	return 0;
}

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

原文地址: http://outofmemory.cn/zaji/5670209.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存