标签:des style blog class code c
>_<:Learning the physical engine
>_<: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 DWORD tPre,tNow,tCheck; 18 RECT rect;//窗口矩形 19 int x[2]; 20 int y[2]; 21 int vx[2]; 22 int vy[2]; 23 24 // 此代码模块中包含的函数的前向声明: 25 ATOM MyRegisterClass(HINSTANCE hInstance); 26 BOOL InitInstance(HINSTANCE, int); 27 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 28 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); 29 void MyPaint(HDC hdc); 30 31 32 int APIENTRY _tWinMain(HINSTANCE hInstance, 33 HINSTANCE hPrevInstance, 34 LPTSTR lpCmdLine, 35 int nCmdShow){ 36 37 MSG msg; 38 MyRegisterClass(hInstance); 39 // 执行应用程序初始化: 40 if (!InitInstance (hInstance, nCmdShow)){ 41 return FALSE; 42 } 43 // 主消息循环: 44 while (GetMessage(&msg, NULL, 0, 0)){ 45 TranslateMessage(&msg); 46 DispatchMessage(&msg); 47 } 48 return (int) msg.wParam; 49 } 50 51 // 函数: MyRegisterClass() 52 // 53 // 目的: 注册窗口类。 54 ATOM MyRegisterClass(HINSTANCE hInstance){ 55 WNDCLASSEX wcex; 56 57 wcex.cbSize = sizeof(WNDCLASSEX); 58 59 wcex.style = CS_HREDRAW | CS_VREDRAW; 60 wcex.lpfnWndProc = WndProc; 61 wcex.cbClsExtra = 0; 62 wcex.cbWndExtra = 0; 63 wcex.hInstance = hInstance; 64 wcex.hIcon = NULL; 65 wcex.hCursor = LoadCursor(NULL, IDC_ARROW); 66 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 67 wcex.lpszMenuName = "Beautifulzzzz"; 68 wcex.lpszClassName = "Beautifulzzzz"; 69 wcex.hIconSm = NULL; 70 71 return RegisterClassEx(&wcex); 72 } 73 74 // 75 // 函数: InitInstance(HINSTANCE, int) 76 // 77 // 目的: 保存实例句柄并创建主窗口 78 // 79 // 注释: 80 // 81 // 在此函数中,我们在全局变量中保存实例句柄并 82 // 创建和显示主程序窗口。 83 // 棋盘拼接以及调用InitGame()开始棋局 84 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){ 85 HBITMAP bmp; 86 hInst = hInstance; // 将实例句柄存储在全局变量中 87 88 hWnd = CreateWindow("Beautifulzzzz","Beautifulzzzz", WS_OVERLAPPEDWINDOW, 89 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); 90 91 if (!hWnd) 92 { 93 return FALSE; 94 } 95 96 MoveWindow(hWnd,10,10,600,450,true); 97 ShowWindow(hWnd, nCmdShow); 98 UpdateWindow(hWnd); 99 100 hdc=GetDC(hWnd); 101 mdc=CreateCompatibleDC(hdc); 102 bufdc=CreateCompatibleDC(hdc); 103 104 bmp=CreateCompatibleBitmap(hdc,600,480); 105 SelectObject(mdc,bmp); 106 107 bg=(HBITMAP)LoadImageA(NULL,"bg.bmp",IMAGE_BITMAP,640,480,LR_LOADFROMFILE); 108 ball[0]=(HBITMAP)LoadImageA(NULL,"ball0.bmp",IMAGE_BITMAP,52,26,LR_LOADFROMFILE); 109 ball[1]=(HBITMAP)LoadImageA(NULL,"ball1.bmp",IMAGE_BITMAP,52,26,LR_LOADFROMFILE); 110 111 GetClientRect(hWnd,&rect);//取得内部窗口区域的大小; 112 113 x[0]=50;y[0]=50;vx[0]=4;vy[0]=4; 114 x[1]=380;y[1]=380;vx[1]=-4;vy[1]=-4; 115 116 SetTimer(hWnd,1,10,NULL); 117 MyPaint(hdc); 118 119 return TRUE; 120 } 121 122 // 123 // 函数: WndProc(HWND, UINT, WPARAM, LPARAM) 124 // 125 // 目的: 处理主窗口的消息。 126 // 127 // WM_COMMAND - 处理应用程序菜单 128 // WM_PAINT - 绘制主窗口 129 // WM_DESTROY - 发送退出消息并返回 130 // 131 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){ 132 int wmId, wmEvent; 133 PAINTSTRUCT ps; 134 135 switch (message){ 136 case WM_TIMER: 137 A:MyPaint(hdc); 138 break; 139 case WM_PAINT: 140 hdc = BeginPaint(hWnd, &ps); 141 goto A;// TODO: 在此添加任意绘图代码... 142 EndPaint(hWnd, &ps); 143 break; 144 case WM_DESTROY: 145 DeleteDC(mdc); 146 DeleteDC(bufdc); 147 DeleteObject(bg); 148 DeleteObject(ball[0]); 149 DeleteObject(ball[1]); 150 151 KillTimer(hWnd,1); 152 ReleaseDC(hWnd,hdc); 153 154 PostQuitMessage(0); 155 break; 156 default: 157 return DefWindowProc(hWnd, message, wParam, lParam); 158 } 159 return 0; 160 } 161 162 //MyPaint() 163 //1、窗口贴图 164 //2、计算小球贴图坐标并判断小球是否碰撞窗口边缘 165 void MyPaint(HDC hdc){ 166 SelectObject(bufdc,bg); 167 BitBlt(mdc,0,0,640,480,bufdc,0,0,SRCCOPY); 168 169 SelectObject(bufdc,ball[0]); 170 BitBlt(mdc,x[0],y[0],26,26,bufdc,26,0,SRCAND); 171 BitBlt(mdc,x[0],y[0],26,26,bufdc,0,0,SRCPAINT); 172 173 SelectObject(bufdc,ball[1]); 174 BitBlt(mdc,x[1],y[1],26,26,bufdc,26,0,SRCAND); 175 BitBlt(mdc,x[1],y[1],26,26,bufdc,0,0,SRCPAINT); 176 177 BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY); 178 179 for(int i=0;i<2;i++){ 180 //计算x轴方向贴图坐标与速度 181 x[i]+=vx[i]; 182 if(x[i]<=0){ 183 x[i]=0; 184 vx[i]=-vx[i]; 185 }else if(x[i]>=rect.right-26){ 186 x[i]=rect.right-26; 187 vx[i]=-vx[i]; 188 } 189 190 //计算y轴方向坐标及速度 191 y[i]+=vy[i]; 192 if(y[i]<=0){ 193 y[i]=0; 194 vy[i]=-vy[i]; 195 }else if(y[i]>=rect.bottom-26){ 196 y[i]=rect.bottom-26; 197 vy[i]=-vy[i]; 198 } 199 } 200 201 if((x[0]-x[1])*(x[0]-x[1])+(y[0]-y[1])*(y[0]-y[1])<=1000){ 202 vx[0]=-vx[0];vy[0]=-vy[0]; 203 vx[1]=-vx[1];vy[1]=-vy[1]; 204 } 205 }
[游戏模版19] Win32 物理引擎 匀速运动,布布扣,bubuko.com
标签:des style blog class code c
原文地址:http://www.cnblogs.com/zjutlitao/p/3735163.html