运行结果:
源代码:
#include
#pragma comment(lib, "winmm.lib") //调用PlaySound函数所需库文件t
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
#define WINDOW_TITLE L"【游戏程序设计】粒子系统-烟花"
//粒子星星
struct Flystar
{
int x; //星光所在的x坐标
int y; //星光所在的y坐标
int vx; //星光x方向的速度
int vy; //星光y方向的速度
int lasted; //星光存在的时间
bool exist; //星光是否存在
};
HINSTANCE hInst;
HDC hdc, mdc, bufdc;
HBITMAP hBg, hMask, hFlyStar; //贴图的三个HBITMAP变量
int count; //计数
Flystar flystar[50];
RECT rect; //用户区矩形
int MyWindowClass(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void MyDraw(HDC);
/*****************************************************************************************************************
在不同的应用程序中,在此处添加相关的全局变量
******************************************************************************************************************/
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPreInstace,
LPSTR lpCmdLine, int nCmdShow)
{
MyWindowClass(hInstance);
PlaySound(L"sound.wav", NULL, SND_FILENAME| SND_ASYNC| SND_LOOP); //循环播放背景音乐
if(!InitInstance(hInstance, nCmdShow))
return FALSE;
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
int MyWindowClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = L"gamebase";
wcex.hIconSm = NULL;
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance;
HWND hwnd = CreateWindow(L"gamebase", WINDOW_TITLE, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance, NULL);
if(!hwnd)
return FALSE;
MoveWindow(hwnd, 10, 10, WINDOW_WIDTH, WINDOW_HEIGHT, true);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
hdc = GetDC(hwnd);
mdc = CreateCompatibleDC(hdc);
bufdc = CreateCompatibleDC(hdc);
hBg = (HBITMAP)LoadImage(NULL, L"bg.bmp", IMAGE_BITMAP, WINDOW_WIDTH, WINDOW_HEIGHT, LR_LOADFROMFILE);
hMask = (HBITMAP)LoadImage(NULL, L"mask.bmp", IMAGE_BITMAP, 20, 20, LR_LOADFROMFILE);
hFlyStar = (HBITMAP)LoadImage(NULL, L"flystar.bmp", IMAGE_BITMAP, 20, 20, LR_LOADFROMFILE);
HBITMAP bmp = CreateCompatibleBitmap(hdc, WINDOW_WIDTH, WINDOW_HEIGHT);
SelectObject(mdc, bmp);
count = 0;
GetClientRect(hwnd, &rect); //获取用户区矩形
SetTimer(hwnd, 1, 100, NULL);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
switch(message)
{
//时间消息
//在消息循环中加入处理WM_TIMER消息,当接收到此消息时使用MyDraw函数进行窗口绘图
case WM_TIMER:
MyDraw(hdc);
break;
/**************************************************************************************************************
在退出程序前,往往在此处删除创建的相关资源
***************************************************************************************************************/
case WM_DESTROY: //窗口结束消息
DeleteObject(hBg);
DeleteObject(hMask);
DeleteObject(hFlyStar);
DeleteDC(mdc);
DeleteDC(bufdc);
ReleaseDC(hwnd, hdc);
KillTimer(hwnd, 1); //窗口结束时,删除所建立的定时器
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
/***************************************************************************************************************
在函数MyDraw()中进行相关绘制工作
****************************************************************************************************************/
void MyDraw(HDC hdc)
{
if(count == 0)
{
//随机设置爆炸点
int x = rand() % rect.right;
int y = rand() % rect.bottom;
//产生星光粒子
for(int i = 0; i < 50; ++i)
{
flystar[i].exist = true;
flystar[i].x = x;
flystar[i].y = y;
//按粒子编号i来决定粒子在那个象限运动,且x,y方向的移动速度
//随机为1-15的一个值,有1+rand()%15来完成
switch(i % 4)
{
case 0:
flystar[i].vx = -(1 + rand()%15);
flystar[i].vy = -(1 + rand()%15);
break;
case 1:
flystar[i].vx = 1 + rand()%15;
flystar[i].vy = -(1 + rand()%15);
break;
case 2:
flystar[i].vx = -(1 + rand()%15);
flystar[i].vy = 1 + rand()%15;
break;
case 3:
flystar[i].vx = 1 + rand()%15;
flystar[i].vy = 1 + rand()%15;
break;
}
//设定粒子的存在的时间为0
flystar[i].lasted = 0;
}
//50个粒子由for循环设置完成后,我们将粒子的数量设为50,代表目前有50颗星光
count = 50;
}
//先在内存dc中贴上背景图片
SelectObject(bufdc, hBg);
BitBlt(mdc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, bufdc, 0, 0, SRCCOPY);
for(int i = 0; i < 50; ++i)
//判断粒子是否还存在,若存在,则根据其坐标进行贴图操作
if(flystar[i].exist)
{
SelectObject(bufdc, hMask);
BitBlt(mdc, flystar[i].x, flystar[i].y, 20, 20, bufdc, 0, 0, SRCAND);
SelectObject(bufdc, hFlyStar);
BitBlt(mdc, flystar[i].x, flystar[i].y, 20, 20, bufdc, 0, 0, SRCPAINT);
//计算下一次贴图的坐标
flystar[i].x += flystar[i].vx;
flystar[i].y += flystar[i].vy;
//每一次贴图后,粒子的存在时间累加1
++flystar[i].lasted;
//进行条件判断,若某粒子跑出窗口区域一定的范围,则将该粒子设为不存在,且粒子数随之递减
if(flystar[i].x < 0 || flystar[i].x > rect.right || flystar[i].y < 0
|| flystar[i].y > rect.bottom || flystar[i].lasted > 50)
{
flystar[i].exist = false; //删除星光粒子
--count; //递减星光总数
}
}
//将mdc的全部内容贴到hdc中
BitBlt(hdc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, mdc, 0, 0, SRCCOPY);
}