小小贪吃蛇
1 /*------------------------------------------------------------ 2 this program design by Bug 3 ------------------------------------------------------------*/ 4 5 #include <WINDOWS.H> 6 #include <STDIO.H> 7 #include <STRING.H> 8 #include <TIME.H> 9 #include <STDLIB.H> 10 11 12 13 #define DRAFRAMENUM 62 //Drawn framework of rect ' number 14 #define ID_TIME 1 //Time ID 15 #define RECTDISTANCE 10 //Every rectangular size 16 #define MAXSNAKELEN 60 //Limit snake's length 17 #define BOSS 92 18 19 20 21 typedef struct POSITION 22 { 23 int xLeft ; 24 int yTop ; 25 int xRight ; 26 int yBottom ; 27 28 }BODYPOS,BEANPOS, * PBODYPOS; 29 30 BODYPOS bodyPos[60] = { 31 {300,300,310,310}, 32 {310,300,320,310}, 33 {320,300,330,310}, 34 {330,300,340,310} 35 }; //initialize body of snake 36 BEANPOS beanPos; 37 38 static int snakeLen = 4,Pause = 0,over = 0,beanMiss = 0; 39 40 void bean(void); 41 int feed(void); 42 void moveJudge(int,int,int,int,PBODYPOS); 43 int gameOver(); 44 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; 45 46 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow) 47 { 48 static TCHAR szAppName[] = TEXT ("snake") ; 49 HWND hwnd ; 50 MSG msg ; 51 WNDCLASS wndclass ; 52 int temp; 53 temp = feed(); 54 beanPos.xLeft = temp; 55 beanPos.xRight = temp + RECTDISTANCE; 56 beanPos.yTop = temp; 57 beanPos.yBottom= temp + RECTDISTANCE; 58 59 wndclass.style = CS_HREDRAW | CS_VREDRAW ; 60 wndclass.lpfnWndProc = WndProc ; 61 wndclass.cbClsExtra = 0 ; 62 wndclass.cbWndExtra = 0 ; 63 wndclass.hInstance = hInstance ; 64 wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; 65 wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; 66 wndclass.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH) ; 67 wndclass.lpszMenuName = NULL ; 68 wndclass.lpszClassName = szAppName ; 69 70 if (!RegisterClass (&wndclass)) 71 { 72 MessageBox (NULL, TEXT ("This program requires Windows NT!"), 73 szAppName, MB_ICONERROR) ; 74 return 0 ; 75 } 76 77 hwnd = CreateWindow (szAppName, // window class name 78 TEXT ("Snake"), // window caption 79 WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX& ~WS_THICKFRAME, // window style 80 350, // initial x position 81 50, // initial y position 82 736, // initial x size 83 648, // initial y size 84 NULL, // parent window handle 85 NULL, // window menu handle 86 hInstance, // program instance handle 87 NULL) ; // creation parameters 88 89 ShowWindow (hwnd, iCmdShow) ; 90 UpdateWindow (hwnd) ; 91 92 while (GetMessage (&msg, NULL, 0, 0)) 93 { 94 TranslateMessage (&msg) ; 95 DispatchMessage (&msg) ; 96 } 97 return msg.wParam ; 98 } 99 100 LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 101 { 102 HDC hdc ; 103 PAINTSTRUCT ps ; 104 RECT rect ; 105 HBRUSH hBrush; 106 HFONT hFont; 107 LOGFONT lf; 108 static int upButton,downButton,leftButton,rightButton; 109 int i,k,xRight,xLeft,yTop,yBottom; 110 char szBuff[10][6]; 111 112 113 xRight = 0 ; 114 yTop = 0 ; 115 xLeft = 10 ; 116 yBottom = 10 ; 117 rect.left = 10 ; 118 rect.top = 10 ; 119 rect.right = 610 ; 120 rect.bottom= 610 ; 121 122 123 lf.lfHeight = 60; 124 lf.lfWidth = 60; 125 lf.lfWeight = 1000; 126 lf.lfEscapement = 0; 127 lf.lfOrientation = 0; 128 lf.lfItalic = 0; 129 lf.lfUnderline = 0; 130 lf.lfStrikeOut = 0; 131 lf.lfCharSet = 1; 132 133 snakeLen=min(snakeLen,MAXSNAKELEN); //insure the snake's length litter than MAXSNAKELEN 134 135 switch (message) 136 { 137 case WM_CREATE: 138 SetTimer(hwnd,ID_TIME,500,NULL); //send WM_TIME message every 0.5 minutes 139 break; 140 case WM_TIMER: 141 InvalidateRect(hwnd,NULL,TRUE); 142 break; 143 case WM_KEYDOWN: 144 switch (wParam) //remember the state of key 145 { 146 case VK_UP: 147 upButton = 1 ; 148 downButton = 0 ; 149 leftButton = 0 ; 150 rightButton = 0 ; 151 break; 152 case VK_DOWN: 153 upButton = 0 ; 154 downButton = 1 ; 155 leftButton = 0 ; 156 rightButton = 0 ; 157 break; 158 case VK_LEFT: 159 upButton = 0 ; 160 downButton = 0 ; 161 leftButton = 1 ; 162 rightButton = 0 ; 163 break; 164 case VK_RIGHT: 165 upButton = 0 ; 166 downButton = 0 ; 167 leftButton = 0 ; 168 rightButton = 1 ; 169 break; 170 case VK_ESCAPE: //pause function 171 Pause = 1; 172 InvalidateRect(hwnd,&rect,TRUE); 173 KillTimer(hwnd,ID_TIME); 174 break; 175 case 'R': 176 if (Pause && !over) 177 { 178 SendMessage(hwnd,WM_CREATE,wParam,lParam); 179 Pause = 0; 180 } 181 break; 182 case VK_SPACE: //snake accelerate 183 if (!Pause && !over) 184 { 185 InvalidateRect(hwnd,&rect,TRUE); 186 } 187 break; 188 case VK_RETURN: 189 if (over) //reset 190 { 191 SendMessage(hwnd,WM_CREATE,wParam,lParam); 192 snakeLen = 4; 193 over = 0; 194 bodyPos[0].xLeft = 300;bodyPos[0].yTop = 300;bodyPos[0].xRight = 310;bodyPos[0].yBottom = 310; 195 bodyPos[1].xLeft = 310;bodyPos[1].yTop = 300;bodyPos[1].xRight = 320;bodyPos[1].yBottom = 310; 196 bodyPos[2].xLeft = 320;bodyPos[2].yTop = 300;bodyPos[2].xRight = 330;bodyPos[2].yBottom = 310; 197 bodyPos[3].xLeft = 330;bodyPos[3].yTop = 300;bodyPos[3].xRight = 340;bodyPos[3].yBottom = 310; 198 } 199 default: 200 break; 201 } 202 break; 203 case WM_PAINT: 204 hdc = BeginPaint (hwnd, &ps) ; 205 SetBkMode(hdc,TRANSPARENT); //background attribute 206 SetTextColor(hdc,RGB(250,2,2)); //text color 207 hBrush = CreateSolidBrush(RGB(0,0,200)); 208 SelectObject(hdc,hBrush); 209 210 TextOut(hdc,650,60,szBuff[0],wsprintf(szBuff[0],"%s","function:")); 211 TextOut(hdc,640,80,szBuff[1],wsprintf(szBuff[1],"%s","ESC to pause")); 212 TextOut(hdc,640,100,szBuff[2],wsprintf(szBuff[2],"%s","R to resume")); 213 TextOut(hdc,630,120,szBuff[3],wsprintf(szBuff[3],"%s","Enter to revive")); 214 TextOut(hdc,635,200,szBuff[4],wsprintf(szBuff[4],"%s","direction key")); 215 TextOut(hdc,640,220,szBuff[5],wsprintf(szBuff[5],"%s","↑↓←→")); 216 TextOut(hdc,635,400,szBuff[7],wsprintf(szBuff[7],"%s %d","Score :",snakeLen * RECTDISTANCE-40)); 217 TextOut(hdc,640,480,szBuff[2],wsprintf(szBuff[2],"%s","By :Bug")); 218 219 for (k=0;k<DRAFRAMENUM;k++) //drawn framework 220 { 221 Rectangle(hdc,xLeft,yTop+(k*RECTDISTANCE),xRight,yBottom+(k*RECTDISTANCE)); 222 } 223 for (k=0;k<DRAFRAMENUM;k++) 224 { 225 Rectangle(hdc,xLeft+(k*RECTDISTANCE),yTop,xRight+(k*RECTDISTANCE),yBottom); 226 } 227 xLeft = 610 ; //rotate painting dirction 228 xRight = 620 ; 229 yTop = 610 ; 230 yBottom = 620 ; 231 for (k=0;k<DRAFRAMENUM;k++) 232 { 233 Rectangle(hdc,xLeft,yTop-(k*RECTDISTANCE),xRight,yBottom-(k*RECTDISTANCE)); 234 } 235 for (k=0;k<DRAFRAMENUM;k++) 236 { 237 Rectangle(hdc,xLeft-(k*RECTDISTANCE),yTop,xRight-(k*RECTDISTANCE),yBottom); 238 } 239 DeleteObject(hBrush); 240 241 if(!Pause) //drawn snake 242 { 243 hBrush = CreateSolidBrush(RGB(255,0,0)); 244 SelectObject(hdc,hBrush); 245 246 moveJudge(upButton,downButton,leftButton,rightButton,bodyPos); 247 for (i=0;i<snakeLen;i++) 248 { 249 Rectangle(hdc,bodyPos[i].xLeft,bodyPos[i].yTop,bodyPos[i].xRight,bodyPos[i].yBottom); 250 } 251 DeleteObject(hBrush); 252 253 254 hBrush = CreateSolidBrush(RGB(255,140,0)); 255 SelectObject(hdc,hBrush); 256 if (BOSS == snakeLen) 257 { 258 hBrush = CreateSolidBrush(RGB(0,0,0)); 259 SelectObject(hdc,hBrush); 260 } 261 Rectangle(hdc,beanPos.xLeft,beanPos.yTop,beanPos.xRight,beanPos.yBottom); //drawn bean 262 DeleteObject(hBrush); 263 264 bean(); 265 }else{ 266 hFont = CreateFontIndirect(&lf); 267 SelectObject(hdc,hFont); 268 TextOut(hdc,200,230,szBuff [9],wsprintf(szBuff[9],"%s","Pause")); 269 DeleteObject(hFont); 270 } 271 if(gameOver()) 272 { 273 hFont = CreateFontIndirect(&lf); 274 SelectObject(hdc,hFont); 275 TextOut(hdc,200,180,szBuff [6],wsprintf(szBuff[6],"%s","Game")); 276 TextOut(hdc,200,250,szBuff [7],wsprintf(szBuff[7],"%s","Over")); 277 KillTimer(hwnd,ID_TIME); 278 over = 1; 279 DeleteObject(hFont); 280 } 281 EndPaint (hwnd, &ps) ; 282 return 0 ; 283 case WM_DESTROY: 284 PostQuitMessage (0) ; 285 return 0 ; 286 } 287 return DefWindowProc (hwnd, message, wParam, lParam) ; 288 } 289 290 void moveJudge(int upButton,int downButton,int leftButton,int rightButton,PBODYPOS pBodyPos) 291 { 292 BODYPOS temp[60]; 293 294 if(0 == pBodyPos[0].xLeft - pBodyPos[1].xLeft ) //judge the snake ' moveing way 295 { 296 if(rightButton) 297 { 298 memcpy(&pBodyPos[1],pBodyPos,sizeof(bodyPos)-sizeof(BODYPOS)); 299 pBodyPos[0].xLeft = pBodyPos[0].xLeft + RECTDISTANCE; 300 pBodyPos[0].xRight = pBodyPos[0].xRight + RECTDISTANCE; 301 }else if (leftButton) 302 { 303 memcpy(&pBodyPos[1],pBodyPos,sizeof(bodyPos)-sizeof(BODYPOS)); 304 pBodyPos[0].xLeft = pBodyPos[0].xLeft - RECTDISTANCE; 305 pBodyPos[0].xRight = pBodyPos[0].xRight - RECTDISTANCE; 306 }else if(pBodyPos[0].yTop - pBodyPos[1].yTop <0) 307 { 308 memcpy(&pBodyPos[1],pBodyPos,sizeof(bodyPos)-sizeof(BODYPOS)); 309 pBodyPos[0].yTop = pBodyPos[0].yTop - RECTDISTANCE; 310 pBodyPos[0].yBottom = pBodyPos[0].yBottom - RECTDISTANCE; 311 }else 312 { 313 memcpy(&pBodyPos[1],pBodyPos,sizeof(bodyPos)-sizeof(BODYPOS)); 314 pBodyPos[0].yTop = pBodyPos[0].yTop + RECTDISTANCE; 315 pBodyPos[0].yBottom = pBodyPos[0].yBottom + RECTDISTANCE; 316 } 317 }else 318 { 319 320 if(upButton) 321 { 322 memcpy(&pBodyPos[1],pBodyPos,sizeof(bodyPos)-sizeof(BODYPOS)); 323 pBodyPos[0].yTop = pBodyPos[0].yTop - RECTDISTANCE; 324 pBodyPos[0].yBottom = pBodyPos[0].yBottom - RECTDISTANCE; 325 }else if (downButton) 326 { 327 memcpy(&pBodyPos[1],pBodyPos,sizeof(bodyPos)-sizeof(BODYPOS)); 328 pBodyPos[0].yTop = pBodyPos[0].yTop + RECTDISTANCE; 329 pBodyPos[0].yBottom = pBodyPos[0].yBottom + RECTDISTANCE; 330 }else if(pBodyPos[0].xLeft - pBodyPos[1].xLeft <0) 331 { 332 memcpy(&pBodyPos[1],pBodyPos,sizeof(bodyPos)-sizeof(BODYPOS)); 333 pBodyPos[0].xLeft = pBodyPos[0].xLeft - RECTDISTANCE; 334 pBodyPos[0].xRight = pBodyPos[0].xRight - RECTDISTANCE; 335 }else 336 { 337 memcpy(&pBodyPos[1],pBodyPos,sizeof(bodyPos)-sizeof(BODYPOS)); 338 pBodyPos[0].xLeft = pBodyPos[0].xLeft + RECTDISTANCE; 339 pBodyPos[0].xRight = pBodyPos[0].xRight + RECTDISTANCE; 340 } 341 } 342 } 343 int gameOver(void) 344 { 345 int i,xLeft,xRight,yTop,yBottom; 346 xLeft = 0 ; 347 yTop = 0 ; 348 xRight = 610 ; 349 yBottom = 610 ; 350 351 352 for(i=1;i<snakeLen;i++) //idioctonia 353 { 354 if ((bodyPos[0].xLeft == bodyPos[i].xLeft) && (bodyPos[0].yTop == bodyPos[i].yTop)) 355 return 1; 356 } 357 if (xLeft == bodyPos[0].xLeft || xRight == bodyPos[0].xRight) //knock snake' s head against a brick wall 358 { 359 return 1; 360 }else if(yTop == bodyPos[0].yTop || yBottom == bodyPos[0].yBottom) 361 return 1; 362 363 return 0; 364 365 } 366 void bean() 367 { 368 int i; 369 370 if (beanPos.xLeft == bodyPos[0].xLeft && beanPos.yTop == bodyPos[0].yTop) 371 beanMiss = 1; 372 if(beanMiss) 373 { 374 while(beanMiss) 375 { 376 beanPos.xLeft = feed(); 377 beanPos.xRight = beanPos.xLeft + RECTDISTANCE; 378 beanPos.yTop = beanPos.xLeft; 379 beanPos.yBottom = beanPos.xRight; 380 for (i=0;i<snakeLen;i++) 381 { 382 if (beanPos.xLeft != bodyPos[i].xLeft && beanPos.yTop != bodyPos[i].yTop ) 383 { 384 beanMiss = 0; 385 break; 386 } 387 } 388 } 389 snakeLen++; 390 } 391 } 392 393 int feed(void) 394 { 395 int temp,i; 396 397 srand((int)time(0)); 398 399 for (i=0;i<10;i++) 400 temp =rand()%611; 401 if (temp<10) 402 { 403 temp+=10; 404 } 405 if (0 != temp % 10) 406 { 407 while(0 != temp % 10) 408 { 409 temp--; 410 if (0 == temp % 10) 411 break; 412 } 413 } 414 return temp; 415 }
终于可以发一张帖子了,大一上的贪吃蛇.
经过这次程序了解了.
(1)创建画笔画刷要记得删除否则会溢出.
(2)消息机制,非消息和消息队列.

浙公网安备 33010602011771号