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