C++飛機大戰


  1 #include<windows.h>
  2 #include"resource.h"
  3 #include<stdlib.h>
  4 #include<time.h>
  5 #include<stdio.h>
  6   
  7 #define TIMER_DIREN 101      //定義定時器
  8 #define TIMER_DIRENMOVE 102
  9 #define TIMER_ZIDAN 103
 10 #define TIMER_DIRENRELEASE 104
 11   
 12 typedef struct Node    //敵人,自己,子彈結構體
 13 {
 14   int x;
 15   int y;
 16   struct Node *pnext;
 17 }DiRen,FeiJi,ZiDan;
 18 void ZaoDiRen();                //造敵人
 19 void ShowDiRen(DiRen *pHead,HWND hWnd);     //顯示敵人
 20 void ZaoZiDan();                //造子彈
 21 void ShowZiDan(ZiDan *pHead,HWND hWnd);     //顯示子彈
 22 void DiRenMove(DiRen *pHead);          //敵人移動
 23 void ZiDanMove(DiRen *pHead);          //子彈移動
 24 void shoot(HWND hWnd,FeiJi *ziji,DiRen **diren,ZiDan **zidan);//判斷是否射中   
 25 void ReleaseDiren(DiRen **pHead);        //釋放出去的敵人
 26 void ReleaseZidan(ZiDan **pHead);        //釋放出去的子彈
 27 void ZaoZiJi(HWND hWnd);            //造自己
 28 LRESULT CALLBACK pp(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);//回調函數
 29 int __stdcall WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
 30 {
 31   WNDCLASSEX wc;
 32   HWND hWnd;
 33   MSG msg;
 34   wc.hInstance=hInstance;
 35   wc.cbClsExtra=0;
 36   wc.cbSize=sizeof(WNDCLASSEX);
 37   wc.cbWndExtra=0;
 38   wc.hIcon=NULL ;
 39   wc.hCursor=NULL ;
 40   wc.hIconSm=NULL;
 41   wc.lpfnWndProc=pp;
 42   wc.lpszClassName="hello";
 43   wc.lpszMenuName=NULL;
 44   wc.style=CS_HREDRAW|CS_VREDRAW | CS_OWNDC ;
 45   wc.hbrBackground=(HBRUSH)5;
 46   RegisterClassEx(&wc);
 47   hWnd=CreateWindow("hello","world", WS_OVERLAPPEDWINDOW,100,100,600,600,NULL,NULL,hInstance,NULL);
 48   ShowWindow(hWnd,nCmdShow);
 49   while(GetMessage(&msg,NULL,0,0))
 50   {
 51     TranslateMessage(&msg);
 52     DispatchMessage(&msg);
 53   }
 54   return 0;
 55 }
 56 DiRen *pDiRen=NULL;  //敵人
 57 ZiDan *pZiDan=NULL;  //子彈
 58 FeiJi *pZiJi=NULL;   //自己
 59 static int score=0;   //分數
 60 static char sco[20];  //裝分數的字符竄
 61 LRESULT CALLBACK pp(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
 62 {
 63   int i=1,  //
 64     jscore;
 65   HDC hdc;
 66   HDC memdc;
 67   HBITMAP hbm;
 68   BITMAP bminfo;
 69   switch(msg)
 70   {
 71   case WM_TIMER:   //定時器
 72     hdc=GetDC(hWnd); //得到設備句柄
 73     hbm=LoadBitmap(GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BITMAP4));//載入背景位圖
 74     GetObject(hbm, sizeof(bminfo), &bminfo); 
 75     memdc=CreateCompatibleDC(hdc);
 76     SelectObject(memdc,hbm);
 77     BitBlt(hdc,0,0,600,600,memdc,0,0,SRCCOPY);
 78     /*itoa(score,sco,10);*/
 79     sprintf(sco,"%d",score);  //將分數裝入字符竄
 80     jscore=score;
 81     while((jscore=jscore/10)>0) //判斷分數有幾位
 82       i++;
 83     TextOut(hdc,0,0,"分數",4);
 84     TextOut(hdc,30,0,sco,i); //顯示分數
 85     DeleteDC(memdc);
 86     ReleaseDC(hWnd,hdc);  //釋放句柄
 87     DeleteObject(hbm);
 88     ZaoZiJi(hWnd);     //造自己
 89     if(TIMER_ZIDAN==wParam)  //定時器101
 90     {
 91       ZiDanMove(pZiDan);   //子彈移動
 92       ReleaseZidan(&pZiDan); //釋放出屏幕的子彈
 93     }
 94     else if( TIMER_DIREN==wParam) //定時器102
 95     { 
 96       ZaoDiRen();       //造敵人    
 97     }
 98     else if(TIMER_DIRENRELEASE==wParam)  //定時器103
 99     {
100       ReleaseDiren(&pDiRen);  //釋放出屏幕的敵人
101     }
102     ShowDiRen(pDiRen,hWnd);       //顯示敵人
103     DiRenMove(pDiRen);       //敵人移動
104     ShowZiDan(pZiDan,hWnd);     //顯示子彈
105     shoot(hWnd,pZiJi,&pDiRen,&pZiDan);   //是否射中
106     break;
107   case WM_CLOSE:    //關閉
108     PostQuitMessage(0);
109     break;
110   case WM_KEYDOWN:    //判斷按鍵
111     switch(wParam)   
112     {
113     case VK_LEFT:   //左移
114       if(pZiJi->x>0)
115         pZiJi->x-=20;
116       break;
117     case VK_RIGHT:  //右移
118       if(pZiJi->x<530)
119       pZiJi->x+=20;
120       break;
121     case VK_UP:   //上移
122       if(pZiJi->y>0)
123       pZiJi->y-=20;
124       break;
125     case VK_DOWN:  //下移
126       if(pZiJi->y<520)
127       pZiJi->y+=20;
128       break;
129     case VK_SPACE:  //空格發射子彈
130       ZaoZiDan();
131       break;
132     }
133     break;
134   case WM_CREATE:   //創建
135     srand(time(NULL));  
136     pZiJi=(struct Node*)malloc(sizeof(struct Node));
137     pZiJi->x=200;     //自己的x
138     pZiJi->y=500;     //自己的y
139     SetTimer(hWnd,TIMER_DIREN,1000,NULL);  //設置定時器
140     SetTimer(hWnd,TIMER_DIRENMOVE,200,NULL);
141     SetTimer(hWnd,TIMER_ZIDAN,100,NULL);
142     SetTimer(hWnd,TIMER_DIRENRELEASE,300,NULL);
143     break;
144   }
145   return DefWindowProc(hWnd,msg,wParam,lParam);
146 }
147   
148   
149 void ZaoDiRen()  //造子彈
150 {
151   DiRen *u;
152   u=(struct Node*)malloc(sizeof(struct Node)); 
153   u->x=rand()%550;   //子彈的x隨機出現
154   u->y=-10;      //出現的縱坐標固定
155   u->pnext=NULL;
156   if(NULL==pDiRen)  
157   {
158     pDiRen=u;
159   }
160   else
161   {
162     u->pnext=pDiRen;   //將新產生的鏈表放在頭
163     pDiRen=u;
164   
165   }
166 }
167 void ShowDiRen(struct Node *pHead,HWND hWnd)  //顯示敵人
168 {
169   HDC hdc;
170   HDC memdc;
171   HBITMAP hbm;
172   BITMAP bminfo;
173   hdc=GetDC(hWnd);
174   hbm=LoadBitmap(GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BITMAP1));//載入敵人位圖
175   GetObject(hbm, sizeof(bminfo), &bminfo); 
176   memdc=CreateCompatibleDC(hdc);
177   SelectObject(memdc,hbm);
178   while(pHead!=NULL) //敵人鏈表不為空,顯示敵機
179   { 
180     BitBlt(hdc,pHead->x,pHead->y,40,40,memdc,0,0,SRCCOPY);
181     pHead=pHead->pnext; 
182   }
183   DeleteDC(memdc);
184   ReleaseDC(hWnd,hdc);
185   DeleteObject(hbm);
186 }
187 void ZaoZiJi(HWND hWnd)
188 {
189   HDC hdc;
190   HDC memdc;
191   HBITMAP hbm;
192   BITMAP bminfo;
193   hdc=GetDC(hWnd);
194   hbm=LoadBitmap(GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BITMAP3));//載入自己的位圖
195   GetObject(hbm, sizeof(bminfo), &bminfo); 
196   memdc=CreateCompatibleDC(hdc);
197   SelectObject(memdc,hbm);
198   BitBlt(hdc,pZiJi->x,pZiJi->y,40,40,memdc,0,0,SRCCOPY); //顯示自己
199   DeleteDC(memdc);
200   ReleaseDC(hWnd,hdc);
201   DeleteObject(hbm);
202 }
203 void ZaoZiDan()   //造子彈
204 {
205   ZiDan *u;
206   u=(ZiDan*)malloc(sizeof(ZiDan));
207   u->x=pZiJi->x+15;
208   u->y=pZiJi->y+10;
209   u->pnext=NULL;
210   if(pZiDan==NULL)
211   {
212     pZiDan=u;
213   }  
214   else
215   {
216     u->pnext=pZiDan;  //將子彈放在鏈表頭
217     pZiDan=u;
218   }
219 }
220 void ShowZiDan(ZiDan *pHead,HWND hWnd) //顯示子彈
221 {
222   HDC hdc;
223   HDC memdc;
224   HBITMAP hbm;
225   BITMAP bminfo;
226   hdc=GetDC(hWnd);
227   hbm=LoadBitmap(GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BITMAP2)); //插入子彈位圖
228   GetObject(hbm, sizeof(bminfo), &bminfo); 
229   memdc=CreateCompatibleDC(hdc);
230   SelectObject(memdc,hbm);
231   while(pHead!=NULL)  //子彈鏈表不為空,顯示子彈
232   {
233     /*Ellipse(hdc,pHead->x,pHead->y,pHead->x+5,pHead->y+5);*/
234     BitBlt(hdc,pHead->x,pHead->y,10,10,memdc,0,0,SRCCOPY);
235     pHead=pHead->pnext;
236   }  
237   DeleteDC(memdc);
238   ReleaseDC(hWnd,hdc);
239   DeleteObject(hbm);
240 }
241   
242 void DiRenMove(DiRen *pHead)  //敵人移動
243 {
244   while(pHead!=NULL)  //鏈表不為空,敵人移動
245   {  
246     if(score<500)
247     {
248       pHead->y+=10;
249       pHead=pHead->pnext; 
250     }
251     else
252     {
253       pHead->y+=20;
254       pHead=pHead->pnext;
255     }
256   }
257 }
258 void ZiDanMove(DiRen *pHead)  //子彈移動
259 {
260   while(pHead!=NULL)  //鏈表不為空子彈移動
261   {
262     pHead->y-=20;
263     pHead=pHead->pnext; 
264   }
265 }
266   
267 void shoot(HWND hWnd,FeiJi *ziji,DiRen **diren,ZiDan **zidan) //判斷是否中
268 {
269   DiRen *js1=*diren;
270   ZiDan *js2=*zidan;
271   int n = 1;
272   while(js1!=NULL) //判斷自己是否撞機
273   {
274     //撞擊釋放定時器游戲結束
275     if((ziji->x-js1->x<30&&ziji->x-js1->x>-38)&&(ziji->y-js1->y<25&&ziji->y-js1->y>-38))
276     {
277       KillTimer(hWnd,TIMER_DIREN);
278       KillTimer(hWnd,TIMER_ZIDAN);
279       KillTimer(hWnd,TIMER_DIRENMOVE);
280       KillTimer(hWnd,TIMER_DIRENRELEASE);
281       MessageBox(hWnd,"You Lose","窗口",MB_OK);
282       PostQuitMessage(0);
283       break;
284     }
285     else
286       js1=js1->pnext;  //沒有判斷下一個敵機
287   } 
288   js1=*diren;  //敵機回到頭
289   while((js1=*diren)!=NULL)  //判斷敵人是否為空
290   {
291     zidan = &pZiDan;  
292     n = 0;
293     while((js2=*zidan)!=NULL) //判斷子彈是否為空
294     {    
295       //敵機中彈
296       if((js2->x - js1->x <= 40&&js2->x - js1->x>=-5)&&(js2->y - js1->y <= 40&&js2->y - js1->y>=-8))
297       {
298         score+=100;
299         n = 1;
300         *zidan = js2->pnext;
301         if(js1->pnext!=NULL) //鏈表下節不為空,指向下一個釋放中彈的飛機子彈
302         {
303           *diren = js1->pnext;
304           diren = &pDiRen;
305           free(js1);
306           free(js2);
307         }
308         else
309           *diren = NULL;  
310         break;
311       }
312       else
313       {
314         zidan = &js2->pnext;  //沒中看下一個
315       }
316     }
317     if(n != 1)  //判斷是否是中彈出來的
318     {
319       diren = &js1->pnext; 
320     }
321   }
322 }
323 void ReleaseDiren(DiRen **pHead) //釋放飛出屏幕的敵人
324 {
325   DiRen *js=*pHead;
326   while((js=*pHead)!=NULL)
327   {
328     if(js->y>600)    //飛出屏幕釋放
329     {
330       *pHead=js->pnext;
331       free(js);
332     }
333     else
334     {
335       pHead = &js->pnext;  //看下一個
336     }
337   }
338 }
339 void ReleaseZidan(ZiDan **pHead)  //釋放子彈
340 {
341   ZiDan *js=*pHead;
342   while((js=*pHead)!=NULL)
343   {
344     if(js->y<0)    //飛出的子彈釋放
345     {
346       *pHead=js->pnext;  
347       free(js);
348     }
349     else
350       pHead=&js->pnext;  //沒飛出看下一個
351   }
352 }

 

 

 

rs:

//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by 打飛機.rc
//
#define IDB_BITMAP1                     101
#define IDB_BITMAP2                     102
#define IDB_BITMAP3                     103
#define IDB_BITMAP4                     104
#define IDB_BITMAP5                     105
#define IDB_BITMAP6                     108
#define IDB_BITMAP7                     109

// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        110
#define _APS_NEXT_COMMAND_VALUE         40001
#define _APS_NEXT_CONTROL_VALUE         1001
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM