正文
增加 59 行删除 59 行
453 行未改动全部展开45445545645745845946046146216 行未改动47948048148248348448548648748848949049149249349449543 行未改动5395405415425435445455465479 行未改动55755855956056156256356456556656756856957017 行未改动5885895905915925935945955965975985996006016026036046056066076086096106113 行未改动61561661761861962062162262362462562662762862963063163263363463563663763863964064164236 行未改动6796806816826836846856866876 行未改动69469569669769869970070170251 行未改动754755756757758759760761762101 行未改动全部展开86486586686786886987087187287387487587687787887988088188288388488588688714 行未改动90290390490590690790890991013 行未改动9249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769778 行未改动98698798898999099199299399499599699799899910001001100210035 行未改动10091010101110121013101410151016101719 行未改动103710381039104010411042104310441045104610471048104915 行未改动10651066106710681069107010711072107310741075107631 行未改动110811091110111111121113111411151116111711181119112011211122112329 行未改动115311541155115611571158115911601161116211631164116511661167116810 行未改动11791180118111821183118411851186118712 行未改动453 行未改动全部展开cout << " 1. 无限自动生成地图 (Infinite procedural map)\n";cout << " 2. 加载已有地图 (Load existing map from file)\n";cout << " --------------------------------------------------\n";cout << " 输入编号并回车:";int choice_input = 1; // 默认选择cin >> choice_input;cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 清除行尾的换行符16 行未改动}string fname_base;if (fname_choice == 1) {fname_base = "map1";} else if (fname_choice == 2) {fname_base = "map2";} else {cout << "无效选择,默认为 'map1.txt'.\n";fname_base = "map1";}loadExistingMap(fname_base + ".txt");} else {mapMode = MapMode::Infinite;cout << " 已选择无限自动生成模式。\n";}43 行未改动outtextxy(50, current_y, _T("- 1 键: 在前方空地放置当前选定的方块类型")); current_y += line_height;outtextxy(50, current_y, _T("- [ 键 (左方括号): 循环选择上一个要放置的方块类型")); current_y += line_height;outtextxy(50, current_y, _T("- ] 键 (右方括号): 循环选择下一个要放置的方块类型")); current_y += line_height;outtextxy(50, current_y, _T("- ESC 键: 退出游戏")); current_y += line_height;outtextxy(50, current_y, _T("- + 键: 增大迷你地图尺寸")); current_y += line_height;outtextxy(50, current_y, _T("- - 键: 减小迷你地图尺寸")); current_y += line_height;outtextxy(50, current_y, _T("- R 键: 左右翻转迷你地图显示")); current_y += line_height * 2;outtextxy(50, current_y, _T("--------------------------------------------------")); current_y += line_height;outtextxy(50, current_y, _T("准备好了吗?按下 ENTER 键开始冒险..."));9 行未改动startTime = GetTickCount(); // 记录游戏开始时间BeginBatchDraw(); // 开启批量绘制,避免闪烁// 按键状态变量 (用于检测按键按下/释放,避免重复触发)bool altPrev=false, mPrev=false, xPrev=false, kPrev=false, rPrev=false;bool onePrev=false, spacePrev=false;bool cyclePrevPrev = false, cycleNextPrev = false;bool plusPrev = false, minusPrev = false;long long lastFrameTime = GetTickCount(); // 上一帧的时间// 游戏主循环17 行未改动}}altPrev=alt;if(mouseMode){POINT p; GetCursorPos(&p);int dx=p.x-lastMousePos.x;int dy=p.y-lastMousePos.y; // 用于俯仰角player.angle+=dx*MOUSE_SENSITIVITY;player.pitch-=dy*MOUSE_SENSITIVITY; // 负号用于 Y 轴反转SetCursorPos(lastMousePos.x,lastMousePos.y); // 将鼠标光标重新定位到中心}// 水平视角旋转 (偏航)if(keyDown(VK_LEFT)) player.angle-=KEY_SENSITIVITY;if(keyDown(VK_RIGHT)) player.angle+=KEY_SENSITIVITY;// 垂直视角旋转 (俯仰)if(keyDown(VK_UP)) player.pitch+=PITCH_SENSITIVITY;if(keyDown(VK_DOWN)) player.pitch-=PITCH_SENSITIVITY;player.pitch = max(-MAX_PITCH_ANGLE, min(MAX_PITCH_ANGLE, player.pitch)); // 限制俯仰角度// 更新玩家的三角函数值 (在角度/俯仰角改变后必须更新)player.updateTrig();3 行未改动double currentMoveSpeed = MOVE_SPEED;if (keyDown(VK_SHIFT)) currentMoveSpeed *= 1.5; // 按住 SHIFT 冲刺if(keyDown('W')){ // 前进nx+=player.cosA*currentMoveSpeed;ny+=player.sinA*currentMoveSpeed;}if(keyDown('S')){ // 后退nx-=player.cosA*currentMoveSpeed;ny-=player.sinA*currentMoveSpeed;}if(keyDown('A')){ // 横向左移nx+=player.sinA*currentMoveSpeed;ny-=player.cosA*currentMoveSpeed;}if(keyDown('D')){ // 横向右移nx-=player.sinA*currentMoveSpeed;ny+=player.cosA*currentMoveSpeed;}// 简单的水平碰撞检测// 检查玩家包围盒的四个角double player_half_width = 0.2; // 玩家的有效半宽用于碰撞// 仅检查 X 方向的移动是否会导致碰撞bool can_move_x = (getWorldAt(static_cast<int>(nx - player_half_width), static_cast<int>(player.y - player_half_width)) != WALL &&getWorldAt(static_cast<int>(nx + player_half_width), static_cast<int>(player.y - player_half_width)) != WALL &&36 行未改动player.onGround = true; // 标记为在地面上}// 小地图开关if(keyDown('M')&&!mPrev) showMiniMap=!showMiniMap;mPrev=keyDown('M');// “后室”模式开关if(keyDown('K')&&!kPrev) backroomsMode=!backroomsMode;6 行未改动// 循环选择放置方块类型 ([ 和 ] 键)bool cyclePrev = keyDown(VK_OEM_4); // VK_OEM_4 是 '['bool cycleNext = keyDown(VK_OEM_6); // VK_OEM_6 是 ']'const int min_placeable_type = WALL; // BlockType::WALL (1)const int max_placeable_type = TRAP; // BlockType::TRAP (5)const int num_placeable_types = max_placeable_type - min_placeable_type + 1; // 总共 5 种可放置类型51 行未改动}}}}if (player.health <= 0) {gameOver = true; // 游戏结束,玩家失败gameEndReason = 2; // 设置失败原因}101 行未改动全部展开if (!moved_this_turn && dir_x != 0) {moved_this_turn = tryMoveMonster(m, old_mx, old_my, old_mx + dir_x, old_my);}if (!moved_this_turn && dir_y != 0) {moved_this_turn = tryMoveMonster(m, old_mx, old_my, old_mx, old_my + dir_y);}}}// --- 粒子更新 ---for (int i = particles.size() - 1; i >= 0; --i) {Particle& p = particles[i];p.x += p.vx;p.y += p.vy;p.z += p.vz;p.vz -= PARTICLE_GRAVITY; // 应用重力p.vx *= PARTICLE_FRICTION; // 应用摩擦力p.vy *= PARTICLE_FRICTION;p.vz *= PARTICLE_FRICTION;14 行未改动// 天空 / 天花板COLORREF skyBaseColor = backroomsMode ? RGB(255, 230, 100) : RGB(135,206,235); // 后室模式为黄色调,否则为天蓝色setfillcolor(skyBaseColor);solidrectangle(0, 0, SCREEN_WIDTH, halfH_shifted);// 后室模式: 模拟天花板灯光 (简单的矩形)if(backroomsMode){int light_grid_x_count = 5;int light_grid_y_count = 3;13 行未改动// 地面COLORREF groundBaseColor = backroomsMode ? RGB(180, 160, 50) : RGB(110,110,110); // 后室模式为深黄色调,否则为灰色setfillcolor(groundBaseColor);solidrectangle(0, halfH_shifted, SCREEN_WIDTH, SCREEN_HEIGHT);// 射线投射for(int x=0; x<SCREEN_WIDTH; ++x) {double cameraX = 2 * x / double(SCREEN_WIDTH) - 1; // 屏幕 x 坐标在 [-1, 1] 范围内的映射double rayDirX = player.cosA - player.sinA * cameraX; // 射线方向 X 分量double rayDirY = player.sinA + player.cosA * cameraX; // 射线方向 Y 分量int mapX = int(player.x), mapY = int(player.y); // 射线当前所在的地图块double deltaX = fabs(1 / rayDirX), deltaY = fabs(1 / rayDirY); // 射线在 X/Y 方向前进一格所需距离double sideX, sideY; // 射线到下一个 X/Y 方向边界的距离int stepX, stepY; // 步进方向 (+1 或 -1)if (rayDirX < 0) { stepX = -1; sideX = (player.x - mapX) * deltaX; }else { stepX = 1; sideX = (mapX + 1.0 - player.x) * deltaX; }if (rayDirY < 0) { stepY = -1; sideY = (player.y - mapY) * deltaY; }else { stepY = 1; sideY = (mapY + 1.0 - player.y) * deltaY; }int side; // 击中的是 X 边 (0) 还是 Y 边 (1)int hitType=WALL; // 默认击中墙壁// DDA 算法 (数字差分分析)while(true) {if(sideX < sideY) { sideX += deltaX; mapX += stepX; side = 0; }else { sideY += deltaY; mapY += stepY; side = 1; }hitType = getWorldAt(mapX, mapY); // 获取当前瓷砖类型if(hitType) break; // 击中墙壁或物体则退出}// 计算垂直距离double perp = side==0 ? (mapX - player.x + (1-stepX)/2.0)/rayDirX: (mapY - player.y + (1-stepY)/2.0)/rayDirY;perp = max(perp, 0.1); // 避免除以零或过小的数// 计算墙壁在屏幕上的顶部和底部 Y 坐标// 假设墙壁高度为 1 (从 Z=0 到 Z=1)int wall_top_y = int(halfH_shifted - (1.0 - player.z) * SCREEN_HEIGHT / perp);int wall_bottom_y = int(halfH_shifted - (0.0 - player.z) * SCREEN_HEIGHT / perp);int drawStartY = max(0, wall_top_y); // 实际绘制的起始 Y 坐标 (屏幕范围内)int drawEndY = min(SCREEN_HEIGHT, wall_bottom_y); // 实际绘制的结束 Y 坐标 (屏幕范围内)// 墙壁着色和颜色double shade_factor = 1.0 / (1.0 + perp * perp * 0.01); // 距离衰减着色shade_factor = max(0.0, min(1.0, shade_factor)); // 限制在 0-1 之间COLORREF col;int baseR, baseG, baseB;switch (hitType) {8 行未改动case MONSTER: baseR = 255; baseG = 0; baseB = 0; break; // 红色case TRAP: baseR = 139; baseG = 0; baseB = 0; break; // 暗红色default: baseR = 0; baseG = 0; baseB = 0; break; // 不应出现非空方块}// 应用距离着色col = RGB(static_cast<int>(baseR * shade_factor),static_cast<int>(baseG * shade_factor),static_cast<int>(baseB * shade_factor));setlinecolor(col); // 设置线条颜色line(x, drawStartY, x, drawEndY); // 绘制垂直的墙壁线段}// 粒子渲染 (在墙壁之后绘制,使其显示在墙壁之上)for (const auto& p : particles) {double px_rel = p.x - player.x; // 粒子相对于玩家的 X 坐标double py_rel = p.y - player.y; // 粒子相对于玩家的 Y 坐标5 行未改动// 应用俯仰角旋转到垂直分量double final_pz = pz_rel * player.cosP - rotY_depth * player.sinP; // 旋转后的有效 Zdouble final_depth = rotY_depth * player.cosP + pz_rel * player.sinP; // 旋转后的有效深度if (final_depth > 0.1) { // 粒子在玩家前方double inv_depth = 1.0 / final_depth;int screenX = int(SCREEN_WIDTH / 2 + rotX * SCREEN_HEIGHT * inv_depth); // 计算屏幕 X 坐标int screenY = int(SCREEN_HEIGHT / 2 - final_pz * (SCREEN_HEIGHT/2) * inv_depth); // 计算屏幕 Y 坐标19 行未改动// 应用俯仰角旋转double final_pz = pz_rel * player.cosP - rotY_depth * player.sinP;double final_depth = rotY_depth * player.cosP + pz_rel * player.sinP;if (final_depth > 0.1) { // 怪物在玩家前方double inv_depth = 1.0 / final_depth;int screenX = int(SCREEN_WIDTH / 2 + rotX * SCREEN_HEIGHT * inv_depth);int screenY = int(SCREEN_HEIGHT / 2 - final_pz * (SCREEN_HEIGHT/2) * inv_depth);// 限制屏幕坐标范围if (screenX >= 0 && screenX < SCREEN_WIDTH && screenY >= 0 && screenY < SCREEN_HEIGHT) {// 绘制一个简单的红色矩形作为怪物15 行未改动// 最简单的方法是构建 std::wstring,然后获取其 c_str()。// 或者使用 _stprintf_s 或 _snwprintf 等函数。// 考虑到兼容性,这里使用 _T() 和 string::c_str(),但这依赖于项目字符集设置正确。outtextxy(5, 5, _T(("HP:" + to_string(player.health)).c_str())); // 显示生命值settextcolor(YELLOW);outtextxy(SCREEN_WIDTH - 150, 5, _T(("TREASURES:" + to_string(player.treasures)).c_str())); // 显示宝藏数量long long elapsedTime = (currentFrameTime - startTime) / 1000; // 游戏已进行时间 (秒)settextcolor(WHITE);outtextxy(5, 25, _T(("TIME:" + to_string(elapsedTime) + "s").c_str())); // 显示游戏时间31 行未改动setlinecolor(RGB(150,150,150)); // 修复: 将GREY替换为RGB值rectangle(sx - 1, sy - 1, sx + minimap_draw_width + 1, sy + minimap_draw_height + 1);int cx=static_cast<int>(player.x), cy=static_cast<int>(player.y); // 玩家当前所在地图块for(int dy_idx=0;dy_idx<V;++dy_idx){for(int dx_idx=0;dx_idx<V;++dx_idx){int map_dx = dx_idx - minimap_radius; // 相对于玩家的 X 偏移int map_dy = dy_idx - minimap_radius; // 相对于玩家的 Y 偏移int wx = cx + map_dx;int wy = cy + map_dy;if (minimap_flipped) { // 如果小地图翻转,则水平翻转 X 坐标wx = cx - map_dx;}29 行未改动else if (normalized_angle >= (7 * PI / 8.0) && normalized_angle < (9 * PI / 8.0)) arrow_char_raw = '<';else if (normalized_angle >= (9 * PI / 8.0) && normalized_angle < (11 * PI / 8.0)) arrow_char_raw = '/'; // 左下else if (normalized_angle >= (11 * PI / 8.0) && normalized_angle < (13 * PI / 8.0)) arrow_char_raw = 'v';else arrow_char_raw = '\\'; // 右下settextcolor(RED);settextstyle(12, 0, _T("Consolas")); // 箭头使用较小的字体// 箭头字符是ASCII,不需要_T(),但是 outtextxy 期望 TCHAR*。// 简单处理:将 char 转换为 string,再用 _T()。string arrow_str(1, arrow_char_raw);outtextxy(sx + minimap_radius * tile_size + tile_size/2 - textwidth(_T("M"))/2,sy + minimap_radius * tile_size + tile_size/2 - textheight(_T("M"))/2,_T(arrow_str.c_str()));// 恢复 HUD 字体样式settextstyle(16, 0, _T("Consolas"));10 行未改动// 刷新并准备下一帧FlushBatchDraw();Sleep(10); // 控制帧率,避免过高}// 结束批量绘制并关闭窗口EndBatchDraw();closegraph();12 行未改动453 行未改动全部展开45445545645745845946046146216 行未改动47948048148248348448548648748848949049149249349449543 行未改动5395405415425435445455465479 行未改动55755855956056156256356456556656756856957017 行未改动5885895905915925935945955965975985996006016026036046056066076086096106113 行未改动61561661761861962062162262362462562662762862963063163263363463563663763863964064164236 行未改动6796806816826836846856866876 行未改动69469569669769869970070170251 行未改动754755756757758759760761762101 行未改动全部展开86486586686786886987087187287387487587687787887988088188288388488588688714 行未改动90290390490590690790890991013 行未改动9249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769778 行未改动98698798898999099199299399499599699799899910001001100210035 行未改动10091010101110121013101410151016101719 行未改动103710381039104010411042104310441045104610471048104915 行未改动10651066106710681069107010711072107310741075107631 行未改动110811091110111111121113111411151116111711181119112011211122112329 行未改动115311541155115611571158115911601161116211631164116511661167116810 行未改动11791180118111821183118411851186118712 行未改动453 行未改动全部展开cout << " 1. 无限自动生成地图 (Infinite procedural map)\n";cout << " 2. 加载已有地图 (Load existing map from file)\n";cout << " --------------------------------------------------\n";cout << " 输入编号并回车:";int choice_input = 1; // 默认选择cin >> choice_input;cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 清除行尾的换行符16 行未改动}string fname_base;if (fname_choice == 1) {fname_base = "map1";} else if (fname_choice == 2) {fname_base = "map2";} else {cout << "无效选择,默认为 'map1.txt'.\n";fname_base = "map1";}loadExistingMap(fname_base + ".txt");} else {mapMode = MapMode::Infinite;cout << " 已选择无限自动生成模式。\n";}43 行未改动outtextxy(50, current_y, _T("- 1 键: 在前方空地放置当前选定的方块类型")); current_y += line_height;outtextxy(50, current_y, _T("- [ 键 (左方括号): 循环选择上一个要放置的方块类型")); current_y += line_height;outtextxy(50, current_y, _T("- ] 键 (右方括号): 循环选择下一个要放置的方块类型")); current_y += line_height;outtextxy(50, current_y, _T("- ESC 键: 退出游戏")); current_y += line_height;outtextxy(50, current_y, _T("- + 键: 增大迷你地图尺寸")); current_y += line_height;outtextxy(50, current_y, _T("- - 键: 减小迷你地图尺寸")); current_y += line_height;outtextxy(50, current_y, _T("- R 键: 左右翻转迷你地图显示")); current_y += line_height * 2;outtextxy(50, current_y, _T("--------------------------------------------------")); current_y += line_height;outtextxy(50, current_y, _T("准备好了吗?按下 ENTER 键开始冒险..."));9 行未改动startTime = GetTickCount(); // 记录游戏开始时间BeginBatchDraw(); // 开启批量绘制,避免闪烁// 按键状态变量 (用于检测按键按下/释放,避免重复触发)bool altPrev=false, mPrev=false, xPrev=false, kPrev=false, rPrev=false;bool onePrev=false, spacePrev=false;bool cyclePrevPrev = false, cycleNextPrev = false;bool plusPrev = false, minusPrev = false;long long lastFrameTime = GetTickCount(); // 上一帧的时间// 游戏主循环17 行未改动}}altPrev=alt;if(mouseMode){POINT p; GetCursorPos(&p);int dx=p.x-lastMousePos.x;int dy=p.y-lastMousePos.y; // 用于俯仰角player.angle+=dx*MOUSE_SENSITIVITY;player.pitch-=dy*MOUSE_SENSITIVITY; // 负号用于 Y 轴反转SetCursorPos(lastMousePos.x,lastMousePos.y); // 将鼠标光标重新定位到中心}// 水平视角旋转 (偏航)if(keyDown(VK_LEFT)) player.angle-=KEY_SENSITIVITY;if(keyDown(VK_RIGHT)) player.angle+=KEY_SENSITIVITY;// 垂直视角旋转 (俯仰)if(keyDown(VK_UP)) player.pitch+=PITCH_SENSITIVITY;if(keyDown(VK_DOWN)) player.pitch-=PITCH_SENSITIVITY;player.pitch = max(-MAX_PITCH_ANGLE, min(MAX_PITCH_ANGLE, player.pitch)); // 限制俯仰角度// 更新玩家的三角函数值 (在角度/俯仰角改变后必须更新)player.updateTrig();3 行未改动double currentMoveSpeed = MOVE_SPEED;if (keyDown(VK_SHIFT)) currentMoveSpeed *= 1.5; // 按住 SHIFT 冲刺if(keyDown('W')){ // 前进nx+=player.cosA*currentMoveSpeed;ny+=player.sinA*currentMoveSpeed;}if(keyDown('S')){ // 后退nx-=player.cosA*currentMoveSpeed;ny-=player.sinA*currentMoveSpeed;}if(keyDown('A')){ // 横向左移nx+=player.sinA*currentMoveSpeed;ny-=player.cosA*currentMoveSpeed;}if(keyDown('D')){ // 横向右移nx-=player.sinA*currentMoveSpeed;ny+=player.cosA*currentMoveSpeed;}// 简单的水平碰撞检测// 检查玩家包围盒的四个角double player_half_width = 0.2; // 玩家的有效半宽用于碰撞// 仅检查 X 方向的移动是否会导致碰撞bool can_move_x = (getWorldAt(static_cast<int>(nx - player_half_width), static_cast<int>(player.y - player_half_width)) != WALL &&getWorldAt(static_cast<int>(nx + player_half_width), static_cast<int>(player.y - player_half_width)) != WALL &&36 行未改动player.onGround = true; // 标记为在地面上}// 小地图开关if(keyDown('M')&&!mPrev) showMiniMap=!showMiniMap;mPrev=keyDown('M');// “后室”模式开关if(keyDown('K')&&!kPrev) backroomsMode=!backroomsMode;6 行未改动// 循环选择放置方块类型 ([ 和 ] 键)bool cyclePrev = keyDown(VK_OEM_4); // VK_OEM_4 是 '['bool cycleNext = keyDown(VK_OEM_6); // VK_OEM_6 是 ']'const int min_placeable_type = WALL; // BlockType::WALL (1)const int max_placeable_type = TRAP; // BlockType::TRAP (5)const int num_placeable_types = max_placeable_type - min_placeable_type + 1; // 总共 5 种可放置类型51 行未改动}}}}if (player.health <= 0) {gameOver = true; // 游戏结束,玩家失败gameEndReason = 2; // 设置失败原因}101 行未改动全部展开if (!moved_this_turn && dir_x != 0) {moved_this_turn = tryMoveMonster(m, old_mx, old_my, old_mx + dir_x, old_my);}if (!moved_this_turn && dir_y != 0) {moved_this_turn = tryMoveMonster(m, old_mx, old_my, old_mx, old_my + dir_y);}}}// --- 粒子更新 ---for (int i = particles.size() - 1; i >= 0; --i) {Particle& p = particles[i];p.x += p.vx;p.y += p.vy;p.z += p.vz;p.vz -= PARTICLE_GRAVITY; // 应用重力p.vx *= PARTICLE_FRICTION; // 应用摩擦力p.vy *= PARTICLE_FRICTION;p.vz *= PARTICLE_FRICTION;14 行未改动// 天空 / 天花板COLORREF skyBaseColor = backroomsMode ? RGB(255, 230, 100) : RGB(135,206,235); // 后室模式为黄色调,否则为天蓝色setfillcolor(skyBaseColor);solidrectangle(0, 0, SCREEN_WIDTH, halfH_shifted);// 后室模式: 模拟天花板灯光 (简单的矩形)if(backroomsMode){int light_grid_x_count = 5;int light_grid_y_count = 3;13 行未改动// 地面COLORREF groundBaseColor = backroomsMode ? RGB(180, 160, 50) : RGB(110,110,110); // 后室模式为深黄色调,否则为灰色setfillcolor(groundBaseColor);solidrectangle(0, halfH_shifted, SCREEN_WIDTH, SCREEN_HEIGHT);// 射线投射for(int x=0; x<SCREEN_WIDTH; ++x) {double cameraX = 2 * x / double(SCREEN_WIDTH) - 1; // 屏幕 x 坐标在 [-1, 1] 范围内的映射double rayDirX = player.cosA - player.sinA * cameraX; // 射线方向 X 分量double rayDirY = player.sinA + player.cosA * cameraX; // 射线方向 Y 分量int mapX = int(player.x), mapY = int(player.y); // 射线当前所在的地图块double deltaX = fabs(1 / rayDirX), deltaY = fabs(1 / rayDirY); // 射线在 X/Y 方向前进一格所需距离double sideX, sideY; // 射线到下一个 X/Y 方向边界的距离int stepX, stepY; // 步进方向 (+1 或 -1)if (rayDirX < 0) { stepX = -1; sideX = (player.x - mapX) * deltaX; }else { stepX = 1; sideX = (mapX + 1.0 - player.x) * deltaX; }if (rayDirY < 0) { stepY = -1; sideY = (player.y - mapY) * deltaY; }else { stepY = 1; sideY = (mapY + 1.0 - player.y) * deltaY; }int side; // 击中的是 X 边 (0) 还是 Y 边 (1)int hitType=WALL; // 默认击中墙壁// DDA 算法 (数字差分分析)while(true) {if(sideX < sideY) { sideX += deltaX; mapX += stepX; side = 0; }else { sideY += deltaY; mapY += stepY; side = 1; }hitType = getWorldAt(mapX, mapY); // 获取当前瓷砖类型if(hitType) break; // 击中墙壁或物体则退出}// 计算垂直距离double perp = side==0 ? (mapX - player.x + (1-stepX)/2.0)/rayDirX: (mapY - player.y + (1-stepY)/2.0)/rayDirY;perp = max(perp, 0.1); // 避免除以零或过小的数// 计算墙壁在屏幕上的顶部和底部 Y 坐标// 假设墙壁高度为 1 (从 Z=0 到 Z=1)int wall_top_y = int(halfH_shifted - (1.0 - player.z) * SCREEN_HEIGHT / perp);int wall_bottom_y = int(halfH_shifted - (0.0 - player.z) * SCREEN_HEIGHT / perp);int drawStartY = max(0, wall_top_y); // 实际绘制的起始 Y 坐标 (屏幕范围内)int drawEndY = min(SCREEN_HEIGHT, wall_bottom_y); // 实际绘制的结束 Y 坐标 (屏幕范围内)// 墙壁着色和颜色double shade_factor = 1.0 / (1.0 + perp * perp * 0.01); // 距离衰减着色shade_factor = max(0.0, min(1.0, shade_factor)); // 限制在 0-1 之间COLORREF col;int baseR, baseG, baseB;switch (hitType) {8 行未改动case MONSTER: baseR = 255; baseG = 0; baseB = 0; break; // 红色case TRAP: baseR = 139; baseG = 0; baseB = 0; break; // 暗红色default: baseR = 0; baseG = 0; baseB = 0; break; // 不应出现非空方块}// 应用距离着色col = RGB(static_cast<int>(baseR * shade_factor),static_cast<int>(baseG * shade_factor),static_cast<int>(baseB * shade_factor));setlinecolor(col); // 设置线条颜色line(x, drawStartY, x, drawEndY); // 绘制垂直的墙壁线段}// 粒子渲染 (在墙壁之后绘制,使其显示在墙壁之上)for (const auto& p : particles) {double px_rel = p.x - player.x; // 粒子相对于玩家的 X 坐标double py_rel = p.y - player.y; // 粒子相对于玩家的 Y 坐标5 行未改动// 应用俯仰角旋转到垂直分量double final_pz = pz_rel * player.cosP - rotY_depth * player.sinP; // 旋转后的有效 Zdouble final_depth = rotY_depth * player.cosP + pz_rel * player.sinP; // 旋转后的有效深度if (final_depth > 0.1) { // 粒子在玩家前方double inv_depth = 1.0 / final_depth;int screenX = int(SCREEN_WIDTH / 2 + rotX * SCREEN_HEIGHT * inv_depth); // 计算屏幕 X 坐标int screenY = int(SCREEN_HEIGHT / 2 - final_pz * (SCREEN_HEIGHT/2) * inv_depth); // 计算屏幕 Y 坐标19 行未改动// 应用俯仰角旋转double final_pz = pz_rel * player.cosP - rotY_depth * player.sinP;double final_depth = rotY_depth * player.cosP + pz_rel * player.sinP;if (final_depth > 0.1) { // 怪物在玩家前方double inv_depth = 1.0 / final_depth;int screenX = int(SCREEN_WIDTH / 2 + rotX * SCREEN_HEIGHT * inv_depth);int screenY = int(SCREEN_HEIGHT / 2 - final_pz * (SCREEN_HEIGHT/2) * inv_depth);// 限制屏幕坐标范围if (screenX >= 0 && screenX < SCREEN_WIDTH && screenY >= 0 && screenY < SCREEN_HEIGHT) {// 绘制一个简单的红色矩形作为怪物15 行未改动// 最简单的方法是构建 std::wstring,然后获取其 c_str()。// 或者使用 _stprintf_s 或 _snwprintf 等函数。// 考虑到兼容性,这里使用 _T() 和 string::c_str(),但这依赖于项目字符集设置正确。outtextxy(5, 5, _T(("HP:" + to_string(player.health)).c_str())); // 显示生命值settextcolor(YELLOW);outtextxy(SCREEN_WIDTH - 150, 5, _T(("TREASURES:" + to_string(player.treasures)).c_str())); // 显示宝藏数量long long elapsedTime = (currentFrameTime - startTime) / 1000; // 游戏已进行时间 (秒)settextcolor(WHITE);outtextxy(5, 25, _T(("TIME:" + to_string(elapsedTime) + "s").c_str())); // 显示游戏时间31 行未改动setlinecolor(RGB(150,150,150)); // 修复: 将GREY替换为RGB值rectangle(sx - 1, sy - 1, sx + minimap_draw_width + 1, sy + minimap_draw_height + 1);int cx=static_cast<int>(player.x), cy=static_cast<int>(player.y); // 玩家当前所在地图块for(int dy_idx=0;dy_idx<V;++dy_idx){for(int dx_idx=0;dx_idx<V;++dx_idx){int map_dx = dx_idx - minimap_radius; // 相对于玩家的 X 偏移int map_dy = dy_idx - minimap_radius; // 相对于玩家的 Y 偏移int wx = cx + map_dx;int wy = cy + map_dy;if (minimap_flipped) { // 如果小地图翻转,则水平翻转 X 坐标wx = cx - map_dx;}29 行未改动else if (normalized_angle >= (7 * PI / 8.0) && normalized_angle < (9 * PI / 8.0)) arrow_char_raw = '<';else if (normalized_angle >= (9 * PI / 8.0) && normalized_angle < (11 * PI / 8.0)) arrow_char_raw = '/'; // 左下else if (normalized_angle >= (11 * PI / 8.0) && normalized_angle < (13 * PI / 8.0)) arrow_char_raw = 'v';else arrow_char_raw = '\\'; // 右下settextcolor(RED);settextstyle(12, 0, _T("Consolas")); // 箭头使用较小的字体// 箭头字符是ASCII,不需要_T(),但是 outtextxy 期望 TCHAR*。// 简单处理:将 char 转换为 string,再用 _T()。string arrow_str(1, arrow_char_raw);outtextxy(sx + minimap_radius * tile_size + tile_size/2 - textwidth(_T("M"))/2,sy + minimap_radius * tile_size + tile_size/2 - textheight(_T("M"))/2,_T(arrow_str.c_str()));// 恢复 HUD 字体样式settextstyle(16, 0, _T("Consolas"));10 行未改动// 刷新并准备下一帧FlushBatchDraw();Sleep(10); // 控制帧率,避免过高}// 结束批量绘制并关闭窗口EndBatchDraw();closegraph();12 行未改动