#include <graphics.h>
#include <math.h>
#include <stdlib.h>
#include <ctime>
#include <conio.h>
#pragma comment(lib,"winmm.lib")
#define PI 3.1415926548
#define NUM 13 //整个屏幕中只能最多出现13个烟花
//烟花弹的结构体
struct Jet
{
int x, y; //当前坐标
int hx, hy; //最高点坐标
int height; //上升的高度 y1-y2
bool shoot; //是否发射
DWORD t1, t2, dt; //记录时间 速度
IMAGE img[2]; //烟花弹的图片
byte n : 1; //位段 //n 只占用1个位 n++
}jet[NUM];
//烟花的结构体
struct Fire
{
int r; //半径
int x, y;
int max_r; //最大半径
int cen_x, cen_y; //中心的坐标
int width, height; //宽度 高度
int xy[240][240]; //像素矩阵
bool show; //是否显示
bool draw; //是否绘制
DWORD t1, t2, dt; //绽放速度
}fire[NUM];
void Welcome()
{
//初始化界面
initgraph(1200, 800); //1200 800
srand((unsigned int)time(NULL));
IMAGE img1; //定义一张图片
//加载图片
loadimage(&img1, L"./res/timg.jpg", 1200, 800);
mciSendString(L"open ./res/Snowdream.mp3 alias BGM", 0, 0, 0);
mciSendString(L"play BGM", 0, 0, 0);
for (int i = 0; i < 50; i++) //50
{
//圆的轨迹方程。
int x = 600 + int(180 * sin(PI * 2 * i / 60));
int y = 200 + int(180 * cos(PI * 2 * i / 60));
cleardevice();
settextcolor(YELLOW);
settextstyle(i, 0, L"华文行楷");
outtextxy(x - 80, y, L"原来你是我最想留住的幸运");
outtextxy(x - 10, y + 100, L"原来我们和爱情曾经靠得那么近");
Sleep(25);
}
Sleep(8000);
putimage(0, 0, &img1);
settextstyle(25, 0, L"隶书");
settextcolor(RED);
setbkmode(0);
outtextxy(800, 100, L"曾经爱你");
outtextxy(800, 150, L"是真的");
outtextxy(800, 200, L"依然爱你");
outtextxy(800, 250, L"也是真的");
outtextxy(800, 300, L"让我一生");
outtextxy(800, 350, L"为你着迷");
outtextxy(1000, 400, L"——Tsing Zhou");
Sleep(10000);
}
//程序的初始化函数
void Init(int i)
{
int r[13] = { 120, 120, 155, 123, 130, 147, 138, 138, 130, 135, 140, 132, 155 };
int x[13] = { 120, 120, 110, 117, 110, 93, 102, 102, 110, 105, 100, 108, 110 };
int y[13] = { 120, 120, 85, 118, 120, 103, 105, 110, 110, 120, 120, 105, 85 };
fire[i].width = 240;
fire[i].height = 240;
fire[i].max_r = r[i];
fire[i].cen_x = x[i];
fire[i].cen_y = y[i];
fire[i].show = false;
fire[i].dt = 5;
fire[i].t1 = timeGetTime();
fire[i].r = 0;
//初始化烟花弹
jet[i].t1 = timeGetTime(); //系统时间
jet[i].n = 0; //暗色
jet[i].shoot = false;
jet[i].dt = rand() % 10; //每一个烟花弹的速度相同还是不同 0-9
jet[i].height = 0;
}
//加载图片
void Loadjpg()
{
IMAGE fm, gm;
loadimage(&fm, L"./res/flower.jpg", 3120, 240);
for (int i = 0; i < 13; i++)
{
SetWorkingImage(&fm);
getimage(&gm, i * 240, 0, 240, 240);
SetWorkingImage(&gm);
for (int a = 0; a < 240; a++)
{
for (int b = 0; b < 240; b++)
{
fire[i].xy[a][b] = getpixel(a, b);
}
}
}
//加载烟花弹
IMAGE jetimg;
loadimage(&jetimg, L"./res/shoot.jpg", 200, 50);
//窗口 默认工作区
//jetimg
SetWorkingImage(&jetimg);
for (int i = 0; i < NUM; i++)
{
int num = rand() % 5; //0-4
getimage(&jet[i].img[0], num * 20, 0, 20, 50); //获取图片
getimage(&jet[i].img[1], (num + 5) * 20, 0, 20, 50);
}
SetWorkingImage(NULL);
}
//选择一些烟花弹
void Choise(DWORD &t1)
{
//烟花弹出现 时间间隔
DWORD t2 = timeGetTime();
if (t2 - t1 > 100) //每间隔100个ms 产生一个新的烟花弹
{
int n = rand() % 13;
if (jet[n].shoot == false && fire[n].show == false)
{
//重置坐标各种数据
jet[n].x = rand() % 1200;
jet[n].y = rand() % 100 + 600; //600-699
jet[n].hx = jet[n].x;
jet[n].hy = rand() % 400; //0-399
jet[n].height = jet[n].y - jet[n].hy;
jet[n].shoot = true;
putimage(jet[n].x, jet[n].y, &jet[n].img[jet[n].n], SRCINVERT);
}
t1 = t2;
}
}
//发射
void Shoot()
{
for (int i = 0; i < NUM; i++)
{
jet[i].t2 = timeGetTime();
if (jet[i].t2 - jet[i].t1 > jet[i].dt&&jet[i].shoot == true)
{
putimage(jet[i].x, jet[i].y, &jet[i].img[jet[i].n], SRCINVERT);
if (jet[i].hy < jet[i].y)
{
jet[i].n++; //闪烁
jet[i].y -= 5;
}
putimage(jet[i].x, jet[i].y, &jet[i].img[jet[i].n], SRCINVERT);
//上升到height的3/4的时候 减速
if ((jet[i].y - jet[i].hy) * 4 < jet[i].height)
{
jet[i].dt = rand() % 4 + 10;
}
if (jet[i].y <= jet[i].hy)
{
putimage(jet[i].x, jet[i].y, &jet[i].img[jet[i].n], SRCINVERT);
fire[i].x = jet[i].hx + 10;
fire[i].y = jet[i].hy;
fire[i].show = true;
jet[i].shoot = false;
}
jet[i].t1 = jet[i].t2;
}
}
}
//烟花绽放
void show(DWORD* pMem)
{
int drt[16] = { 5, 5, 5, 5, 5, 6, 25, 25, 25, 25, 55, 55, 55, 55, 55, 55 };
for (int i = 0; i < NUM; i++)
{
fire[i].t2 = timeGetTime();
if (fire[i].t2 - fire[i].t1 > fire[i].dt&&fire[i].show == true)
{
if (fire[i].r < fire[i].max_r)
{
fire[i].r++;
fire[i].r < drt[fire[i].r / 10];
fire[i].draw = true;
}
if (fire[i].r >= fire[i].max_r - 1)
{
fire[i].draw = false;
Init(i);
}
fire[i].t1 = fire[i].t2;
}
if (fire[i].draw) //饱和度 色相 亮度
{
for (double a = 0; a <= 6.28; a += 0.01)
{
int x1 = (int)(fire[i].cen_x + fire[i].r*cos(a));
int y1 = (int)(fire[i].cen_y + fire[i].r*sin(a));
if (x1 > 0 && x1 < fire[i].width&&y1>0 && y1 < fire[i].height)
{
int b = fire[i].xy[x1][y1] & 0xff;
int g = (fire[i].xy[x1][y1] >> 8) & 0xff;
int r = (fire[i].xy[x1][y1] >> 16);
int xx = (int)(fire[i].x + fire[i].r*cos(a));
int yy = (int)(fire[i].y - fire[i].r*sin(a));
if (r > 0x20 && g > 0x20 && b > 0x20 && xx > 0 && xx < 1200 && yy>0 && yy < 800)
pMem[yy * 1200 + xx] = BGR(fire[i].xy[x1][y1]);
}
}
fire[i].draw = false;
}
}
}
//主函数
int main()
{
Welcome();
DWORD t1 = timeGetTime();
DWORD* pMem = GetImageBuffer();
for (int i = 0; i < NUM; i++) //13 13个烟花弹
{
Init(i);
}
Loadjpg();
while (1)
{
for (int clr = 0; clr < 1000; clr++)
{
for (int j = 0; j < 2; j++)
{
int px1 = rand() % 1200;
int py1 = rand() % 800; //2000
if (py1 < 799)
{
pMem[py1 * 1200 + px1] = pMem[py1 * 1200 + px1 + 1] = BLACK;
}
}
}
Choise(t1);
Shoot();
show(pMem);
}
return 0;
}
评论1
最新资源