【问题标题】:Minesweeper SFML code issue with revealed board显示板的扫雷 SFML 代码问题
【发布时间】:2021-01-30 05:13:37
【问题描述】:

我对这段代码有疑问。这是扫雷游戏。游戏很好,但是当我将光标移到游戏窗口外时,整个棋盘都显示给我了。怎么解决,有大佬知道吗?你能帮我解决这个问题吗?我认为问题可能在于检查光标位置,或者表格的填充方式。

#include<SFML/Graphics.hpp>
#include<time.h>

using namespace sf;


int main()
{
    srand(time(0));

    RenderWindow app(VideoMode(400, 400), "Start The Minesweeper");

    int w = 32;
    int grid[12][12];
    int sgrid[12][12]; // For showing the grid

    Texture t;
    t.loadFromFile("images/tiles.jpg");
    Sprite s(t);

    for(int i = 1; i<=10; i++)
        for (int j = 1; j <= 10; j++) {
            sgrid[i][j] = 10;

            if (rand() % 5 == 0) grid[i][j] = 9;
            else grid[i][j] = 0;
        }

    for (int i = 1; i <= 10; i++)
        for (int j = 1; j <= 10; j++) {
            int n = 0;
            if (grid[i][j] == 9) continue;
            if (grid[i + 1][j] == 9) n++;
            if (grid[i][j + 1] == 9) n++;
            if (grid[i - 1][j] == 9) n++;
            if (grid[i][j - 1] == 9) n++;

            if (grid[i + 1][j + 1] == 9) n++;
            if (grid[i - 1][j - 1] == 9) n++;
            if (grid[i - 1][j + 1] == 9) n++;
            if (grid[i + 1][j - 1] == 9) n++;

            grid[i][j] = n;
        }

    while (app.isOpen())
    {
        Vector2i pos = Mouse::getPosition(app);
        int x = pos.x / w;
        int y = pos.y / w;

        Event e;
        while (app.pollEvent(e))
        {
            if (e.type == Event::Closed)
                app.close();

            if (e.type == Event::MouseButtonPressed)
                if (e.key.code == Mouse::Left) sgrid[x][y] = grid[x][y];
                else if (e.key.code == Mouse::Right) sgrid[x][y] = 11;

        }

        app.clear(Color::White);
        for (int i = 1; i <= 10; i++)
            for (int j = 1; j <= 10; j++) {

                if (sgrid[x][y] == 9) sgrid[i][j] = grid[i][j];

                s.setTextureRect(IntRect(sgrid[i][j] * w, 0, w, w));
                s.setPosition(i * w, j * w);
                app.draw(s);
            }

        app.display();
        


    }


    
}

image to this code

【问题讨论】:

    标签: c++ sfml minesweeper


    【解决方案1】:

    无论是否按下鼠标按钮,您似乎都在更新 x 和 y 网格位置,在这一行:

    if (sgrid[x][y] == 9) sgrid[i][j] = grid[i][j];
    

    此外,您似乎正在检查鼠标单击位置处的显示 sgrid[x][y] 而不是数据 grid[x][y]

    您最有可能要做的是为单击鼠标按钮时添加一个标志,并且仅在单击按钮时检查是否有地雷。我在下面的代码中做了一些小改动,试一试:

    #include<SFML/Graphics.hpp>
    #include<time.h>
    
    using namespace sf;
    
    
    int main()
    {
        srand(time(0));
    
        RenderWindow app(VideoMode(400, 400), "Start The Minesweeper");
    
        int w = 32;
        int grid[12][12];
        int sgrid[12][12]; // For showing the grid
    
        Texture t;
        t.loadFromFile("images/tiles.jpg");
        Sprite s(t);
    
        for(int i = 1; i<=10; i++)
            for (int j = 1; j <= 10; j++) {
                sgrid[i][j] = 10;
    
                if (rand() % 5 == 0) grid[i][j] = 9;
                else grid[i][j] = 0;
            }
    
        for (int i = 1; i <= 10; i++)
            for (int j = 1; j <= 10; j++) {
                int n = 0;
                if (grid[i][j] == 9) continue;
                if (grid[i + 1][j] == 9) n++;
                if (grid[i][j + 1] == 9) n++;
                if (grid[i - 1][j] == 9) n++;
                if (grid[i][j - 1] == 9) n++;
    
                if (grid[i + 1][j + 1] == 9) n++;
                if (grid[i - 1][j - 1] == 9) n++;
                if (grid[i - 1][j + 1] == 9) n++;
                if (grid[i + 1][j - 1] == 9) n++;
    
                grid[i][j] = n;
            }
    
        while (app.isOpen())
        {
            Vector2i pos = Mouse::getPosition(app);
            int x = pos.x / w;
            int y = pos.y / w;
            bool mbleft = false;
    
            Event e;
            while (app.pollEvent(e))
            {
                if (e.type == Event::Closed)
                    app.close();
    
                if (e.type == Event::MouseButtonPressed)
                    if (e.key.code == Mouse::Left)
                    {
                        sgrid[x][y] = grid[x][y];
                        mbleft = true;
                    }
                    else if (e.key.code == Mouse::Right) sgrid[x][y] = 11;
    
            }
    
            app.clear(Color::White);
            for (int i = 1; i <= 10; i++)
                for (int j = 1; j <= 10; j++) {
    
                    if (mbleft && sgrid[x][y] == 9) sgrid[i][j] = grid[i][j];
    
                    s.setTextureRect(IntRect(sgrid[i][j] * w, 0, w, w));
                    s.setPosition(i * w, j * w);
                    app.draw(s);
                }
    
            app.display();
            
    
    
        }
    
    
        
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-25
      • 1970-01-01
      相关资源
      最近更新 更多