从学习windows 开始 懂得一点点东西就按耐不住自己的好奇心想做出第一个属于自己的游戏。
再网上也看了看别人的成果,因为自己也是刚开始学习,所以决定模仿一个拼图游戏。
一下是自己 参考 后 写出的代码
用了人家的一个随机洗牌的算法,但是发现一个问题,有时候会发生拼图无法拼出的情况,这个问题暂时还没有进行改进
/* file: 拼图游戏 author: 1050577129 data: 2015/4/5 */ #include<windows.h> #include<stdio.h> #include<stdlib.h> #include<time.h> #include "resource.h" const int VHNUMS = 3; int nBlack,saveBlack; int Win = 0; const int CELLNUM = VHNUMS * VHNUMS; int map[16]; LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM ); int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow ) { WNDCLASS wndcls; HWND hwnd; wndcls.style = CS_HREDRAW | CS_VREDRAW; wndcls.lpfnWndProc = (WNDPROC)WndProc; wndcls.cbClsExtra=0; wndcls.cbWndExtra=0; wndcls.hInstance = hInstance; wndcls.hIcon = LoadIcon(NULL,IDI_APPLICATION); wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wndcls.hCursor=LoadCursor(NULL,IDC_ARROW); wndcls.lpszClassName="first"; wndcls.lpszMenuName=NULL; if(!RegisterClass(&wndcls)) { MessageBox(NULL, TEXT("This program requires Window NT!"), TEXT("error"), MB_ICONERROR); return 0; } hwnd=CreateWindow(TEXT("first"), TEXT("拼图"), WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX, CW_USEDEFAULT,CW_USEDEFAULT, 640,508, NULL,NULL,hInstance,NULL); ShowWindow(hwnd,iCmdShow); UpdateWindow(hwnd); MSG msg; while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } void InitGameMap() { for(int i=0;i<CELLNUM;i++) map[i] = i; srand((unsigned int)(time(0))); for(int j =CELLNUM-1;j>0;j--) { int randIndex = rand() % j; int tem = map[j]; map[j] = map[randIndex]; map[randIndex] = tem; } map[CELLNUM-1] = -1; nBlack = CELLNUM -1; } void DrawGameMap(HDC hdc, HDC mdc) { int cxCell = 640 / VHNUMS; int cyCell = 480 / VHNUMS; for (int i = 0; i < VHNUMS; i++) for (int j = 0; j < VHNUMS; j++) { int nCell = map[j*VHNUMS + i]; if (nCell == -1) { Rectangle(hdc, i*cxCell, j*cyCell, i*cxCell+cxCell, j*cyCell+cyCell); continue; } int nRow = nCell / VHNUMS; int nCol = nCell % VHNUMS; BitBlt(hdc, i*cxCell, j*cyCell, cxCell, cyCell, mdc, nCol*cxCell, nRow*cyCell, SRCCOPY); } /*BitBlt(hdc, 0, 0, 640, 480, mdc, 0, 0, SRCCOPY); */ for(int b=0;b<VHNUMS;b++) { MoveToEx(hdc, b*cxCell,0,NULL); LineTo(hdc, b*cxCell, 512); } for(int c=0;c<VHNUMS;c++) { MoveToEx(hdc, 0,c*cyCell,NULL); LineTo(hdc,640, c*cyCell); } } void RedrawRect(HWND hwnd, int index) { RECT rect; int xPos, yPos; xPos = index % VHNUMS; yPos = index / VHNUMS; rect.left = xPos*640 / VHNUMS; rect.right = rect.left + 640 / VHNUMS; rect.top = yPos * 480 / VHNUMS; rect.bottom = rect.top + 480 / VHNUMS; InvalidateRect(hwnd, &rect, FALSE); } void Swap(int a, int b) { int tmp = map[a]; map[a] = map[b]; map[b] = tmp; } void MoveRight() { saveBlack = nBlack; if(nBlack % VHNUMS == 0) return; Swap(nBlack, nBlack-1); nBlack = nBlack - 1; } void MoveLeft() { saveBlack = nBlack; if(nBlack % VHNUMS == VHNUMS-1) return; Swap(nBlack+1, nBlack); nBlack = nBlack + 1; } void MoveUp() { saveBlack = nBlack; if(nBlack >= CELLNUM-VHNUMS) return; Swap(nBlack+VHNUMS, nBlack); nBlack = nBlack+VHNUMS; } void MoveDown() { saveBlack = nBlack; if(nBlack < VHNUMS) return; Swap(nBlack-VHNUMS, nBlack); nBlack = nBlack-VHNUMS; } void CheckIsWin() { int x = 0, j = 0; for (int i = 0; i < CELLNUM; i++) { if (map[i] == i) x++; else j = i; } if(x == CELLNUM - 1) { Win = 1; map[j] = j; return; } else Win = 0; } LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam ) { HBITMAP hBitmap; static int cxClient, cyClient, cxSource, cySource; BITMAP bitmap; HDC hdc, mdc; PAINTSTRUCT ps; //RECT rect; switch(message) { case WM_CREATE: InitGameMap(); return 0; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); mdc = CreateCompatibleDC(hdc); hBitmap = (HBITMAP)LoadImage(NULL, "bp12.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION); GetObject(hBitmap,sizeof(BITMAP), &bitmap); SelectObject(mdc, hBitmap); DrawGameMap(hdc, mdc); DeleteDC(mdc); DeleteObject(hBitmap); EndPaint(hwnd, &ps); return 0; case WM_KEYDOWN: switch(wParam) { case VK_LEFT: MoveLeft(); break; case VK_RIGHT: MoveRight(); break; case VK_UP: MoveUp(); break; case VK_DOWN: MoveDown(); break; default: break; } if (saveBlack != nBlack) { RedrawRect(hwnd, saveBlack); RedrawRect(hwnd, nBlack); } CheckIsWin(); if(Win) { MessageBox(hwnd, TEXT("恭喜你你赢了"),TEXT("胜利"), MB_ICONINFORMATION); } return 0; case WM_DESTROY: // DeleteObject(hBitmap); PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); }