admin 管理员组文章数量: 887021
2024年1月25日发(作者:html网站的首页文件通常命名为)
好看的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图形和多线程的相关知识,和加强程序员对于算法的理解。同时,通过调整用于生成烟花的
随机数函数和数据的范围,可以让编写者从中更深刻地了解程序逻辑和代码优化。因此,此程序也为初学者提供了一个很好的编程示例。
版权声明:本文标题:好看的c语言程序烟花源代码 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1706148030h501844.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论