烟花代码C语言

2024-01-01 17:18:22

元旦已到,所以想自己用c写份C语言实现的烟花代码,也是花了好几个小时,现在终于完工了。

我的的代码要用到easyx图形库和vs编译环境,ok,那么话不多说正文开始

视频效果我也上传了

一.

材料:我们烟花离不开上升时的烟花弹和最后的爆炸产生的烟花,我把自己用的,放这里了。

这些如果觉得不符合审美的话也可以在网上自己找找,

二环境搭建

这里要用到的是vs和easyx图形库,没有的可以在b站搜一下下载方法,这里不再赘述

三,代码实现原理

简单来说就三点;

1.控制烟花弹上升

2.控制烟花逐渐展开

3.最后清屏,加上一张图片和一些话

4.别忘了音乐!!!!

四:代码实现

头文件:

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#include<easyx.h>
#include<conio.h>
#include<mmsystem.h>
#include<time.h>
#include<malloc.h>
#include<math.h>
//#pragma comment(lib,"winmm.lib")
#include<stdlib.h>
#define a 3
#include<stdio.h>
#include"标头.h"
//烟花弹
typedef struct jet
{
	int x, y;//坐标
	int hx, hy;//最高点坐标
	unsigned long t1, t2, dt;
	IMAGE img;
	bool isfly;//上升状态?
	int b, c;
}jet;
typedef struct fire
{
	int r;//当前半径
	int maxr;//
	int x, y;
	int cx, cy;
	COLORREF xy[240][240];
	unsigned t1, t2, dt;
	//bool isboom;
	bool isshow;
	IMAGE img;
	int d1, d2;
}fire;
typedef struct flower
{
	jet jets;
	fire fires;
}flower;
int i = 1;
void initjet(jet* jets)
{
	char arr[10] = "jet1.jpg";
	sprintf(arr, "jet%d.jpg", i);
	//jet*jets = jes + i;
	jets->x = rand() % (800 - 120)+60;
	jets->y = 750;
	jets->hx = jets->x;
	jets->hy = rand() % 400;
	jets->b = GetTickCount();//获取时间
	//jets->dt = 10;
	jets->t1 = GetTickCount();
	jets->isfly = true;
	IMAGE image;
	if (i > 7)
		i -= 7;
	//jets->img = image;
	switch (i)
	{
	case 1:
		
		loadimage(&jets->img,"jet1.jpg" , 20, 50);
		break;
	case 2:
		loadimage(&jets->img, "jet2.jpg", 20, 50);
		break;
	case 3:
		loadimage(&jets->img, "jet3.jpg", 20, 50);
		break;
	case 4:
		loadimage(&jets->img, "jet4.jpg", 20, 50);
		break;
	case 5:
		loadimage(&jets->img, "jet5.jpg", 20, 50);
		break;
	case 6:
		loadimage(&jets->img, "jet6.jpg", 20, 50);
		break;
	case 7:
		loadimage(&jets->img, "jet7.jpg", 20, 50);
		break;
	}
	i++;
}
void initfire(fire* fires)
{
	fires->r = 10;
	fires->maxr = 110;
	fires->isshow = false;
	fires->t1 = GetTickCount();
	if (i > 7)
		i -= 7;
	switch (i)
	{
	case 1:
		loadimage(&fires->img, "fire1.jpg", 240, 240);
		SetWorkingImage(&fires->img);
		for (int i = 0; i < 240; i++)
			for (int j = 0; j < 240; j++)
				fires->xy[i][j] = getpixel(i, j);
		SetWorkingImage();
		break;
	case 2:
		loadimage(&fires->img, "fire2.jpg", 240, 240);
		SetWorkingImage(&fires->img);
		for (int i = 0; i < 240; i++)
			for (int j = 0; j < 240; j++)
				fires->xy[i][j] = getpixel(i, j);
		SetWorkingImage();
		break;
	case 3:
		loadimage(&fires->img, "fire3.jpg", 240, 240);
		SetWorkingImage(&fires->img);
		for (int i = 0; i < 240; i++)
			for (int j = 0; j < 240; j++)
				fires->xy[i][j] = getpixel(i, j);
		SetWorkingImage();
		break;
	case 4:
		loadimage(&fires->img, "fire4.jpg", 240, 240);
		SetWorkingImage(&fires->img);
		for (int i = 0; i < 240; i++)
			for (int j = 0; j < 240; j++)
				fires->xy[i][j] = getpixel(i, j);
		SetWorkingImage();
		break;
	case 5:
		loadimage(&fires->img, "fire5.jpg", 240, 240);
		SetWorkingImage(&fires->img);
		for (int i = 0; i < 240; i++)
			for (int j = 0; j < 240; j++)
				fires->xy[i][j] = getpixel(i, j);
		SetWorkingImage();
		break;
	case 6:
		loadimage(&fires->img, "fire6.jpg", 240, 240);
		SetWorkingImage(&fires->img);
		for (int i = 0; i < 240; i++)
			for (int j = 0; j < 240; j++)
				fires->xy[i][j] = getpixel(i, j);
		SetWorkingImage();
		break;
	case 7:
		loadimage(&fires->img, "fire7.jpg", 240, 240);
		SetWorkingImage(&fires->img);
		for (int i = 0; i < 240; i++)
			for (int j = 0; j < 240; j++)
				fires->xy[i][j] = getpixel(i, j);
		SetWorkingImage();
		break;
	}
	i++;
}
void jetmove(jet*jets,fire *fires)
{
	jets->c = GetTickCount();
	if (jets->isfly == true&&jets->c>jets->b+10)
	{
		putimage(jets->x, jets->y, &jets->img);
		jets->b = jets->c;
		if (jets->y - 5 >= jets->hy)
			jets->y -= 5;
		else
			jets->isfly = false;
	}
	if (jets->isfly == false)
	{
		fires->cx = jets->x;
		fires->cy = jets->y;
		cleardevice();
		fires->isshow = true;
	}
	if (jets->c < jets->b + 10)
		Sleep(1);
}
void firemove(jet* jets, fire* fires)
{
	fires->d2 = GetTickCount();
	if (fires->r <= fires->maxr && fires->isshow&&fires->d2>fires->d1+10)
	{
		fires->d1 = fires->d2;
		//画烟花
		//BeginBatchDraw();
		for (int i = -fires->r; i <= fires->r; i++)
			for (int j = -fires->r; j <= fires->r; j++)
				if (i * i + j * j <= fires->r * fires->r)
				{
					putpixel(i + fires->cx, j + fires->cy, fires->xy[i + 120][j + 120]);
				}
		fires->r += 10;
	}
	 if(fires->r>fires->maxr)
	{
		fires->isshow = false;
		jets->isfly = true;
		initjet(jets);
		initfire(fires);
	}
	if (fires->d2 < fires->d1 + 10)
		Sleep(1);
}

int main()
{
	timeBeginPeriod(1);
	initgraph(800, 500);//创建窗口
	mciSendString("open 恋爱告急.mp3", NULL, 0, NULL);//放歌
	mciSendString("play 恋爱告急.mp3", NULL, 0, NULL);//放歌
	srand((unsigned)time(NULL));//随机种子
	//initgraph(wideth, height);//创建画布
	fire fires;
	jet jets;
	//初始化烟花弹
	srand((unsigned)time(NULL));  
	flower flowers[a];
	//flower flowers1[a];
	//flower *flowers= (flower*)malloc(a * sizeof(flower));
	if (flowers == NULL)
		return 0;
	int i;
	for ( i = 0; i < a; i++)
	{
		initjet(&flowers[i].jets);
		//initjet(&flowers1[i].jets);
		//loadimage(&flowers[i].jets.img, "jet1.jpg", 20, 50);
		initfire(&flowers[i].fires);
		//initfire(&flowers1[i].fires);
	}
	BeginBatchDraw();
	int n = 30;
	while (!_kbhit())
	{
		int i;
		cleardevice();
		//Sleep(10);
		settextstyle(25, 0, "楷体");//字体风格
		for (i = 0; i < a; i++)
		{
			if (flowers[i].jets.isfly)
			{
				jetmove(&flowers[i].jets, &flowers[i].fires);
				
			}
			else
			{
				//Sleep(10);
				firemove(&flowers[i].jets, &flowers[i].fires);
				
			}
		}
		FlushBatchDraw();
	}
	IMAGE img;
	loadimage(&img, "宵宫.png", 800, 600, 0);
	putimage(0,0,&img);
	setbkmode(TRANSPARENT);
	settextcolor(BLUE);
	outtextxy(300, 200, "烟花易逝,人情长存");
	outtextxy(300, 170, "2024,去啦!");
	EndBatchDraw();
	_getch();
	settextcolor(YELLOW);//字体颜色
	
	_getch();
	return 0;
}

以上就是全部代码啦,感觉有用的话,就点个赞支持一下吧

文章来源:https://blog.csdn.net/2301_79551553/article/details/135320985
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。