当前位置: 首页 > news >正文

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

运行方式:

  • Visual Studio (请拥有“使用 C++ 的桌面开发”工作负载)
    下载方式:https://visualstudio.microsoft.com/zh-hans/dowloads/
    (若上述方法不行,请在 Microsoft Store 内尝试搜索 Visual Studio Community 并下载)
    请启动VS(建议使用VS2026):创建新项目> Windows 桌面应用程序> 输入项目名称> 创建
    右键“源代码”> 新建> main.cpp

main.cpp源代码

#include<windows.h>#include<gdiplus.h>#pragmacomment(lib,"gdiplus.lib")#defineCLASS_NAMEL"Subterra"#defineWINDOW_WIDTH800#defineWINDOW_HEIGHT512#defineICON_WIDTH32#defineICON_HEIGHT32#defineFDS_MS16structPLAYER{intX=400;intY=256;intWidth=14;intHeight=20;doubleG=80;doubleStart_Jump_Speed=60000;doubleMove_Speed=24000;doubleJump_Speed=0;boolIs_Jumping=false;intMoving=0;//0sat, 1left 2rightboolIs_Squating=false;boolGame_Over=false;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_hInstance=hInstance;WNDCLASSEX wcex={0};wcex.cbSize=sizeof(WNDCLASSEX);wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);wcex.hCursor=LoadCursor(NULL,IDC_ARROW);wcex.hIcon=LoadIcon(NULL,IDC_ARROW);wcex.hInstance=hInstance;wcex.lpfnWndProc=WindowProc;wcex.lpszClassName=CLASS_NAME;wcex.style=CS_HREDRAW|CS_VREDRAW;if(!RegisterClassEx(&wcex)){MessageBox(NULL,L"Failed to register window class",L"ERROR",MB_ICONERROR);return0;}hWnd=CreateWindowEx(0,CLASS_NAME,CLASS_NAME,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,WINDOW_WIDTH,WINDOW_HEIGHT,NULL,NULL,hInstance,NULL);if(!hWnd){MessageBox(NULL,L"Failed to create window",L"ERROR",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 hMyIcon=CreateTransparentIcon(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 hdc=BeginPaint(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 hMaskBmp=CreateBitmap(Width,Height,1,1,NULL);SelectObject(hMemDC,hMaskBmp);HBRUSH hBrushWhite=CreateSolidBrush(RGB(255,255,255));RECT rc={0,0,Width,Height};FillRect(hMemDC,&rc,hBrushWhite);DeleteObject(hBrushWhite);HPEN hPenBlack=CreatePen(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 hScreenDC=GetDC(NULL);if(!hScreenDC)returnNULL;HDC hMemDC=CreateCompatibleDC(hScreenDC);BITMAPINFOHEADER biHeader={0};biHeader.biSize=sizeof(BITMAPINFOHEADER);biHeader.biWidth=Width;biHeader.biHeight=Height;biHeader.biPlanes=1;biHeader.biBitCount=32;biHeader.biCompression=BI_RGB;void*pBits=NULL;HBITMAP hColorBmp=CreateDIBSection(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(intj=0;j<Height;j++)for(inti=0;i<Width;i++)pPixel[j*Width+i]=0x00000000;intcx=Width/2,cy=Height/2,r=Width/2-2;for(inty=cy-r;y<=cy+r;y++)for(intx=cx-r;x<=cx+r;x++)if((x-cx)*(x-cx)+(y-cy)*(y-cy)<=r*r)if(y>=0&&y<Height&&x>=0&&x<Width)pPixel[y*Width+x]=0xFF0000FF;//BGRAHBITMAP hMaskBmp=CreateTransparentMask(hMemDC,Width,Height);ICONINFO iconInfo={0};iconInfo.fIcon=TRUE;iconInfo.xHotspot=Width/2;iconInfo.yHotspot=Height/2;iconInfo.hbmColor=hColorBmp;iconInfo.hbmMask=hMaskBmp;HICON hIcon=CreateIconIndirect(&iconInfo);DeleteObject(hColorBmp);DeleteObject(hMaskBmp);DeleteDC(hMemDC);ReleaseDC(NULL,hScreenDC);returnhIcon;}voidInitGraphics(HDC hdcScreen){hPlayerDC=CreateCompatibleDC(hdcScreen);hBitPlayer=CreateCompatibleBitmap(hdcScreen,Player1.Width,Player1.Height);SelectObject(hPlayerDC,hBitPlayer);HBRUSH hBrushWhite=CreateSolidBrush(RGB(255,255,255));HBRUSH hBrushRed=CreateSolidBrush(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;intdx=0;intdy=0;if(Player1.Keys['W'])dy-=3;if(Player1.Keys['S'])dy+=3;if(Player1.Keys['A'])dx-=3;if(Player1.Keys['D'])dx+=3;Player1.X+=dx;Player1.Y+=dy;if(Player1.X<0)Player1.X=0;if(Player1.X>WINDOW_WIDTH)Player1.X=WINDOW_WIDTH;if(Player1.Y<0)Player1.Y=0;if(Player1.Y>WINDOW_HEIGHT)Player1.Y=WINDOW_HEIGHT;InvalidateRect(hWnd,NULL,TRUE);}

备注:目前仅实现矩形的wasd移动
编辑者:zzxjason

http://www.jsqmd.com/news/1402923/

相关文章:

  • 从PID到模型预测:管道小球摆杆控制的核心难点与工程实现
  • Linux vsftpd匿名FTP服务器搭建:安全配置与被动模式故障排查
  • OpenClaw像素风赛博办公室:AI Agent工作流可视化与实时监控实践
  • 华为eNSP设备启动失败:从虚拟化冲突到权限配置的完整排错指南
  • 工业陶瓷零件的材料选型与精度控制:从氧化锆到氮化硅的几个技术点
  • 【无标题】买房收房别只看颜值,这些坑你根本看不见 在本地房产交易活跃的当下,越来越多人意识到:房子表面光鲜不代表住得安心。尤其是二手房翻新或精装交付,墙面平整、地板漂亮背后,可能藏着水电隐患、防水失效
  • Quartus内置ModelSim‑Altera报错Error: Failure to obtain a Verilog simulation license. Unable to checkout
  • Cursor 修复循环校验翻车:3次追问后账单超预算200%,我这样设熔断
  • 居家如何养护艾草暖宫精油日常养护感受海艾独有的温润体感
  • PDF 临时处理总要四处找工具?常用操作放进一个工具箱
  • 脱皮腰果色选怎样减少黑斑、碎仁与异色料混入
  • 上海市口碑好的防水补漏维修公司怎么找_屋面防水本地维修队伍水准盘点,挑选要点深度剖析 - 雨婺虹修缮
  • 网络安全入门:小白必学的极简框架与实战指南
  • OpenClaw部署运维指南:从安装到稳定运行的AI智能体实践
  • Windows鼠标异常唤醒电脑的排查与解决全攻略
  • IDEA代码报红但能运行?深度解析索引缓存机制与系统化解决方案
  • Java枚举类深度解析:从常量管理到状态机设计的实战指南
  • Linux 服务器测速工具:speedtest‑cli 命令完整详解(命令行网速测试实战)
  • 40岁适合做什么创业项目?
  • 找个大佬带带我
  • 标书销毁怎么做?这几种方式最靠谱 - 生态测评师
  • RAG 召回率 95% 仍答错:Anthropic 重排机制差点让我交差一份科幻小说
  • 空地协同巡线系统:从仿真到实战的嵌入式与机器人视觉开发指南
  • 彻底解决Maven依赖爆红:从诊断到根治的系统化指南
  • Object.hasOwn 兼容性解析:从原理到工程化解决方案
  • 芯片ESD测试仪是什么?主要有哪几种不同类型?
  • Thrust 编译时算法选择:让编译器帮你选最快的路
  • Qt QPushButton文本自动换行实现:子类化重绘方案详解
  • Jupyter Notebook使用指南
  • 一个撇号击穿整条证据链:我用一个真实 Bug 测试 LLM 的代码调试能力