[游戏模版16] Win32 飞机射击 敌人追踪

 

>_<:AI introduction.

>_<:According the plane position (nowX,nowY) relative to birds' position (p[i].x,p[i].y)  automaticly change birds' position.

 1 //贴上小鸟
 2 SelectObject(bufdc,bird);
 3 for(i=0;i<3;i++){
 4     if(rand()%3!=1){
 5         if(p[i].y>nowY-16)
 6             p[i].y-=5;
 7         else
 8             p[i].y+=5;
 9 
10         if(p[i].x>nowX-25)
11             p[i].x-=5;
12         else
13             p[i].x+=5;
14     }
15     if(p[i].x>nowX-25){   //判断小鸟移动方向
16         BitBlt(mdc,p[i].x,p[i].y,61,61,bufdc,61,61,SRCAND);
17         BitBlt(mdc,p[i].x,p[i].y,61,61,bufdc,0,61,SRCPAINT);
18     }else{
19         BitBlt(mdc,p[i].x,p[i].y,61,61,bufdc,61,0,SRCAND);
20         BitBlt(mdc,p[i].x,p[i].y,61,61,bufdc,0,0,SRCPAINT);
21     }
22 }

>_<:resource

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

 

posted @ 2014-05-17 15:12  beautifulzzzz  阅读(878)  评论(0编辑  收藏  举报