[游戏模版21] Win32 物理引擎 能量守恒
>_<: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 // 此代码模块中包含的函数的前向声明: 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 // 执行应用程序初始化: 39 if (!InitInstance (hInstance, nCmdShow)){ 40 return FALSE; 41 } 42 // 主消息循环: 43 while (GetMessage(&msg, NULL, 0, 0)){ 44 TranslateMessage(&msg); 45 DispatchMessage(&msg); 46 } 47 return (int) msg.wParam; 48 } 49 50 // 函数: 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 // 函数: InitInstance(HINSTANCE, int) 75 // 76 // 目的: 保存实例句柄并创建主窗口 77 // 78 // 注释: 79 // 80 // 在此函数中,我们在全局变量中保存实例句柄并 81 // 创建和显示主程序窗口。 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);//取得内部窗口区域的大小; 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 // 函数: WndProc(HWND, UINT, WPARAM, LPARAM) 122 // 123 // 目的: 处理主窗口的消息。 124 // 125 // WM_COMMAND - 处理应用程序菜单 126 // WM_PAINT - 绘制主窗口 127 // WM_DESTROY - 发送退出消息并返回 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 }