当前是想做一个烟花绽放的效果,可惜实力实在是太菜 . . .
程序中有两个有意思的算法,一个是刚开始 文字出来的效果,还有一个是 爱心的形成算法,如果整个程序的代码不会写没有关系,但是这两个算法希望大家可以学习一下 . . .
效果如下所示:
爱心烟花表白
当文字出现之后,我们按下回车,就有后来的烟花效果了
代码如下所示:
#include <iostream>
#include <graphics.h>
#include <ctime>
#include <cstdlib>
#include <conio.h>
#include <mmsystem.h>
#pragma comment (lib,"winmm")
const int WIDTH = 960;
const int HEIGHT = 720;
const double PI = 3.1415926;
const int NUM = 10;
void Init(); // 初始化一些东西
class Fire
{
private:
struct Property //烟花的属性
{
int max_x; //最高点
int max_y;
int x; //当前位置
int y;
bool really; //是否可以发射
bool max_really;
int radius = 15;
int k = 0;
};
struct Fz
{
int x;
int y;
};
Fz fz[NUM] = { 0 };
Property arr[NUM]; //默认为10个炮弹
public:
Fire();
~Fire() {}
void CreateFire(int n); //恢复属性
void PlayFire(); //发射烟花
bool JudgePlay(int n); //判断发射
void Change(int n) { arr[n].really = false; }
void Fire_666(); //烟花绽放
};
int main()
{
srand((unsigned)time(nullptr));
Fire* fire = new Fire();
DWORD t1 = timeGetTime(), tt1, t2 = timeGetTime(), tt2;
DWORD t3 = timeGetTime(), tt3;
Init();
int flag;
BeginBatchDraw();
while (!kbhit())
{
tt1 = timeGetTime();
if (tt1 - t1 > 500)
{
flag = rand() % 10 + 1;
if (fire->JudgePlay(flag) == true)
fire->Change(flag);
t1 = tt1;
}
tt2 = timeGetTime();
if (tt2 - t2 > 20)
{
fire->PlayFire();
t2 = tt2;
}
tt3 = timeGetTime();
if (tt3 - t3 > 50)
{
fire->Fire_666();
t3 = tt3;
}
for (int i = 0; i < 3000; i++) //擦点
{
int a = rand() % WIDTH;
int b = rand() % HEIGHT;
putpixel(a, b, BLACK);
}
EndBatchDraw();
}
delete fire;
return 0;
}
void Init()
{
initgraph(WIDTH, HEIGHT);
mciSendString("open bj.mp3", 0, 0, 0);
mciSendString("play bj.mp3 repeat", 0, 0, 0);
int redius = 300;
int coordx = WIDTH / 2 + 250, coordy = HEIGHT / 2 + 50;
int num = 60;
int n = 1;
settextcolor(YELLOW);
for (int i = 0; i < num / 2; i++)
{
double hudu = (PI / 180 * i * (360 / num));
int x = (int)(coordx + cos(hudu) * redius);
int y = (int)(coordy - sin(hudu) * redius);
cleardevice();
settextstyle(20 + n, n, "楷体");
n += 1;
outtextxy(x - 210, y + 50, "用我三生烟火");
outtextxy(x - 70, y + 150, "换你一世迷离");
Sleep(50);
}
settextcolor(RED);
outtextxy(WIDTH / 2 - 200, HEIGHT / 2 - 150, "作者: 花 梦");
std::cin.get();
}
Fire::Fire()
{
for (int i = 0; i < NUM; i++) //初始化每个炮弹的属性
{
arr[i].max_x = rand() % (WIDTH - 100) + 50;
arr[i].max_y = rand() % 200 + 50;
arr[i].x = arr[i].max_x;
arr[i].y = rand() % 100 + 720;
arr[i].really = true;
}
}
void Fire::CreateFire(int n)
{
arr[n].max_x = rand() % (WIDTH - 100) + 50;
arr[n].max_y = rand() % 200 + 50;
arr[n].x = arr[n].max_x;
arr[n].y = rand() % 100 + 720;
arr[n].really = true;
arr[n].radius = 15;
arr[n].k = 0;
}
bool Fire::JudgePlay(int n)
{
if (arr[n].really == true)
return true;
else
return false;
}
void Fire::PlayFire()
{
for (int i = 0; i < NUM; i++)
{
setfillcolor(RGB(rand() % 256, rand() % 256, rand() % 256));
if (JudgePlay(i) == false)
{
if (arr[i].y > arr[i].max_y)
{
clearellipse(arr[i].x - 5, arr[i].y - 10, arr[i].x + 5, arr[i].y + 10);
arr[i].y -= 5;
solidellipse(arr[i].x - 5, arr[i].y - 10, arr[i].x + 5, arr[i].y + 10);
putpixel(arr[i].x, arr[i].y + 10, getfillcolor());
putpixel(arr[i].x - 1, arr[i].y + 10, getfillcolor());
putpixel(arr[i].x + 1, arr[i].y + 10, getfillcolor());
}
else
{
clearellipse(arr[i].x - 5, arr[i].y - 10, arr[i].x + 5, arr[i].y + 10);
arr[i].max_really = true;
fz[i].x = arr[i].max_x;
fz[i].y = arr[i].max_y;
}
}
}
}
void Fire::Fire_666()
{
int num = 60;
for (int n = 0; n < NUM; n++)
{
if (arr[n].max_really == true)
{
setfillcolor(RGB(rand() % 256, rand() % 256, rand() % 256));
for (int i = 0; i < num; i++)
{
double radian = (PI / 180) * (360 / num) * i;
int x = (int)(arr[n].x + cos(radian) * arr[n].radius);
int y = (int)(arr[n].y - sin(radian) * arr[n].radius);
for (int j = 0; j < arr[n].radius; j++)
{
putpixel(x - j, y - j, getfillcolor());
putpixel(x + j, y - j, getfillcolor());
}
}
arr[n].k++;
arr[n].radius += 15;
if (arr[n].k == 3)
{
arr[n].max_really = false;
CreateFire(n);
}
}
}
}