欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

250行代码基于C + EasyX实现感人的表白小程序,画出美丽的爱心雨!

程序员文章站 2022-03-06 14:30:27
...

这个小程序是在 18年的夏天写的,曾经有一份真挚的爱情摆在我面前,我没有好好珍惜,算了,还是赶紧 new一个新对象吧 ~

程序中的爱心是我用 api画出来的,也是比较麻烦的一种方法,如果使用图片的话会快很多,话不多来,现在看看效果是怎么样的 . . .

刚开始的效果是下面这样的:
250行代码基于C + EasyX实现感人的表白小程序,画出美丽的爱心雨!
如果点否的话会一直循环下面这种类型的字:
250行代码基于C + EasyX实现感人的表白小程序,画出美丽的爱心雨!
点是的话,会慢慢形成下面的界面:
250行代码基于C + EasyX实现感人的表白小程序,画出美丽的爱心雨!
完整的效果如下所示:

爱心表白小程序


代码如下所示:

//愿所有有情人终成眷属

#include <graphics.h>
#include <time.h>
#include <mmsystem.h>//播放音乐的头文件
#pragma comment(lib,"winmm.lib")//播放音乐的库文件

#define LOVE_NUM 30

char sz_1[][24] = { "我一直以为山是水的故事",
"云是风的故事",
"你是我的故事",
"可是却不知道",
"我是不是你的故事" };

char sz_2[][32] = { "如果可以和你在一起",
"我宁愿让天空所有的星光全部损落",
"因为你的眼睛",
"是我生命里最亮的光芒" };

typedef struct LOVE
{
	int x;
	int y;
}love;

love sz_love[LOVE_NUM];
POINT ILoveYou[LOVE_NUM][26];

void Init(); //初始化   音乐 背景颜色
void DrawHouse();//画出房子
void DrawHouse1();//更新房子
void InitLove();//初始化爱心
void DrawLove(); //画出爱心雨
void SayToYou(); //要说的话
void SayToYou1();//更新要说的话
void BoxMessage();//消息盒子

int main()
{
	ShowWindow(FindWindow("ConsoleWindowClass", 0), 0);		//隐藏黑窗口

	BoxMessage(); //消息盒子

	Init();		     //初始化
	InitLove();
	DrawHouse();	//画出房子
	SayToYou();     //要说的话

	BeginBatchDraw();
	while (1)
	{
		cleardevice();	//刷新屏幕
		DrawHouse1();
		SayToYou1();
		DrawLove();		//画出爱心
		Sleep(5);

		FlushBatchDraw();
	}

}

//初始化   音乐 背景颜色
void Init()
{
	srand((unsigned)time(0));
	initgraph(840, 680);
	//播放背景音乐
	mciSendString("open 1.mp3 alias cong", 0, 0, 0);
	mciSendString("play cong repeat", 0, 0, 0);
	setbkcolor(RGB(255, 174, 201));
	cleardevice();
}

//画出房子
void DrawHouse()
{
	Sleep(520);
	setfillcolor(RGB(185, 122, 87));
	setlinecolor(RGB(50, 50, 50));
	fillrectangle(100, 350, 300, 550);
	POINT a[] = { { 130, 280 }, { 270, 280 }, { 350, 350 }, { 50, 350 } };
	Sleep(520);
	setfillcolor(LIGHTGRAY);

	fillpolygon(a, 4);
	setfillcolor(LIGHTGRAY);
	Sleep(520);
	fillrectangle(220, 470, 270, 550);
	setlinestyle(PS_DASHDOTDOT, 2, NULL, NULL);
	Sleep(520);
	line(220, 550, 245, 528);
	setlinestyle(PS_SOLID, 3, NULL, NULL);
	Sleep(520);
	line(100, 450, 200, 450);
	POINT b[] = { { 153, 401 }, { 157, 397 }, { 164, 392 }, { 171, 391 }, { 178, 393 }, { 184, 398 }, { 186, 403 }, { 187, 408 }, { 186, 414 }, { 183, 420 }, { 179, 426 }, { 172, 433 }, { 164, 439 }, { 158, 442 }, { 153, 445 },
	{ 143, 439 }, { 134, 433 }, { 127, 426 }, { 123, 421 }, { 120, 414 }, { 118, 408 }, { 120, 402 }, { 122, 397 }, { 126, 393 }, { 135, 390 }, { 145, 393 } };
	setfillcolor(RGB(255, 174, 201));
	Sleep(520);
	solidpolygon(b, 26);
	Sleep(1314);
}

//用来更新
void DrawHouse1()
{
	setfillcolor(RGB(185, 122, 87));
	setlinecolor(RGB(50, 50, 50));
	fillrectangle(100, 350, 300, 550);
	POINT a[] = { { 130, 280 }, { 270, 280 }, { 350, 350 }, { 50, 350 } };
	setfillcolor(LIGHTGRAY);
	fillpolygon(a, 4);
	setfillcolor(LIGHTGRAY);
	fillrectangle(220, 470, 270, 550);
	setlinestyle(PS_DASHDOTDOT, 2, NULL, NULL);
	line(220, 550, 245, 528);
	setlinestyle(PS_SOLID, 3, NULL, NULL);
	line(100, 450, 200, 450);
	POINT b[] = { { 153, 401 }, { 157, 397 }, { 164, 392 }, { 171, 391 }, { 178, 393 }, { 184, 398 }, { 186, 403 }, { 187, 408 }, { 186, 414 }, { 183, 420 }, { 179, 426 }, { 172, 433 }, { 164, 439 }, { 158, 442 }, { 153, 445 },
	{ 143, 439 }, { 134, 433 }, { 127, 426 }, { 123, 421 }, { 120, 414 }, { 118, 408 }, { 120, 402 }, { 122, 397 }, { 126, 393 }, { 135, 390 }, { 145, 393 } };
	setfillcolor(RGB(255, 174, 201));
	solidpolygon(b, 26);
}

//画出爱心雨
void DrawLove()
{
	for (int i = 0; i < LOVE_NUM; i++)
	{
		sz_love[i].y += 2;
		if (sz_love[i].y > 700)
		{
			sz_love[i].x = rand() % 840;
			sz_love[i].y = -20;

		}
		ILoveYou[i][0] = { sz_love[i].x, sz_love[i].y - 13 };
		ILoveYou[i][1] = { sz_love[i].x + 4, sz_love[i].y - 17 };
		ILoveYou[i][2] = { sz_love[i].x + 11, sz_love[i].y - 22 };
		ILoveYou[i][3] = { sz_love[i].x + 18, sz_love[i].y - 23 };
		ILoveYou[i][4] = { sz_love[i].x + 25, sz_love[i].y - 21 };
		ILoveYou[i][5] = { sz_love[i].x + 31, sz_love[i].y - 16 };
		ILoveYou[i][6] = { sz_love[i].x + 33, sz_love[i].y - 11 };
		ILoveYou[i][7] = { sz_love[i].x + 34, sz_love[i].y - 6 };
		ILoveYou[i][8] = { sz_love[i].x + 33, sz_love[i].y };
		ILoveYou[i][9] = { sz_love[i].x + 30, sz_love[i].y + 6 };
		ILoveYou[i][10] = { sz_love[i].x + 26, sz_love[i].y + 12 };
		ILoveYou[i][11] = { sz_love[i].x + 19, sz_love[i].y + 19 };
		ILoveYou[i][12] = { sz_love[i].x + 11, sz_love[i].y + 25 };
		ILoveYou[i][13] = { sz_love[i].x + 5, sz_love[i].y + 28 };
		ILoveYou[i][14] = { sz_love[i].x, sz_love[i].y + 31 };
		ILoveYou[i][15] = { sz_love[i].x - 5, sz_love[i].y + 28 };
		ILoveYou[i][16] = { sz_love[i].x - 11, sz_love[i].y + 25 };
		ILoveYou[i][17] = { sz_love[i].x - 19, sz_love[i].y + 19 };
		ILoveYou[i][18] = { sz_love[i].x - 26, sz_love[i].y + 12 };
		ILoveYou[i][19] = { sz_love[i].x - 30, sz_love[i].y + 6 };
		ILoveYou[i][20] = { sz_love[i].x - 33, sz_love[i].y };
		ILoveYou[i][21] = { sz_love[i].x - 34, sz_love[i].y - 6 };
		ILoveYou[i][22] = { sz_love[i].x - 33, sz_love[i].y - 11 };
		ILoveYou[i][23] = { sz_love[i].x - 31, sz_love[i].y - 16 };
		ILoveYou[i][24] = { sz_love[i].x - 25, sz_love[i].y - 21 };
		ILoveYou[i][25] = { sz_love[i].x - 18, sz_love[i].y - 23 };
	}
	setfillcolor(RED);
	for (int i = 0; i < LOVE_NUM; i++)
	{
		solidpolygon(ILoveYou[i], 26);
	}

}

//初始化爱心
void InitLove()
{
	for (int i = 0; i < LOVE_NUM; i++)
	{
		sz_love[i].x = rand() % 840;
		sz_love[i].y = rand() % 680;

		ILoveYou[i][0] = { sz_love[i].x, sz_love[i].y - 13 };
		ILoveYou[i][1] = { sz_love[i].x + 4, sz_love[i].y - 17 };
		ILoveYou[i][2] = { sz_love[i].x + 11, sz_love[i].y - 22 };
		ILoveYou[i][3] = { sz_love[i].x + 18, sz_love[i].y - 23 };
		ILoveYou[i][4] = { sz_love[i].x + 25, sz_love[i].y - 21 };
		ILoveYou[i][5] = { sz_love[i].x + 31, sz_love[i].y - 16 };
		ILoveYou[i][6] = { sz_love[i].x + 33, sz_love[i].y - 11 };
		ILoveYou[i][7] = { sz_love[i].x + 34, sz_love[i].y - 6 };
		ILoveYou[i][8] = { sz_love[i].x + 33, sz_love[i].y };
		ILoveYou[i][9] = { sz_love[i].x + 30, sz_love[i].y + 6 };
		ILoveYou[i][10] = { sz_love[i].x + 26, sz_love[i].y + 12 };
		ILoveYou[i][11] = { sz_love[i].x + 19, sz_love[i].y + 19 };
		ILoveYou[i][12] = { sz_love[i].x + 11, sz_love[i].y + 25 };
		ILoveYou[i][13] = { sz_love[i].x + 5, sz_love[i].y + 28 };
		ILoveYou[i][14] = { sz_love[i].x, sz_love[i].y + 31 };
		ILoveYou[i][15] = { sz_love[i].x - 5, sz_love[i].y + 28 };
		ILoveYou[i][16] = { sz_love[i].x - 11, sz_love[i].y + 25 };
		ILoveYou[i][17] = { sz_love[i].x - 19, sz_love[i].y + 19 };
		ILoveYou[i][18] = { sz_love[i].x - 26, sz_love[i].y + 12 };
		ILoveYou[i][19] = { sz_love[i].x - 30, sz_love[i].y + 6 };
		ILoveYou[i][20] = { sz_love[i].x - 33, sz_love[i].y };
		ILoveYou[i][21] = { sz_love[i].x - 34, sz_love[i].y - 6 };
		ILoveYou[i][22] = { sz_love[i].x - 33, sz_love[i].y - 11 };
		ILoveYou[i][23] = { sz_love[i].x - 31, sz_love[i].y - 16 };
		ILoveYou[i][24] = { sz_love[i].x - 25, sz_love[i].y - 21 };
		ILoveYou[i][25] = { sz_love[i].x - 18, sz_love[i].y - 23 };
	}
}

//要说的话
void SayToYou()
{
	int x = 450, y = 100;

	settextstyle(20, 10, "楷体");
	settextcolor(RGB(255, 100, 0));
	for (int i = 0; i < 5; i++)
	{
		outtextxy(x, y, sz_1[i]);
		y += 25;
		Sleep(1314);
	}

	y += 20;
	for (int i = 0; i < 4; i++)
	{
		outtextxy(x, y, sz_2[i]);
		y += 25;
		Sleep(1314);
	}
	Sleep(1314);	//1314个毫秒后,下起爱心雨
}

//更新要说的话
void SayToYou1()
{
	int x = 450, y = 100;
	for (int i = 0; i < 5; i++)
	{
		outtextxy(x, y, sz_1[i]);
		y += 25;
	}

	y += 25;
	for (int i = 0; i < 4; i++)
	{
		outtextxy(x, y, sz_2[i]);
		y += 25;
	}
}

//消息盒子
void BoxMessage()
{
	while (MessageBox(NULL, "小姐姐我喜欢你", "愿意做我女朋友吗", MB_YESNO) != IDYES)
	{
		if (MessageBox(NULL, "为你做饭吃呀", "愿意做我女朋友吗", MB_YESNO) == IDYES) break;
		if (MessageBox(NULL, "为你洗衣服呀", "愿意做我女朋友吗", MB_YESNO) == IDYES) break;
		if (MessageBox(NULL, "钱归你管呀", "愿意做我女朋友吗", MB_YESNO) == IDYES) break;
		if (MessageBox(NULL, "你最大呀", "愿意做我女朋友吗", MB_YESNO) == IDYES) break;
	}

	if (MessageBox(NULL, "为亲爱的你,献上一首歌,想听吗", "我最喜欢你了 ^_^ 嘻嘻", MB_YESNO) != IDYES)
	{
		exit(0);
	}
}