easyx图形库制作新年烟花(附图片资源)

目录

先看效果:

代码:


先看效果:

map

                看吧是不是非常好看,哈哈,接下来就直接上代码!

这个是图形库的坐标图

代码:

#include<stdio.h>
#include<graphics.h>
#include<Windows.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<string.h>
#include<math.h>
#include<mmsystem.h>
//对于静态库我是在项目属性里面导入了
//#pragma commoment(lib,"winmm.lib")

#define fire 8
#define pi 3.1415926
struct jet
{
	int x, y;
	int h;
	int step;
	bool show;
	IMAGE kk[2];
	byte n : 1;//定义一个byte类型,其字段为1,意思是这个类型只有两种要么0要么1,这里可以去实现上面两个图片的交替,实现烟花弹闪来闪去
}j[fire];//定义烟花弹的结构体与数量

struct f
{
	int x, y;
	int cenx, ceny;
	int r;
	int maxr;
	int w, h;
	DWORD pixel[240][240];
	bool show;
	bool flower;
}h[fire];定义烟花的结构体与数量

void firedata(int i)//初始化烟花的结构体内容
{
	h[i].cenx = 120;//中心点横坐标
	h[i].ceny = 120;//中心点纵坐标
	h[i].maxr = 120;//最大烟花半径
	h[i].r = 0;//初始化半径为0
	h[i].w = 240;//烟花图片的宽度
	h[i].h = 240;//烟花图片的高度
	h[i].show = false;
	h[i].flower = false;//布尔变量
}
void load()
{

	IMAGE jk,t;
	loadimage(&jk, "烟花.jpg", 3120, 240);//加载烟花图片
	for (int c = 0; c < fire; c++)
	{
		firedata(c);//每一个烟花初始化
		SetWorkingImage(&jk);//对图片进行操作
		getimage(&t, c * 240, 0, 240, 240);//获取要操作的图片到t中
		SetWorkingImage(&t);//对图片t进行操作
		for (int i = 0; i < 240;i++)
		{
			for (int j = 0; j < 240; j++)
			{
				h[c].pixel[i][j] = getpixel(i, j);//获取图片t的像素点到烟花上面
			}
		}
	}	
	IMAGE jj;
	loadimage(&jj, "all.jpg", 200, 50);
	SetWorkingImage(&jj);
	for (int i = 0; i < fire; i++)
	{
		int ra = rand() % 5;
		getimage(&j[i].kk[0], ra*20, 0, 20, 50);//获取烟花弹的图片 暗
		getimage(&j[i].kk[1], ra*20+ 100, 0, 20, 50);//获取烟花弹图片 亮
		j[i].show = false;
	}
	SetWorkingImage();//把操作界面转换到主界面去
}
void creat()//创造烟花弹
{
	int i = rand() % fire;
	if (j[i].show == false)//要进行判断,如果烟花展示为false那么就创造一个
	{
		j[i].x = rand() % (getwidth()-20);
		j[i].y = -rand() % 50+getheight();
		j[i].h = rand() %(int) (getheight() / 2);
		j[i].step = 4 + rand() % 3;
		j[i].show = true;//令其布尔变量为true
	}
}
void launch()//发射烟花弹
{
	for (int i = 0; i < fire; i++)
	{
		if (j[i].show)
		{
			putimage(j[i].x, j[i].y, &j[i].kk[j[i].n],SRCINVERT);			
			if (j[i].y > j[i].h)//如果烟花弹的纵坐标大于最大高度就进行烟花弹上升
			{
				j[i].y -=j[i].step;
				j[i].n++;
			}
			putimage(j[i].x, j[i].y, &j[i].kk[j[i].n], SRCINVERT);

			if (j[i].y <= j[i].h)
			{
				j[i].show = false;//如果烟花弹到达最高点,那么就false
				setfillcolor(BLACK);
			solidrectangle(j[i].x, j[i].y, j[i].x+20, j[i].y +44+j[i].step );
			h[i].x = j[i].x;
			h[i].y = j[i].y;
			h[i].show = true;//此时开始绘制烟花
			}		
		}
	}
}
void bloom(DWORD *p)//画烟花绽放
{
	for (int i = 0; i < fire; i++)
	{
		if (h[i].show == true)
		{
			if (h[i].r < h[i].maxr) {
				h[i].r++;//每次半径+1
				h[i].flower = true;//
			}
			if (h[i].r >= h[i].maxr)
			{
				firedata(i);//如果半径超过最大半径就把这个烟花回归到初始化,回到奇点
			}
		}
		if (h[i].flower == true)
		{
			for (double a = 0; a <= 2 * pi; a+=0.01)//画一个圆模板的像素点
			{
				int x = h[i].cenx + h[i].r * cos(a);
				int y= h[i].ceny + h[i].r * sin(a);
				if (x > 0 && x < h[i].w &&y>0 && y < h[i].h)//判断范围
				{
					int wx= h[i].x +  h[i].r * cos(a);//获取此时的界面坐标
					int wy= h[i].y  + h[i].r * sin(a);
					if (wx > 0 && wx < getwidth() && wy < getheight() && wy>0)
					{
						p[wy * getwidth() + wx] = BGR(h[i].pixel[x][y]);//以此界面坐标画出像素点
					}
				}
			}
		}

	}
}
void backgroundsound()//背景音乐
{
	mciSendString("open mm.mp3 alias music", 0, 0, 0);
	mciSendString("play music", 0, 0, 0);
}
void welcome()//开始的欢迎
{
	int x, y;
	int i = 0;
	while (i < 50) {
		BeginBatchDraw();
		x = 300 + 100*(int)cos(i*2);
		y = 300 +100*(int) sin(i*2);
		cleardevice();
		settextcolor(YELLOW);
		settextstyle(i, 0, "华文行楷");
		setbkmode(TRANSPARENT);	
		outtextxy(x, y, "新年快乐");
		i++;
		Sleep(50);
		FlushBatchDraw();
	}
	EndBatchDraw();
	Sleep(2 * 1000);
	cleardevice();
	backgroundsound();//导入背景音乐
}
int main()
{
	initgraph(1500, 700);
	srand((unsigned)time(0)+clock());//设置随机数
	welcome(); 
	load();
	DWORD* p=GetImageBuffer();//获取窗口的内存指针
	while (1)
	{
		
		for (int i = 0; i < getwidth()/5*4; i++)//这个是用来消除像素点的,就是擦去像素点变回黑色,这可以使得烟花慢慢消失
		{
			for (int k = 0; k < 6; k++) {
				int x = rand() % getwidth();
				int y = rand() % getheight();
				if (y < getheight())
				{
					p[y * getwidth() + x] =BLACK;//设置像素点为黑色
				}
			}
		}
		creat();
		launch();
		bloom(p);
		Sleep(12);//停机12毫秒,这里可以去用时间获取函数来代替,效果会比较好
	}
	system("pause");
}

图片资源: 

 

祝大家新年快乐!!!

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fitz&

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值