浪漫表白效果:
開發環境:Visual Studio 2019 EasyX圖形庫插件即可
源碼展示:
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<conio.h>
#include<graphics.h> //自己安裝了才有
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")//多媒體設備接口庫文件
#define PI 3.1415
#define NUM 13 //煙花彈數量
#define WIDTH 960
#define HEIGHT 640
struct Fire
{
int x, y;
int cen_x, cen_y; //中心點坐标
int r; //當前綻放半徑
int max_r; //最大綻放半徑
int width, height;
DWORD pixel[240][240]; //圖片像素數組
bool isShow; //煙花是否準備好了
bool isDraw; //是否繪制煙花
}fire[NUM];
struct Jet //煙花彈
{
int x, y; //坐标
int hx, hy; //最高點的坐标
bool isLaunch; //煙花彈是否在發射中
IMAGE img[2];
byte n : 1; //位段 0 1 0 1 0 1 0 1 0 1
}jet[NUM];
void welcome()
{
mciSendString(L"open ./res/小幸運.mp3", 0, 0, 0);
mciSendString(L"play ./res/小幸運.mp3", 0, 0, 0);
for (int i = 0; i < 50; i )
{
//清除屏幕
cleardevice();
int x = 500 180 * sin(2 * PI / 60 * i);
int y = 200 180 * cos(2 * PI / 60 * i);
//設置字體樣式
settextstyle(i, 0, L"楷體");
settextcolor(RGB(0, 202, 0));
setbkmode(TRANSPARENT);
outtextxy(x, y, L"浪漫表白程序");
Sleep(20);
}
_getch();//按任意鍵繼續
cleardevice();
settextstyle(25, 0, L"楷體");
outtextxy(350, 100, L"親愛的XXX");
outtextxy(350, 150, L"自從第一天看見你");
outtextxy(350, 200, L"我的心就不能自已");
outtextxy(350, 250, L"不管天上地下都想看見你");
outtextxy(350, 300, L"...");
outtextxy(350, 350, L"...");
outtextxy(450, 400, L"--愛你的XXX");
}
void InitData(int i);
void loadImg()
{
//初始化綻放效果
IMAGE bloomImg,tImg;
loadimage(&bloomImg, L"./res/flower.jpg", 3120, 240);
for (int i = 0; i < NUM; i )
{
InitData(i);
SetWorkingImage(&bloomImg);
getimage(&tImg, i * 240, 0, 240, 240);
SetWorkingImage(&tImg);
for (int a = 0; a < 240; a )
{
for (int b = 0; b < 240; b )
{
fire[i].pixel[a][b] = getpixel(a, b);
}
}
}
//初始化煙花彈
IMAGE jetImg;
loadimage(&jetImg, L"./res/launch.jpg", 200, 50);
SetWorkingImage(&jetImg);
for (int i = 0; i < NUM; i )
{
int n = rand() % 5;
getimage(&jet[i].img[0], n*20, 0, 20, 50);
getimage(&jet[i].img[1], n*20 100, 0, 20, 50);
jet[i].isLaunch = false;
}
SetWorkingImage();
}
void InitData(int i)
{
fire[i].cen_x = 120;
fire[i].cen_y = 120;
fire[i].max_r = 120;
fire[i].r = 0;
fire[i].width = 240;
fire[i].height = 240;
fire[i].isDraw = false;
fire[i].isShow = false;
}
//産生煙花彈
void createJet()
{
int i = rand() % NUM;//[0~13)
if (jet[i].isLaunch == false)
{
jet[i].x = rand() % (WIDTH-20);
jet[i].y = rand() % 100 HEIGHT;
jet[i].hx = jet[i].x;
jet[i].hy = rand() % (HEIGHT / 3 * 2);
jet[i].isLaunch = true;
}
}
//發射煙花彈
void launch()
{
for (int i = 0; i < NUM; i )
{
if (jet[i].isLaunch)
{
putimage(jet[i].x, jet[i].y, &jet[i].img[jet[i].n],SRCINVERT);
//判斷是否達到最高點了
if (jet[i].y > jet[i].hy)
{
jet[i].y -= 5;
jet[i].n ;
}
putimage(jet[i].x, jet[i].y, &jet[i].img[jet[i].n], SRCINVERT);
if (jet[i].y <= jet[i].hy)
{
jet[i].isLaunch = false;
//可以開始放煙花了
fire[i].x = jet[i].x;
fire[i].y = jet[i].y;
fire[i].isShow = true;
}
}
}
}
void bloom(DWORD *pMem)
{
for (int i = 0; i < NUM; i )
{
if (fire[i].isShow)
{
if (fire[i].r < fire[i].max_r)
{
fire[i].r ;
fire[i].isDraw = true; //可以開始繪制煙花了
}
if (fire[i].r >= fire[i].max_r)
{
InitData(i);
}
}
if (fire[i].isDraw)
{
//求當前半徑下,圓上的每個點對于的弧度
for (double a = 0; a <= 2 * PI; a = 0.01)
{
int img_x = fire[i].cen_x fire[i].r * cos(a);//求出圓上的每個點的坐标
int img_y = fire[i].cen_y fire[i].r * sin(a);
if (img_x > 0 && img_x < fire[i].width && img_y>0 && img_y < fire[i].height)
{
//針對現在的綻放點,對應的屏幕坐标
int win_x= fire[i].x fire[i].r * cos(a);
int win_y = fire[i].y fire[i].r * sin(a);
if (win_x > 0 && win_x < WIDTH && win_y>0 && win_y < WIDTH)
{
pMem[win_y * WIDTH win_x] = BGR(fire[i].pixel[img_x][img_y]);
}
}
}
}
}
}
int main()
{
//創建窗口
initgraph(960, 640);
//設置随機數種子
srand((unsigned)time(NULL) clock());
welcome();
loadImg();
DWORD* pMem = GetImageBuffer(); //獲取窗口的内存指針
while (1)
{
for (int i = 0; i < WIDTH; i )
{
for (int k = 0; k < 4; k )
{
int x = rand() % WIDTH;
int y = rand() % HEIGHT;
if (y < HEIGHT)
{
pMem[y * WIDTH x] = BLACK;
}
}
}
createJet();
launch();
bloom(pMem);
Sleep(10);
}
closegraph();
return 0;
}
以上就是我們表白程序的源碼了!最後祝大家都能和喜歡的人在一起哦~
我的C/C 編程學習基地,點擊正在跳轉「鍊接」即可加入,歡迎有問題的小夥伴一起進來學習交流嗷~
,更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!