void DispatchMessage() { InitSnake(); InitWall(); while (1) { CreateFood(); CreateAward(); DrawString(10, 0, " Score:%04d ", score); AppendSnakeNodeList(Head, Orientation); CheckAward(); if (CheckSnake() || CheckWall()) { DrawString(2, 2, "你死啦! 按任意键重新开始玩!"); getch(); break; } DrawString(Head->Location.x, Head->Location.y, "◎"); if (!CheckFood()) { DrawString(Tail->Location.x, Tail->Location.y, " "); DeleteSnakeNodeList(Tail); } int keep = 0; if (kbhit()) { switch (getch()) { case 0x1b://按下ESC,暂停游戏 getch(); continue; case 0xE0://按下特殊键 switch (getch()) { case 75: if(Orientation==0) keep = 1;if (Orientation%2!=0) Orientation=0; break; case 72: if(Orientation==1) keep = 1;if (Orientation%2==0) Orientation=1; break; case 77: if(Orientation==2) keep = 1;if (Orientation%2!=0) Orientation=2; break; case 80: if(Orientation==3) keep = 1;if (Orientation%2==0) Orientation=3; break; default: ; } break; } } if(!keep&&(100-score*2)>10)Sleep(100-score*2);//难度提升 else Sleep(10); } }
// // FUNCTION: WndProc(HWND, unsigned, WORD, LONG) // // PURPOSE: Processes messages for the main window. // // WM_COMMAND - process the application menu // WM_PAINT - Paint the main window // WM_DESTROY - post a quit message and return // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc,hMemDC; TCHAR szHello[MAX_LOADSTRING]; LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING); switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About); break; case IDM_EXIT: DestroyWindow(hWnd); break; case IDM_UP: SendMessage(hWnd,WM_KEYDOWN,(WPARAM)VK_UP,0); break; case IDM_LEFT: SendMessage(hWnd,WM_KEYDOWN,(WPARAM)VK_LEFT,0); break; case IDM_RIGHT: SendMessage(hWnd,WM_KEYDOWN,(WPARAM)VK_RIGHT,0); break; case IDM_DOWN: SendMessage(hWnd,WM_KEYDOWN,(WPARAM)VK_DOWN,0); break; case IDM_START_PAUSE: SendMessage(hWnd,WM_KEYDOWN,(WPARAM)VK_SPACE,0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_CREATE: m = 20; n = 15; for(i = 0; i < n_body; i++) { Position[i].x = m-i; Position[i].y = n; Matrix[Position[i].x][Position[i].y] = 1; } CreateFood(); CreateUI(hWnd); SetTimer(hWnd,100,speed,NULL); break; case WM_KEYDOWN: switch(wParam) { case VK_UP: //KillTimer(hWnd,100); if(direction != 2 && n>0 && CheckOver(hWnd,m,n-1)) { for(i = 0; i < n_body; i++) { Matrix[Position[i].x][Position[i].y] = 0; } n--; CheckFood(); for(i = n_body-1; i > 0; i--) { Position[i] = Position[i-1]; } Position[0].y = n; for(i = 0; i < n_body; i++) { Matrix[Position[i].x][Position[i].y] = 1; } CreateFood(); direction = 0; DrawUI(hWnd); } else MessageBeep(0); // SetTimer(hWnd,100,500,NULL); break; case VK_LEFT: // KillTimer(hWnd,100); if(direction != 1 && m>0 && CheckOver(hWnd,m-1,n)) { for(i = 0; i < n_body; i++) { Matrix[Position[i].x][Position[i].y] = 0; } m--; CheckFood(); for(i = n_body-1; i > 0; i--) { Position[i] = Position[i-1]; } Position[0].x = m; for(i = 0; i < n_body; i++) { Matrix[Position[i].x][Position[i].y] = 1; } CreateFood(); direction = 3; DrawUI(hWnd); } else MessageBeep(0); // SetTimer(hWnd,100,500,NULL); break; case VK_RIGHT: //KillTimer(hWnd,100); if(direction != 3 && m<39 && CheckOver(hWnd,m+1,n)) { for(i = 0; i < n_body; i++) { Matrix[Position[i].x][Position[i].y] = 0; } m++; CheckFood(); for(i = n_body-1; i > 0; i--) { Position[i] = Position[i-1]; } Position[0].x = m; for(i = 0; i < n_body; i++) { Matrix[Position[i].x][Position[i].y] = 1; } CreateFood(); direction = 1; DrawUI(hWnd); } else MessageBeep(0); // SetTimer(hWnd,100,500,NULL); break; case VK_SPACE: if(start_or_pause) { KillTimer(hWnd,100); start_or_pause = false; }else { SetTimer(hWnd,100,speed,NULL); start_or_pause = true; } break; case VK_DOWN: // KillTimer(hWnd,100); if(direction != 0 && n<31 && CheckOver(hWnd,m,n+1) ) { for(i = 0; i < n_body; i++) { Matrix[Position[i].x][Position[i].y] = 0; } n++; CheckFood(); for(i = n_body-1; i > 0; i--) { Position[i] = Position[i-1]; } Position[0].y = n; for(i = 0; i < n_body; i++) { Matrix[Position[i].x][Position[i].y] = 1; } CreateFood(); direction = 2; DrawUI(hWnd); } else MessageBeep(0); // SetTimer(hWnd,100,500,NULL); break; } break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... //GetClientRect(hWnd, &rt); hbr = (HBRUSH)GetStockObject(BLACK_BRUSH); hBmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1)); hMemDC = CreateCompatibleDC(hdc); hBmpOld = (HBITMAP)SelectObject(hMemDC,hBmp); //Matrix[10][10] = 1; for(i = 0; i < 40; i++) for(j =0; j <32; j++) { rt.left = i * 16; rt.top = 24 + j *16; switch(Matrix[i][j]) { case 0: { rt.right = rt.left + 16; rt.bottom = rt.top + 16 ; FillRect(hdc,&rt,hbr); } break; case 1: { hBmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1)); hMemDC = CreateCompatibleDC(hdc); hBmpOld = (HBITMAP)SelectObject(hMemDC,hBmp); BitBlt(hdc,rt.left,rt.top,16,16,hMemDC,0,0,SRCCOPY); SelectObject(hMemDC, hBmpOld); DeleteObject(hBmp); DeleteObject(hMemDC); } break; case 2: { hBmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP2)); hMemDC = CreateCompatibleDC(hdc); hBmpOld = (HBITMAP)SelectObject(hMemDC,hBmp); BitBlt(hdc,rt.left,rt.top,16,16,hMemDC,0,0,SRCCOPY); SelectObject(hMemDC, hBmpOld); DeleteObject(hBmp); DeleteObject(hMemDC); } break; } } SelectObject(hMemDC, hBmpOld); DeleteObject(hBmp); DeleteObject(hMemDC); EndPaint(hWnd, &ps); break; case WM_TIMER: //KillTimer(hWnd,100); switch(direction) { case 0://UP for(i = 0; i < n_body; i++) { Matrix[Position[i].x][Position[i].y] = 0; } if(n>0) { n--; } else { //MessageBox(NULL,"Game Over!",NULL,NULL); KillTimer(hWnd,100); gameover = false; } CheckFood(); for(i = n_body-1; i > 0; i--) { Position[i] = Position[i-1]; } Position[0].y = n; for(i = 0; i < n_body; i++) { Matrix[Position[i].x][Position[i].y] = 1; } CreateFood(); DrawUI(hWnd); break; case 1://RIGHT for(i = 0; i < n_body; i++) { Matrix[Position[i].x][Position[i].y] = 0; } if(m<39) m++; else { //MessageBox(NULL,"Game Over!",NULL,NULL); KillTimer(hWnd,100); gameover = false; } CheckFood(); for(i = n_body-1; i > 0; i--) { Position[i] = Position[i-1]; } Position[0].x = m; for(i = 0; i < n_body; i++) { Matrix[Position[i].x][Position[i].y] = 1; } CreateFood(); DrawUI(hWnd); break; case 2://DOWN for(i = 0; i < n_body; i++) { Matrix[Position[i].x][Position[i].y] = 0; } if(n<31) n++; else { // MessageBox(NULL,"Game Over!",NULL,NULL); KillTimer(hWnd,100); gameover = false ; } CheckFood(); for(i = n_body-1; i > 0; i--) { Position[i] = Position[i-1]; } Position[0].y = n; for(i = 0; i < n_body; i++) { Matrix[Position[i].x][Position[i].y] = 1; } CreateFood(); DrawUI(hWnd); break; case 3://LEFT for(i = 0; i < n_body; i++) { Matrix[Position[i].x][Position[i].y] = 0; } if(m>0) m--; else { //MessageBox(NULL,"Game Over!",NULL,NULL); KillTimer(hWnd,100); gameover = false; } CheckFood(); for(i = n_body-1; i > 0; i--) { Position[i] = Position[i-1]; } Position[0].x = m; for(i = 0; i < n_body; i++) { Matrix[Position[i].x][Position[i].y] = 1; } CreateFood(); DrawUI(hWnd); break; } //SetTimer(hWnd,100,500,NULL); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }