版本比较
迷宫2
正文
增加 59 行删除 59 行
453 行未改动全部展开45445545645745845845946046146216 行未改动47948048148248348348448548648748848949049149149249349449543 行未改动5395405415425435435445455465479 行未改动55755855956056156156256356356456556656556656756856957017 行未改动5885895905915925935945925935945955965975985996005996006016026036026036046056066076066076086096106113 行未改动61561661761861962061962062162262362462362462562662762862762862963063163263163263363463463563663763863863964064164236 行未改动6796806816826836836846856866876 行未改动69469569669769869869970070170251 行未改动754755756757758758759760761762101 行未改动全部展开86486586686786886886987087187287387487587687787887887988088188288388388488588688714 行未改动90290390490590690690790890991013 行未改动9249259269279289289299309319329339349349359369379379389399409419429439449459459469479489489499509519529539549559569569579589599609619629639649659669669679689699699709719729739739749759769778 行未改动98698798898999099099199299399299399499599599699799899999910001001100210035 行未改动100910101011101210131013101410151016101719 行未改动10371038103910401041104110421043104410451045104610471048104915 行未改动1065106610671068106910691070107110721072107310741075107631 行未改动11081109111011111112111211131114111511161117111811191119112011211122112329 行未改动115311541155115611571157115811591160116111621163116411631164116511661167116810 行未改动117911801181118211831183118411851186118712 行未改动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"; 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("- - 键: 减小迷你地图尺寸")); 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 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; 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; 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_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; 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; nx+=player.cosA*currentMoveSpeed; ny+=player.sinA*currentMoveSpeed; } if(keyDown('S')){ // 后退 nx-=player.cosA*currentMoveSpeed; ny-=player.sinA*currentMoveSpeed; nx-=player.cosA*currentMoveSpeed; ny-=player.sinA*currentMoveSpeed; } if(keyDown('A')){ // 横向左移 nx+=player.sinA*currentMoveSpeed; ny-=player.cosA*currentMoveSpeed; nx+=player.sinA*currentMoveSpeed; ny-=player.cosA*currentMoveSpeed; } if(keyDown('D')){ // 横向右移 nx-=player.sinA*currentMoveSpeed; ny+=player.cosA*currentMoveSpeed; 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; 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), 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; // 旋转后的有效 Z 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); // 计算屏幕 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); 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 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, 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 行未改动