资讯动态

纯WINDOWS API的C++2D小游戏Subterra(目前还未开发,实时更新)

发布时间:2026/8/16 6:00:31 来源:尧图企业网站定制
运行方式Visual Studio (请拥有“使用 C 的桌面开发”工作负载)下载方式https://visualstudio.microsoft.com/zh-hans/dowloads/若上述方法不行请在 Microsoft Store 内尝试搜索 Visual Studio Community 并下载请启动VS建议使用VS2026创建新项目 Windows 桌面应用程序 输入项目名称 创建右键“源代码” 新建 main.cppmain.cpp源代码#includewindows.h#includegdiplus.h#pragmacomment(lib,gdiplus.lib)#defineCLASS_NAMELSubterra#defineWINDOW_WIDTH800#defineWINDOW_HEIGHT512#defineICON_WIDTH32#defineICON_HEIGHT32#defineFDS_MS16structPLAYER{intX400;intY256;intWidth14;intHeight20;doubleG80;doubleStart_Jump_Speed60000;doubleMove_Speed24000;doubleJump_Speed0;boolIs_Jumpingfalse;intMoving0;//0sat, 1left 2rightboolIs_Squatingfalse;boolGame_Overfalse;boolKeys[95]{false};}Player1;HWND hWnd;HINSTANCE g_hInstance;HDC hPlayerDC;HBITMAP hBitPlayer;intMap[WINDOW_WIDTH/16][WINDOW_HEIGHT/16]{0};HBITMAPCreateTransparentMask(HDC hMemDC,intWidth,intHeight);HICONCreateTransparentIcon(intWidth,intHeight);voidInitGraphics(HDC hdcScreen);voidCleanupGraphics();voidDrawCharacter(HDC hdcScreen);voidDrawGrid();voidUpdatePosition();voidUpdateBackGround();voidUpdate();LRESULT CALLBACKWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam);intWINAPIWinMain(HINSTANCE hInstance,HINSTANCE hPrevlstance,LPSTR lpCmdLine,intnCmdShow){g_hInstancehInstance;WNDCLASSEX wcex{0};wcex.cbSizesizeof(WNDCLASSEX);wcex.hbrBackground(HBRUSH)(COLOR_WINDOW1);wcex.hCursorLoadCursor(NULL,IDC_ARROW);wcex.hIconLoadIcon(NULL,IDC_ARROW);wcex.hInstancehInstance;wcex.lpfnWndProcWindowProc;wcex.lpszClassNameCLASS_NAME;wcex.styleCS_HREDRAW|CS_VREDRAW;if(!RegisterClassEx(wcex)){MessageBox(NULL,LFailed to register window class,LERROR,MB_ICONERROR);return0;}hWndCreateWindowEx(0,CLASS_NAME,CLASS_NAME,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,WINDOW_WIDTH,WINDOW_HEIGHT,NULL,NULL,hInstance,NULL);if(!hWnd){MessageBox(NULL,LFailed to create window,LERROR,MB_ICONERROR);return0;}ShowWindow(hWnd,nCmdShow);UpdateWindow(hWnd);MSG msg{0};while(GetMessage(msg,NULL,0,0)){TranslateMessage(msg);DispatchMessage(msg);}return(int)msg.wParam;}LRESULT CALLBACKWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam){switch(uMsg){caseWM_CREATE:{HICON hMyIconCreateTransparentIcon(32,32);if(hMyIcon){SendMessage(hwnd,WM_SETICON,ICON_SMALL,(LPARAM)hMyIcon);SendMessage(hwnd,WM_SETICON,ICON_BIG,(LPARAM)hMyIcon);}InitGraphics(GetDC(hwnd));ReleaseDC(hwnd,GetDC(hwnd));SetTimer(hwnd,1,FDS_MS,NULL);break;}caseWM_KEYDOWN:Player1.Keys[wParam]true;break;caseWM_KEYUP:Player1.Keys[wParam]false;break;caseWM_TIMER:Update();break;caseWM_PAINT:{PAINTSTRUCT ps;HDC hdcBeginPaint(hwnd,ps);DrawCharacter(hdc);EndPaint(hwnd,ps);break;}caseWM_DESTROY:KillTimer(hwnd,1);CleanupGraphics();PostQuitMessage(0);break;}returnDefWindowProc(hwnd,uMsg,wParam,lParam);}HBITMAPCreateTransparentMask(HDC hMemDC,intWidth,intHeight){HBITMAP hMaskBmpCreateBitmap(Width,Height,1,1,NULL);SelectObject(hMemDC,hMaskBmp);HBRUSH hBrushWhiteCreateSolidBrush(RGB(255,255,255));RECT rc{0,0,Width,Height};FillRect(hMemDC,rc,hBrushWhite);DeleteObject(hBrushWhite);HPEN hPenBlackCreatePen(PS_SOLID,1,RGB(0,0,0));SelectObject(hMemDC,hPenBlack);Ellipse(hMemDC,4,4,Width-4,Width-4);DeleteObject(hPenBlack);returnhMaskBmp;}HICONCreateTransparentIcon(intWidth,intHeight){HDC hScreenDCGetDC(NULL);if(!hScreenDC)returnNULL;HDC hMemDCCreateCompatibleDC(hScreenDC);BITMAPINFOHEADER biHeader{0};biHeader.biSizesizeof(BITMAPINFOHEADER);biHeader.biWidthWidth;biHeader.biHeightHeight;biHeader.biPlanes1;biHeader.biBitCount32;biHeader.biCompressionBI_RGB;void*pBitsNULL;HBITMAP hColorBmpCreateDIBSection(hScreenDC,(BITMAPINFO*)biHeader,DIB_RGB_COLORS,pBits,NULL,0);if(!hColorBmp||!pBits){DeleteDC(hMemDC);ReleaseDC(NULL,hScreenDC);returnNULL;}SelectObject(hMemDC,hColorBmp);DWORD*pPixel(DWORD*)pBits;for(intj0;jHeight;j)for(inti0;iWidth;i)pPixel[j*Widthi]0x00000000;intcxWidth/2,cyHeight/2,rWidth/2-2;for(intycy-r;ycyr;y)for(intxcx-r;xcxr;x)if((x-cx)*(x-cx)(y-cy)*(y-cy)r*r)if(y0yHeightx0xWidth)pPixel[y*Widthx]0xFF0000FF;//BGRAHBITMAP hMaskBmpCreateTransparentMask(hMemDC,Width,Height);ICONINFO iconInfo{0};iconInfo.fIconTRUE;iconInfo.xHotspotWidth/2;iconInfo.yHotspotHeight/2;iconInfo.hbmColorhColorBmp;iconInfo.hbmMaskhMaskBmp;HICON hIconCreateIconIndirect(iconInfo);DeleteObject(hColorBmp);DeleteObject(hMaskBmp);DeleteDC(hMemDC);ReleaseDC(NULL,hScreenDC);returnhIcon;}voidInitGraphics(HDC hdcScreen){hPlayerDCCreateCompatibleDC(hdcScreen);hBitPlayerCreateCompatibleBitmap(hdcScreen,Player1.Width,Player1.Height);SelectObject(hPlayerDC,hBitPlayer);HBRUSH hBrushWhiteCreateSolidBrush(RGB(255,255,255));HBRUSH hBrushRedCreateSolidBrush(RGB(255,0,0));SelectObject(hPlayerDC,hBrushRed);Rectangle(hPlayerDC,0,0,Player1.Width,Player1.Height);DeleteObject(hBrushWhite);DeleteObject(hBrushRed);}voidCleanupGraphics(){if(hPlayerDC)DeleteDC(hPlayerDC);if(hBitPlayer)DeleteObject(hBitPlayer);}voidDrawCharacter(HDC hdcScreen){BitBlt(hdcScreen,Player1.X-Player1.Width/2,Player1.Y-Player1.Height,Player1.Width,Player1.Height,hPlayerDC,0,0,SRCCOPY);}voidUpdate(){UpdatePosition();}voidUpdatePosition(){if(Player1.Game_Over)return;intdx0;intdy0;if(Player1.Keys[W])dy-3;if(Player1.Keys[S])dy3;if(Player1.Keys[A])dx-3;if(Player1.Keys[D])dx3;Player1.Xdx;Player1.Ydy;if(Player1.X0)Player1.X0;if(Player1.XWINDOW_WIDTH)Player1.XWINDOW_WIDTH;if(Player1.Y0)Player1.Y0;if(Player1.YWINDOW_HEIGHT)Player1.YWINDOW_HEIGHT;InvalidateRect(hWnd,NULL,TRUE);}备注目前仅实现矩形的wasd移动编辑者zzxjason

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价