没有好的素材,用星星代表雪花了。
运行结果:
源代码:
#include
#pragma comment(lib, "winmm.lib") //调用PlaySound函数所需库文件t
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
#define WINDOW_TITLE L"【游戏程序设计】粒子系统-雪花"
//粒子雪花
struct Snow
{
int x;
int y;
bool exist;
};
HINSTANCE hInst;
HDC hdc, mdc, bufdc;
HBITMAP hBg, hMask, hSnow; //贴图的三个HBITMAP变量
int count; //计数
Snow snow[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);
hSnow = (HBITMAP)LoadImage(NULL, L"snow.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(hSnow);
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)
{
//创建粒子
//当粒子小于50时,产生新的粒子,设定每个粒子的属性值
if(count < 50)
{
snow[count].exist = true; //设定粒子存在
snow[count].x = rand() % rect.right; //将粒子的X坐标设为窗口中水平方向上的任意位置
snow[count].y = 0; //将每个粒子的Y坐标设为0,即从窗口沿往下落
++count; //每产生一个粒子进行累加计数
}
//贴上背景图到mdc中
SelectObject(bufdc, hBg);
BitBlt(mdc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, bufdc, 0, 0, SRCCOPY);
//首先判断粒子是否存在,若存在,进行透明贴图操作
for(int i = 0; i < 50; ++i)
{
if(snow[i].exist)
{
SelectObject(bufdc, hMask);
BitBlt(mdc, snow[i].x, snow[i].y, 20, 20, bufdc, 0, 0, SRCAND);
SelectObject(bufdc, hSnow);
BitBlt(mdc, snow[i].x, snow[i].y, 20, 20, bufdc, 0, 0, SRCPAINT);
if(rand() % 2) //设定雪花下次的位置
snow[i].x += 5;
else
snow[i].x -= 5;
snow[i].y += 10;
if(snow[i].y > rect.bottom) //如果到了屏幕底部,重新设定位置到上方
{
snow[i].x = rand() % rect.right;
snow[i].y = 0;
}
}
else
break;
}
//将mdc的全部内容贴到hdc中
BitBlt(hdc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, mdc, 0, 0, SRCCOPY);
}