游戏中重力的编写

  1. #include "stdafx.h"   
  2. #include <stdio.h>   
  3.   
  4. //全局变量声明   
  5. HINSTANCE hInst;  
  6. HBITMAP bg,angrybird;  
  7. HDC     hdc,mdc,bufdc;  
  8. HWND    hWnd;  
  9. DWORD   tPre,tNow,tCheck;  
  10. RECT    rect;  
  11.    //初始横坐标x=0,初始纵坐标y=100,初始水平方向速度vx=6,   
  12. float x=0.0,y=100.0,vx=6.0,vy=0.0,gy=3.0;
  13.   //初始竖直方向速度vy=0,重力加速度gy=3(这里为了方便演示,我们设置为3)   
  14.   
  15.   
  16. //全局函数声明   
  17. ATOM                MyRegisterClass(HINSTANCE hInstance);  
  18. BOOL                InitInstance(HINSTANCEint);  
  19. LRESULT CALLBACK    WndProc(HWNDUINTWPARAMLPARAM);  
  20. void                MyPaint(HDC hdc);  
  21.   
  22. //****WinMain函数,程序入口点函数**************************************    
  23. int APIENTRY WinMain(HINSTANCE hInstance,  
  24.                      HINSTANCE hPrevInstance,  
  25.                      LPSTR     lpCmdLine,  
  26.                      int       nCmdShow)  
  27. {  
  28.     MSG msg;  
  29.   
  30.     MyRegisterClass(hInstance);  
  31.   
  32.     //初始化   
  33.     if (!InitInstance (hInstance, nCmdShow))   
  34.     {  
  35.         return FALSE;  
  36.     }  
  37.   
  38.            
  39.     //消息循环   
  40.     GetMessage(&msg,NULL,NULL,NULL);            //初始化msg      
  41.     while( msg.message!=WM_QUIT )  
  42.     {  
  43.         if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) )  
  44.         {  
  45.             TranslateMessage( &msg );  
  46.             DispatchMessage( &msg );  
  47.         }  
  48.         else  
  49.         {  
  50.             tNow = GetTickCount();  
  51.             if(tNow-tPre >= 50)  
  52.                 MyPaint(hdc);  
  53.         }  
  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.     bufdc = CreateCompatibleDC(hdc);  
  104.     bmp = CreateCompatibleBitmap(hdc,640,480);  
  105.   
  106.     SelectObject(mdc,bmp);  
  107.   
  108.     bg = (HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,640,480,LR_LOADFROMFILE);  
  109.     angrybird = (HBITMAP)LoadImage(NULL,"angrybird.bmp",IMAGE_BITMAP,120,60,LR_LOADFROMFILE);  
  110.       
  111.     GetClientRect(hWnd,&rect);  
  112.     SelectObject(bufdc,bg);  
  113.     BitBlt(mdc,0,0,640,480,bufdc,0,0,SRCCOPY);  
  114.     MyPaint(hdc);  
  115.   
  116.     return TRUE;  
  117. }  
  118.   
  119. //****自定义绘图函数*********************************   
  120. // 1.窗口贴图   
  121. // 2.计算小球速度,坐标以及判断是否碰到窗口下缘   
  122. void MyPaint(HDC hdc)  
  123. {  
  124.     SelectObject(bufdc,bg);  
  125.     BitBlt(mdc,0,0,640,480,bufdc,0,0,SRCCOPY);  
  126.   
  127.     SelectObject(bufdc,angrybird);  
  128.     BitBlt(mdc,x,y,60,60,bufdc,60,0,SRCAND);  
  129.     BitBlt(mdc,x,y,60,60,bufdc,0,0,SRCPAINT);  
  130.   
  131.     BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY);  
  132.   
  133.   
  134.     x += vx;            //计算X轴方向贴图坐标,每调用一次MyPiant(),x坐标就加上一个恒定不变的vx,相当于匀速运动   
  135.       
  136.     vy = vy + gy/20;       //计算Y轴方向速度分量,vy随着每一次MyPiant()函数的调用就加上一个gy(重力加速度),因为50毫秒刷新一次   
  137.     y += vy;            //计算Y轴方向贴图坐标,每调用一次MyPiant(),y坐标就加上一个刚改变过后的vy,相当于加速运动   
  138.   
  139.   
  140.     //判断是否触地,如果触碰到窗口边界,vy调整为相反方向   
  141.     if(y >= rect.bottom-60)       
  142.     {  
  143.         y = rect.bottom - 60;  
  144.         vy = -vy;  
  145.     }  
  146.   
  147.     tPre = GetTickCount();     //记录上次的绘图时间   
  148. }  
  149.   
  150. //****消息处理函数***********************************   
  151. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)  
  152. {  
  153.     switch (message)  
  154.     {  
  155.         case WM_KEYDOWN:                     //按键消息     
  156.             if(wParam==VK_ESCAPE)            //按下【Esc】键   
  157.                 PostQuitMessage(0);  
  158.             break;  
  159.         case WM_DESTROY:                     //窗口结束消息    
  160.             DeleteDC(mdc);  
  161.             DeleteDC(bufdc);  
  162.             DeleteObject(bg);  
  163.             DeleteObject(angrybird);  
  164.             ReleaseDC(hWnd,hdc);  
  165.             PostQuitMessage(0);  
  166.             break;  
  167.         default:                            //其他消息   
  168.             return DefWindowProc(hWnd, message, wParam, lParam);  
  169.    }  
  170.    return 0;  
  171. }  
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值