>_<:Compared with previous talk,there will be taking about how to create an accelerated speed.Only a little change in the MyPaint(...) function.
>_<: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=3;//时间 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]=50;y[0]=50;vx[0]=4;vy[0]=4;ax[0]=0;ay[0]=2; 113 x[1]=380;y[1]=380;vx[1]=-4;vy[1]=-4;ax[1]=0;ay[1]=0; 114 115 SetTimer(hWnd,1,10,NULL); 116 MyPaint(hdc); 117 118 return TRUE; 119 } 120 121 // 122 // 函数: WndProc(HWND, UINT, WPARAM, LPARAM) 123 // 124 // 目的: 处理主窗口的消息。 125 // 126 // WM_COMMAND - 处理应用程序菜单 127 // WM_PAINT - 绘制主窗口 128 // WM_DESTROY - 发送退出消息并返回 129 // 130 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){ 131 int wmId, wmEvent; 132 PAINTSTRUCT ps; 133 134 switch (message){ 135 case WM_TIMER: 136 A:MyPaint(hdc); 137 break; 138 case WM_PAINT: 139 hdc = BeginPaint(hWnd, &ps); 140 goto A;// TODO: 在此添加任意绘图代码... 141 EndPaint(hWnd, &ps); 142 break; 143 case WM_DESTROY: 144 DeleteDC(mdc); 145 DeleteDC(bufdc); 146 DeleteObject(bg); 147 DeleteObject(ball[0]); 148 DeleteObject(ball[1]); 149 150 KillTimer(hWnd,1); 151 ReleaseDC(hWnd,hdc); 152 153 PostQuitMessage(0); 154 break; 155 default: 156 return DefWindowProc(hWnd, message, wParam, lParam); 157 } 158 return 0; 159 } 160 161 //MyPaint() 162 //1、窗口贴图 163 //2、计算小球贴图坐标并判断小球是否碰撞窗口边缘 164 void MyPaint(HDC hdc){ 165 SelectObject(bufdc,bg); 166 BitBlt(mdc,0,0,640,480,bufdc,0,0,SRCCOPY); 167 168 SelectObject(bufdc,ball[0]); 169 BitBlt(mdc,x[0],y[0],26,26,bufdc,26,0,SRCAND); 170 BitBlt(mdc,x[0],y[0],26,26,bufdc,0,0,SRCPAINT); 171 172 SelectObject(bufdc,ball[1]); 173 BitBlt(mdc,x[1],y[1],26,26,bufdc,26,0,SRCAND); 174 BitBlt(mdc,x[1],y[1],26,26,bufdc,0,0,SRCPAINT); 175 176 BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY); 177 178 for(int i=0;i<2;i++){ 179 //计算x轴方向贴图坐标与速度 180 vx[i]+=t*ax[i]; 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 vy[i]+=t*ay[i]; 192 y[i]+=vy[i]; 193 if(y[i]<=0){ 194 y[i]=0; 195 vy[i]=-vy[i]; 196 }else if(y[i]>=rect.bottom-26){ 197 y[i]=rect.bottom-26; 198 vy[i]=-vy[i]; 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 } 206 }