粒子系统。

  1. #include "stdafx.h"   
  2. #include <stdio.h>   
  3.   
  4. //全局变量声明   
  5. HINSTANCE hInst;  
  6. HBITMAP bg,star,mask;  //用于贴图的三个HBITMAP变量   
  7. HDC hdc,mdc,bufdc;  
  8. HWND    hWnd;  
  9. RECT    rect;  
  10. int i,count; //定义count用于计数   
  11.   
  12.   
  13.   
  14.   
  15. struct flystar  
  16. {  
  17.     int x;       //星光所在的x坐标   
  18.     int y;       //星光所在的y坐标   
  19.     int vx;      //星光x方向的速度   
  20.     int vy;      //星光y方向的速度   
  21.     int lasted;  //星光存在的时间   
  22.     BOOL exist;  //星光是否存在   
  23. }flystar[50];  
  24.   
  25.   
  26. //全局函数声明   
  27. ATOM                MyRegisterClass(HINSTANCE hInstance);  
  28. BOOL                InitInstance(HINSTANCEint);  
  29. LRESULT CALLBACK    WndProc(HWNDUINTWPARAMLPARAM);  
  30. void                MyPaint(HDC hdc);  
  31.   
  32. //****WinMain函数,程序入口点函数**************************************    
  33. int APIENTRY WinMain(HINSTANCE hInstance,  
  34.                      HINSTANCE hPrevInstance,  
  35.                      LPSTR     lpCmdLine,  
  36.                      int       nCmdShow)  
  37. {  
  38.     MSG msg;  
  39.   
  40.     MyRegisterClass(hInstance);  
  41.   
  42.     //初始化   
  43.     if (!InitInstance (hInstance, nCmdShow))   
  44.     {  
  45.         return FALSE;  
  46.     }  
  47.   
  48.            
  49.     //消息循环     
  50.     while (GetMessage(&msg, NULL, 0, 0))     
  51.     {    
  52.         TranslateMessage(&msg);    
  53.         DispatchMessage(&msg);    
  54.     }    
  55.   
  56.     return msg.wParam;  
  57. }  
  58.   
  59. //****设计一个窗口类,类似填空题,使用窗口结构体*********************    
  60. ATOM MyRegisterClass(HINSTANCE hInstance)  
  61. {  
  62.     WNDCLASSEX wcex;  
  63.   
  64.     wcex.cbSize = sizeof(WNDCLASSEX);   
  65.     wcex.style          = CS_HREDRAW | CS_VREDRAW;  
  66.     wcex.lpfnWndProc    = (WNDPROC)WndProc;  
  67.     wcex.cbClsExtra     = 0;  
  68.     wcex.cbWndExtra     = 0;  
  69.     wcex.hInstance      = hInstance;  
  70.     wcex.hIcon          = NULL;  
  71.     wcex.hCursor        = NULL;  
  72.     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);  
  73.     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);  
  74.     wcex.lpszMenuName   = NULL;  
  75.     wcex.lpszClassName  = "maple";  
  76.     wcex.hIconSm        = NULL;  
  77.   
  78.     return RegisterClassEx(&wcex);  
  79. }  
  80.   
  81. //****初始化函数*************************************     
  82. // 1.加载位图资源   
  83. // 2.取得内部窗口区域信息     
  84. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)  
  85. {  
  86.     HBITMAP bmp;  
  87.     hInst = hInstance;  
  88.   
  89.     hWnd = CreateWindow("maple""浅墨的绘图窗口" , WS_OVERLAPPEDWINDOW,  
  90.         CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);  
  91.   
  92.     if (!hWnd)  
  93.     {  
  94.         return FALSE;  
  95.     }  
  96.   
  97.     MoveWindow(hWnd,10,10,600,450,true);  
  98.     ShowWindow(hWnd, nCmdShow);  
  99.     UpdateWindow(hWnd);  
  100.   
  101.     hdc = GetDC(hWnd);  
  102.     mdc = CreateCompatibleDC(hdc);  
  103.   
  104.     bufdc = CreateCompatibleDC(hdc);  
  105.     bmp = CreateCompatibleBitmap(hdc,640,480);  
  106.   
  107.     SelectObject(mdc,bmp);  
  108.   
  109.   
  110.       
  111.   
  112.     bg = (HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,rect.right,rect.bottom,LR_LOADFROMFILE);   
  113.     star = (HBITMAP)LoadImage(NULL,"star.bmp",IMAGE_BITMAP,30,30,LR_LOADFROMFILE);   
  114.     mask = (HBITMAP)LoadImage(NULL,"mask.bmp",IMAGE_BITMAP,30,30,LR_LOADFROMFILE);   
  115.     GetClientRect(hWnd,&rect);  
  116.   
  117.   
  118.       
  119.     SetTimer(hWnd,1,0,NULL);  
  120.   
  121.     MyPaint(hdc);  
  122.   
  123.     return TRUE;  
  124. }  
  125.   
  126. //****自定义绘图函数*********************************   
  127. // 1.窗口贴图   
  128. // 2.实现星光绽放的效果   
  129. void MyPaint(HDC hdc)  
  130. {  
  131.   
  132.   
  133. //创建粒子   
  134.     if(count == 0)              //随机设置爆炸点   
  135.     {  
  136.     int x=rand()%rect.right;  
  137.     int y=rand()%rect.bottom;  
  138.         for(i=0;i<50;i++)       //产生星光粒子   
  139.         {  
  140.             flystar[i].x = x;  
  141.             flystar[i].y = y;  
  142.             flystar[i].lasted = 0;  //设定该粒子存在的时间为零   
  143.             if(i%2==0)       //按粒子编号i来决定粒子在哪个象限运动,且x,y方向的移动速度随机为1—15之间的一个值,由1+rand()%15来完成。   
  144.             {  
  145.                 flystar[i].vx =  -(1+rand()%15);  
  146.                 flystar[i].vy =  -(1+rand()%15);  
  147.             }  
  148.             if(i%2==1)  
  149.             {  
  150.                 flystar[i].vx = 1+rand()%15;  
  151.                 flystar[i].vy = 1+rand()%15;  
  152.             }  
  153.             if(i%4==2)  
  154.             {  
  155.                 flystar[i].vx = -(1+rand()%15);  
  156.                 flystar[i].vy = 1+rand()%15;  
  157.             }  
  158.             if(i%4==3)  
  159.             {  
  160.                 flystar[i].vx = 1+rand()%15;  
  161.                 flystar[i].vy = -(1+rand()%15);  
  162.             }  
  163.             flystar[i].exist = true;  //设定粒子存在   
  164.         }  
  165.         count = 50;   //50个粒子由for循环设置完成后,我们将粒子数量设为50,代表目前有50颗星光   
  166.     }  
  167.       
  168.     //先在内存dc中贴上背景图片   
  169.     SelectObject(bufdc,bg);  
  170.     BitBlt(mdc,0,0,rect.right,rect.bottom,bufdc,0,0,SRCCOPY);  
  171.   
  172.     for(i=0;i<50;i++)  
  173.     {  
  174.         if(flystar[i].exist)   //判断粒子是否还存在,若存在,则根据其坐标(flystar[i].x,flystar[i].y)进行贴图操作   
  175.         {  
  176.             SelectObject(bufdc,mask);  
  177.             BitBlt(mdc,flystar[i].x,flystar[i].y,30,30,bufdc,0,0,SRCAND);  
  178.             SelectObject(bufdc,star);  
  179.             BitBlt(mdc,flystar[i].x,flystar[i].y,30,30,bufdc,0,0,SRCPAINT);  
  180.   
  181.             //计算下一次贴图的坐标   
  182.             flystar[i].x+=flystar[i].vx;  
  183.             flystar[i].y+=flystar[i].vy;  
  184.   
  185.             //在每进行一次贴图后,将粒子的存在时间累加1.   
  186.             flystar[i].lasted++;  
  187.             //进行条件判断,若某粒子跑出窗口区域一定的范围,则将该粒子设为不存在,且粒子数随之递减   
  188.             if(flystar[i].x<=-10 || flystar[i].x>rect.right || flystar[i].y<=-10 || flystar[i].y>rect.bottom || flystar[i].lasted>50)  
  189.             {  
  190.                 flystar[i].exist = false;  //删除星光粒子    
  191.                 count--;                    //递减星光总数   
  192.             }  
  193.         }  
  194.     }  
  195.       
  196. //将mdc中的全部内容贴到hdc中   
  197.     BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY);  
  198.   
  199. }  
  200.   
  201.   
  202.   
  203. //****消息处理函数***********************************   
  204. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)  
  205. {  
  206.     switch (message)  
  207.     {  
  208.         case WM_TIMER:                      //时间消息     
  209.             MyPaint(hdc);                   //在消息循环中加入处理WM_TIMER消息,当接收到此消息时便调用MyPaint()函数进行窗口绘图     
  210.             break;    
  211.         case WM_KEYDOWN:                     //按键消息     
  212.             if(wParam==VK_ESCAPE)            //按下【Esc】键   
  213.                 PostQuitMessage(0);  
  214.             break;  
  215.         case WM_DESTROY:                     //窗口结束消息    
  216.             DeleteDC(mdc);  
  217.             DeleteDC(bufdc);  
  218.             DeleteObject(bg);  
  219.             DeleteObject(star);  
  220.             DeleteObject(mask);  
  221.             KillTimer(hWnd,1);             //窗口结束时,删除所建立的定时器          
  222.             ReleaseDC(hWnd,hdc);  
  223.             PostQuitMessage(0);  
  224.             break;  
  225.         default:                            //其他消息   
  226.             return DefWindowProc(hWnd, message, wParam, lParam);  
  227.    }  
  228.    return 0;  
  229. }  

这个“星光绽放”demo运行的截图如下:


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值