[游戏模版15] Win32 飞机射击

 

>_<:Only give you the code,try to understand it!

>_<:picture resource

[游戏模版15] Win32 飞机射击
  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 <string>
  9 
 10 
 11 //定义结构,飞机子弹
 12 struct BULLET{
 13     int x,y;
 14     bool exist;
 15 };
 16 
 17 // 全局变量:
 18 HINSTANCE hInst;                                // 当前实例
 19 
 20 HBITMAP bg,ship,bullet;//背景图,飞机图,子弹图
 21 HDC hdc,mdc,bufdc;
 22 HWND hWnd;
 23 int x,y,nowX,nowY;//鼠标坐标,飞机坐标(贴图坐标)
 24 int w=0,bcount;//滚动背景所要剪切的宽度,子弹数目
 25 BULLET b[30];//存储飞机发出的子弹
 26 
 27 // 此代码模块中包含的函数的前向声明:
 28 ATOM                MyRegisterClass(HINSTANCE hInstance);
 29 BOOL                InitInstance(HINSTANCE, int);
 30 LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
 31 INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
 32 void                MyPaint(HDC hdc);
 33 
 34 int APIENTRY _tWinMain(HINSTANCE hInstance,
 35                      HINSTANCE hPrevInstance,
 36                      LPTSTR    lpCmdLine,
 37                      int       nCmdShow){
 38 
 39     MSG msg;
 40     MyRegisterClass(hInstance);
 41     // 执行应用程序初始化:
 42     if (!InitInstance (hInstance, nCmdShow)){
 43         return FALSE;
 44     }
 45     // 主消息循环:
 46     while (GetMessage(&msg, NULL, 0, 0)){
 47         TranslateMessage(&msg);
 48         DispatchMessage(&msg);
 49     }
 50     return (int) msg.wParam;
 51 }
 52 
 53 //  函数: MyRegisterClass()
 54 //
 55 //  目的: 注册窗口类。
 56 ATOM MyRegisterClass(HINSTANCE hInstance){
 57     WNDCLASSEX wcex;
 58 
 59     wcex.cbSize = sizeof(WNDCLASSEX);
 60 
 61     wcex.style            = CS_HREDRAW | CS_VREDRAW;
 62     wcex.lpfnWndProc    = WndProc;
 63     wcex.cbClsExtra        = 0;
 64     wcex.cbWndExtra        = 0;
 65     wcex.hInstance        = hInstance;
 66     wcex.hIcon            = NULL;
 67     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
 68     wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
 69     wcex.lpszMenuName    = "Beautifulzzzz";
 70     wcex.lpszClassName    = "Beautifulzzzz";
 71     wcex.hIconSm        = NULL;
 72 
 73     return RegisterClassEx(&wcex);
 74 }
 75 
 76 //   函数: InitInstance(HINSTANCE, int)
 77 //
 78 //   目的: 保存实例句柄并创建主窗口
 79 //
 80 //   注释:
 81 //
 82 //        在此函数中,我们在全局变量中保存实例句柄并
 83 //        创建和显示主程序窗口。
 84 //        1.设定飞机的初始位置
 85 //        2.设定鼠标位置及隐藏
 86 //        3.设定鼠标光标移动区域
 87 //
 88 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){
 89 
 90    HBITMAP bmp;
 91    POINT pt,lt,rb;
 92    RECT rect;
 93 
 94    hInst = hInstance; // 将实例句柄存储在全局变量中
 95 
 96    hWnd = CreateWindow("Beautifulzzzz","Beautifulzzzz", WS_OVERLAPPEDWINDOW,
 97       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
 98 
 99    if (!hWnd)
100    {
101       return FALSE;
102    }
103 
104    MoveWindow(hWnd,10,10,640,480,true);
105    ShowWindow(hWnd, nCmdShow);
106    UpdateWindow(hWnd);
107 
108    hdc=GetDC(hWnd);
109    mdc=CreateCompatibleDC(hdc);
110    bufdc=CreateCompatibleDC(hdc);
111 
112    bmp=CreateCompatibleBitmap(hdc,640,480);
113    SelectObject(mdc,bmp);
114 
115    bg=(HBITMAP)LoadImageA(NULL,"bg.bmp",IMAGE_BITMAP,648,480,LR_LOADFROMFILE);
116    ship=(HBITMAP)LoadImageA(NULL,"ship.bmp",IMAGE_BITMAP,100,148,LR_LOADFROMFILE);
117    bullet=(HBITMAP)LoadImageA(NULL,"bullet.bmp",IMAGE_BITMAP,10,20,LR_LOADFROMFILE);
118 
119    x=300;
120    y=300;
121    nowX=300;
122    nowY=300;
123 
124    //设定鼠标光标位置
125    pt.x=300;
126    pt.y=300;
127    ClientToScreen(hWnd,&pt);
128    SetCursorPos(pt.x,pt.y);
129 
130    ShowCursor(false);//隐藏鼠标光标
131 
132    //限制鼠标光标移动区域
133    GetClientRect(hWnd,&rect);
134    lt.x=rect.left;
135    lt.y=rect.top;
136    rb.x=rect.right;
137    rb.y=rect.bottom;
138    ClientToScreen(hWnd,&lt);
139    ClientToScreen(hWnd,&rb);
140    rect.left=lt.x;
141    rect.top=lt.y;
142    rect.right=rb.x;
143    rect.bottom=rb.y;
144    ClipCursor(&rect);
145 
146 
147    SetTimer(hWnd,1,50,NULL);
148    MyPaint(hdc);
149 
150    return TRUE;
151 }
152 
153 //
154 //  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
155 //
156 //  目的: 处理主窗口的消息。
157 //
158 //  WM_COMMAND    - 处理应用程序菜单
159 //  WM_PAINT    - 绘制主窗口
160 //  WM_DESTROY    - 发送退出消息并返回
161 //
162 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
163     int i;
164     int wmId, wmEvent;
165     PAINTSTRUCT ps;
166 
167     switch (message)
168     {
169     case WM_KEYDOWN:          //按下消息
170         if(wParam==VK_ESCAPE) //按下[esc]
171             PostQuitMessage(0);
172         break;
173     case WM_LBUTTONDOWN:      //单击鼠标左键消息
174         for(i=0;i<30;i++)
175         {
176             if(!b[i].exist)
177             {
178                 b[i].x=nowX;
179                 b[i].y=nowY+30;
180                 b[i].exist=true;
181                 bcount++;
182                 break;
183             }
184         }
185     case WM_MOUSEMOVE:
186         x=LOWORD(lParam);    //取得鼠标x坐标
187         if(x>530)
188             x=530;
189         else if(x<0)
190             x=0;
191 
192         y=HIWORD(lParam);
193         if(y>380)
194             y=380;
195         else if(y<0)
196             y=0;
197 
198         break;
199     case WM_TIMER:
200         A:MyPaint(hdc);
201         break;
202     case WM_PAINT:
203         hdc = BeginPaint(hWnd, &ps);
204         // TODO: 在此添加任意绘图代码...
205         EndPaint(hWnd, &ps);
206         break;
207     case WM_DESTROY:
208         ClipCursor(NULL);//回复鼠标移动区域
209 
210         DeleteDC(mdc);
211         DeleteDC(bufdc);
212         DeleteObject(bg);
213         DeleteObject(bullet);
214         DeleteObject(ship);
215         ReleaseDC(hWnd,hdc);
216 
217         PostQuitMessage(0);
218         break;
219     default:
220         return DefWindowProc(hWnd, message, wParam, lParam);
221     }
222     return 0;
223 }
224 
225 //1.设定飞机坐标并进行贴图
226 //2.设定所有子弹坐标并进行贴图
227 //3.显示真正的鼠标坐标所在的坐标
228 void MyPaint(HDC hdc){
229     char str[20]="";
230     int i;
231 
232     //贴上背景图
233     SelectObject(bufdc,bg);
234     BitBlt(mdc,0,0,w,480,bufdc,640-w,0,SRCCOPY);
235     BitBlt(mdc,w,0,640-w,480,bufdc,0,0,SRCCOPY);
236 
237 
238     //飞机坐标向鼠标坐标位置靠近
239     if(nowX<x){
240         nowX+=10;
241         if(nowX>x)
242             nowX=x;
243     }else{
244         nowX-=10;
245         if(nowX<x)
246             nowX=x;
247     }
248 
249     if(nowY<y){
250         nowY-=10;
251         if(nowY<y)
252             nowY=y;
253     }else{
254         nowY+=10;
255         if(nowY>y)
256             nowY=y;
257     }
258 
259     //贴上飞机图
260     SelectObject(bufdc,ship);
261     BitBlt(mdc,nowX,nowY,100,74,bufdc,0,74,SRCAND);
262     BitBlt(mdc,nowX,nowY,100,74,bufdc,0,0,SRCPAINT);
263 
264     SelectObject(bufdc,bullet);
265     if(bcount!=0)
266         for(i=0;i<30;i++)
267             if(b[i].exist){
268                 //贴上子弹图
269                 BitBlt(mdc,b[i].x,b[i].y,10,10,bufdc,0,10,SRCAND);
270                 BitBlt(mdc,b[i].x,b[i].y,10,10,bufdc,0,0,SRCPAINT);
271 
272                 b[i].x-=10;
273                 if(b[i].x<0){
274                     bcount--;
275                     b[i].exist=false;
276                 }
277             }
278 
279     //显示鼠标坐标
280     sprintf(str,"x坐标:  %d   ",x);
281     TextOutA(mdc,0,0,str,strlen(str));
282     sprintf(str,"y坐标:  %d   ",y);
283     TextOutA(mdc,0,20,str,strlen(str));
284 
285     BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY);
286 
287     w+=10;
288     if(w==640)
289         w=0;
290 }
[游戏模版15] Win32 飞机射击

 

[游戏模版15] Win32 飞机射击,布布扣,bubuko.com

[游戏模版15] Win32 飞机射击

上一篇:shell 参数


下一篇:性感的vim快捷键