日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[游戏模版21] Win32 物理引擎 能量守恒

發(fā)布時間:2025/3/21 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [游戏模版21] Win32 物理引擎 能量守恒 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.


?

>_<:Only a little change in the function of MyPaint(...),besides the initial value have some changes.

>_<:resource

>_<:code:

??

1 #include <windows.h> 2 // C 運行時頭文件 3 #include <stdlib.h> 4 #include <cstdio> 5 #include <malloc.h> 6 #include <memory.h> 7 #include <tchar.h> 8 #include <time.h> 9 #include <string> 10 #include <cmath> 11 12 // 全局變量: 13 HINSTANCE hInst; // 當前實例 14 HBITMAP bg , ball[2]; 15 HDC hdc,mdc,bufdc; 16 HWND hWnd; 17 RECT rect;//窗口矩形 18 int x[2] , y[2];//位置 19 int vx[2] , vy[2];//速度 20 int ax[2] , ay[2];//加速度 21 int t=1 , num=0;//時間 22 23 // 此代碼模塊中包含的函數(shù)的前向聲明: 24 ATOM MyRegisterClass(HINSTANCE hInstance); 25 BOOL InitInstance(HINSTANCE, int); 26 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 27 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); 28 void MyPaint(HDC hdc); 29 30 31 int APIENTRY _tWinMain(HINSTANCE hInstance, 32 HINSTANCE hPrevInstance, 33 LPTSTR lpCmdLine, 34 int nCmdShow){ 35 36 MSG msg; 37 MyRegisterClass(hInstance); 38 // 執(zhí)行應用程序初始化: 39 if (!InitInstance (hInstance, nCmdShow)){ 40 return FALSE; 41 } 42 // 主消息循環(huán): 43 while (GetMessage(&msg, NULL, 0, 0)){ 44 TranslateMessage(&msg); 45 DispatchMessage(&msg); 46 } 47 return (int) msg.wParam; 48 } 49 50 // 函數(shù): MyRegisterClass() 51 // 52 // 目的: 注冊窗口類。 53 ATOM MyRegisterClass(HINSTANCE hInstance){ 54 WNDCLASSEX wcex; 55 56 wcex.cbSize = sizeof(WNDCLASSEX); 57 58 wcex.style = CS_HREDRAW | CS_VREDRAW; 59 wcex.lpfnWndProc = WndProc; 60 wcex.cbClsExtra = 0; 61 wcex.cbWndExtra = 0; 62 wcex.hInstance = hInstance; 63 wcex.hIcon = NULL; 64 wcex.hCursor = LoadCursor(NULL, IDC_ARROW); 65 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 66 wcex.lpszMenuName = "Beautifulzzzz"; 67 wcex.lpszClassName = "Beautifulzzzz"; 68 wcex.hIconSm = NULL; 69 70 return RegisterClassEx(&wcex); 71 } 72 73 // 74 // 函數(shù): InitInstance(HINSTANCE, int) 75 // 76 // 目的: 保存實例句柄并創(chuàng)建主窗口 77 // 78 // 注釋: 79 // 80 // 在此函數(shù)中,我們在全局變量中保存實例句柄并 81 // 創(chuàng)建和顯示主程序窗口。 82 // 棋盤拼接以及調用InitGame()開始棋局 83 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){ 84 HBITMAP bmp; 85 hInst = hInstance; // 將實例句柄存儲在全局變量中 86 87 hWnd = CreateWindow("Beautifulzzzz","Beautifulzzzz", WS_OVERLAPPEDWINDOW, 88 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); 89 90 if (!hWnd) 91 { 92 return FALSE; 93 } 94 95 MoveWindow(hWnd,10,10,600,450,true); 96 ShowWindow(hWnd, nCmdShow); 97 UpdateWindow(hWnd); 98 99 hdc=GetDC(hWnd); 100 mdc=CreateCompatibleDC(hdc); 101 bufdc=CreateCompatibleDC(hdc); 102 103 bmp=CreateCompatibleBitmap(hdc,600,480); 104 SelectObject(mdc,bmp); 105 106 bg=(HBITMAP)LoadImageA(NULL,"bg.bmp",IMAGE_BITMAP,640,480,LR_LOADFROMFILE); 107 ball[0]=(HBITMAP)LoadImageA(NULL,"ball0.bmp",IMAGE_BITMAP,52,26,LR_LOADFROMFILE); 108 ball[1]=(HBITMAP)LoadImageA(NULL,"ball1.bmp",IMAGE_BITMAP,52,26,LR_LOADFROMFILE); 109 110 GetClientRect(hWnd,&rect);//取得內部窗口區(qū)域的大小; 111 112 x[0]=0;y[0]=100;vx[0]=5;vy[0]=0;ax[0]=0;ay[0]=1; 113 114 SetTimer(hWnd,1,10,NULL); 115 MyPaint(hdc); 116 117 return TRUE; 118 } 119 120 // 121 // 函數(shù): WndProc(HWND, UINT, WPARAM, LPARAM) 122 // 123 // 目的: 處理主窗口的消息。 124 // 125 // WM_COMMAND - 處理應用程序菜單 126 // WM_PAINT - 繪制主窗口 127 // WM_DESTROY - 發(fā)送退出消息并返回 128 // 129 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){ 130 int wmId, wmEvent; 131 PAINTSTRUCT ps; 132 133 switch (message){ 134 case WM_TIMER: 135 A:MyPaint(hdc); 136 break; 137 case WM_PAINT: 138 hdc = BeginPaint(hWnd, &ps); 139 goto A;// TODO: 在此添加任意繪圖代碼... 140 EndPaint(hWnd, &ps); 141 break; 142 case WM_DESTROY: 143 DeleteDC(mdc); 144 DeleteDC(bufdc); 145 DeleteObject(bg); 146 DeleteObject(ball[0]); 147 DeleteObject(ball[1]); 148 149 KillTimer(hWnd,1); 150 ReleaseDC(hWnd,hdc); 151 152 PostQuitMessage(0); 153 break; 154 default: 155 return DefWindowProc(hWnd, message, wParam, lParam); 156 } 157 return 0; 158 } 159 160 //MyPaint() 161 //1、窗口貼圖 162 //2、計算小球貼圖坐標并判斷小球是否碰撞窗口邊緣 163 void MyPaint(HDC hdc){ 164 SelectObject(bufdc,bg); 165 BitBlt(mdc,0,0,640,480,bufdc,0,0,SRCCOPY); 166 167 SelectObject(bufdc,ball[0]); 168 BitBlt(mdc,x[0],y[0],26,26,bufdc,26,0,SRCAND); 169 BitBlt(mdc,x[0],y[0],26,26,bufdc,0,0,SRCPAINT); 170 171 BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY); 172 173 for(int i=0;i<1;i++){ 174 //計算x軸方向坐標及速度 175 vx[i]+=ax[i]; 176 x[i]+=vx[i]; 177 178 if(x[i]<=0){ 179 x[i]=0; 180 vx[i]=-vx[i]; 181 }else if(x[i]>=rect.right-26){ 182 x[i]=rect.right-26; 183 vx[i]=-vx[i]; 184 } 185 //計算y軸方向坐標及速度 186 vy[i]+=ay[i]; 187 y[i]+=vy[i]; 188 189 if(y[i]<=0){ 190 y[i]=0; 191 vy[i]=-vy[i]-1; 192 }else if(y[i]>=rect.bottom-26){ 193 y[i]=rect.bottom-26; 194 vy[i]=-vy[i]-1; 195 } 196 } 197 }

本文轉自beautifulzzzz博客園博客,原文鏈接:http://www.cnblogs.com/zjutlitao/p/3735185.html,如需轉載請自行聯(lián)系原作者

總結

以上是生活随笔為你收集整理的[游戏模版21] Win32 物理引擎 能量守恒的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。