2024年2月8日发(作者:)
好看的c语言程序烟花源代码
烟花是人们新年、庆祝等节日最喜爱的庆祝道具之一,而用C语言编写的烟花程序更是酷炫、有趣,甚至具有一定的指导意义。
本程序采用的是Win32 API图形和多线程,实现了屏幕上多个烟花同时绽放、炫彩夺目的效果。以下是程序代码:
```
include
include
include
include
define MAX_FIREWORKS 10
define MAX_PARTICLES 100
struct ParticleStruct {
float x, y;
float vx, vy;
float brightness;
};
struct FireworkStruct {
int x, y;
int color;
int particlesLeft;
bool exploded;
ParticleStruct particles[MAX_PARTICLES];
};
FireworkStruct fireworks[MAX_FIREWORKS];
int screenHeight, screenWidth;
HDC hDCMem;
HBITMAP hBitmap;
HBITMAP hOldBitmap;
void InitializeFirework(int index) {
fireworks[index].x = rand() % screenWidth;
fireworks[index].y = screenHeight - 1;
fireworks[index].color = RGB(rand() % 256, rand() %
256, rand() % 256);
fireworks[index].particlesLeft = rand() % 101 + 100;
fireworks[index].exploded = false;
for (int i = 0; i < MAX_PARTICLES; i++) {
fireworks[index].particles[i].x =
fireworks[index].x;
fireworks[index].particles[i].y =
fireworks[index].y;
fireworks[index].particles[i].vx = (rand() % 11 -
5) / 10.0;
fireworks[index].particles[i].vy = (rand() % 11 -
10) / 10.0;
fireworks[index].particles[i].brightness = 1.0;
}
}
void InitializeFireworks() {
for (int i = 0; i < MAX_FIREWORKS; i++)
InitializeFirework(i);
}
bool UpdateFirework(int index) {
if (!fireworks[index].exploded) {
fireworks[index].y -= rand() % 6 + 3;
if (fireworks[index].y < screenHeight / 4) {
fireworks[index].exploded = true;
for (int i = 0; i < MAX_PARTICLES; i++) {
fireworks[index].particles[i].vx = 0;
fireworks[index].particles[i].vy = 0;
}
}
return true;
}
else {
bool particleExists = false;
for (int i = 0; i < MAX_PARTICLES; i++)
{
fireworks[index].particles[i].vx *= 0.95;
fireworks[index].particles[i].vy *= 0.95;
fireworks[index].particles[i].x +=
fireworks[index].particles[i].vx;
fireworks[index].particles[i].y +=
fireworks[index].particles[i].vy;
fireworks[index].particles[i].brightness *=
0.975;
if (fireworks[index].particles[i].brightness
0.0)
particleExists = true;
}
fireworks[index].particlesLeft--;
if (fireworks[index].particlesLeft == 0
|| !particleExists) {
InitializeFirework(index);
return false;
}
return true;
}
}
>
void UpdateFireworks() {
for (int i = 0; i < MAX_FIREWORKS; i++)
UpdateFirework(i);
}
void DrawFireworks() {
HBRUSH hBrush;
for (int i = 0; i < MAX_FIREWORKS; i++) {
if (!fireworks[i].exploded) {
hBrush = CreateSolidBrush(fireworks[i].color);
SelectObject(hDCMem, hBrush);
Ellipse(hDCMem, fireworks[i].x - 5,
fireworks[i].y - 5, fireworks[i].x + 5, fireworks[i].y + 5);
DeleteObject(hBrush);
}
else {
for (int j = 0; j < MAX_PARTICLES; j++) {
hBrush = CreateSolidBrush(RGB(255, 255,
255)/*fireworks[i].color*/);
SelectObject(hDCMem, hBrush);
SetPixel(hDCMem,
fireworks[i].particles[j].x, fireworks[i].particles[j].y,
fireworks[i].color*fireworks[i].particles[j].brightness);
DeleteObject(hBrush);
}
}
}
}
DWORD WINAPI UpdateThread(LPVOID lpParam) { while (true) {
UpdateFireworks();
Sleep(10);
}
return 0;
}
int main() {
HWND hWnd = GetDesktopWindow();
HDC hDC = GetDC(hWnd);
screenHeight = GetSystemMetrics(SM_CYSCREEN);
screenWidth = GetSystemMetrics(SM_CXSCREEN);
hDCMem = CreateCompatibleDC(hDC);
hBitmap = CreateCompatibleBitmap(hDC, screenWidth,
screenHeight);
hOldBitmap = (HBITMAP)SelectObject(hDCMem, hBitmap);
InitializeFireworks();
CreateThread(NULL, 0, &UpdateThread, NULL, 0, NULL);
MSG msg;
BOOL bRet;
while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) {
if (bRet == -1)
break;
else {
TranslateMessage(&msg);
DispatchMessage(&msg);
DrawFireworks();
BitBlt(hDC, 0, 0, screenWidth, screenHeight,
hDCMem, 0, 0, SRCCOPY);
}
}
SelectObject(hDCMem, hOldBitmap);
DeleteObject(hBitmap);
DeleteDC(hDCMem);
ReleaseDC(hWnd, hDC);
return 0;
}
```
本程序实现了多个烟花同时在屏幕上绽放和消失,其中每个烟花的颜色和位置随机生成。程序的核心算法是利用多线程实现对所有烟花的更新,每个烟花的状态都包括其所处的位置、颜色、烟花粒子等。更新和绘制烟花部分采用了Win32 API图形相关函数,如CreateSolidBrush、SetPixel、BitBlt等。同时为确保程序运行的流畅,程序还包括了多线程相关函数,如CreateThread、Sleep等。
此程序的编写不仅仅是一次有趣的娱乐,同时也具有指导意义,可以帮助初学编程的人更好地了解Win32 API图形和多线程的相关知识,和加强程序员对于算法的理解。同时,通过调整用于生成烟花的
随机数函数和数据的范围,可以让编写者从中更深刻地了解程序逻辑和代码优化。因此,此程序也为初学者提供了一个很好的编程示例。
本文发布于:2024-02-08 13:40:03,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170737080367590.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |