You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用SFML开发扫雷游戏时窗口缩放异常问题

扫雷游戏难度切换后的显示异常问题

我正在开发一款扫雷游戏,当前在实现用户切换游戏难度的功能。已设置一个可正常工作的下拉菜单,用户选择难度后会修改行数、列数、雷数以及窗口宽高,相关逻辑写在游戏循环内:

// Draw the dropdown options if the dropdown is open
if (isDropdownOpen)
{
    window.draw(dropdownBackground);
    for (auto& option : dropdownOptions)
    {
        window.draw(option);
        if (checkButtonPressed(window, option))
        {
            std::string difficulty = option.getString();
            numRows = difficultyMap[difficulty][0];
            numCols = difficultyMap[difficulty][1];
            numMines = difficultyMap[difficulty][2];
            windowWidth = CELL_SIZE * numRows;
            windowHeight = CELL_SIZE * numCols + MENU_HEIGHT;
        }
    }
}

用户可继续当前难度游戏,直到点击重置按钮,点击后会调整窗口尺寸并重新初始化所有内容:

if (checkButtonPressed(window, resetButton))
{
    window.setSize(sf::Vector2u(windowWidth, windowHeight));
    gameGrid = initGameGrid(numRows, numCols, numMines);
    guiGrid = initGuiGrid();
    setupMenu(font, menuBackground, resetButton, gameButton, dropdownOptions, dropdownBackground);

    gameWon = false;
    gameLost = false;
}

但出现了异常:初始窗口显示正常,切换到9×9的初级难度后,窗口内容异常缩小,CELL_SIZE保持恒定,但内容仿佛被拉伸填充整个窗口,点击窗口右下角空白区域可触发对应单元格的reveal操作。

补充drawGrid函数代码:

void drawGrid(sf::RenderWindow& window, const std::vector<std::vector<sf::Sprite>>& grid)
{
    for (int i = 0; i < grid.size(); i++)
    {
        for (int j = 0; j < grid[0].size(); j++)
        {
            window.draw(grid[i][j]);
        }
    }
}

内容的提问来源于stack exchange,提问作者neo gru

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 02:32:37