有人能找出 CHESS 的 sfml 代码有什么问题吗

问题描述 投票:0回答:1

抱歉写了糟糕的代码,我是 cpp 新手 所以问题是无论我做什么,精灵位置都没有改变。我无法弄清楚。

#include \<SFML/Graphics.hpp\>
#include \<vector\>
#include \<string\>
#include\<iostream\>
#include \<unistd.h\>

int main() {
// Create a window
sf::RenderWindow window(sf::VideoMode(800, 800), "Chess by devtony");
sf::RectangleShape board\[8\]\[8\];
sf::Color blck(128,128,128);
sf::Color wth(255,204,204);

    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
    {
        board[i][j].setPosition(i*100,j*100);
        board[i][j].setSize(sf::Vector2f(100, 100));
       board[i][j].setFillColor((i + j) % 2 == 0 ? wth : blck);
        
       
    }
    }

sf::Texture pieces\[4\]\[8\];
for (int i = 0; i \< 8; i++)
{
if (!pieces\[1\]\[i\].loadFromFile("assets/w_pawn.png")) {
std::cout \<\< "Error loading image!" \<\< std::endl;
return EXIT_FAILURE;
}
}

//rooks
for (int i = 0; i \< 8; i++)
{
if (!pieces\[2\]\[i\].loadFromFile("assets/b_pawn.png")) {
std::cout \<\< "Error loading image!" \<\< std::endl;
return EXIT_FAILURE;
}
}
if (!pieces\[0\]\[0\].loadFromFile("assets/w_rook.png")) {
std::cout \<\< "Error loading image!" \<\< std::endl;
return EXIT_FAILURE;
}
if (!pieces\[0\]\[7\].loadFromFile("assets/w_rook.png")) {
std::cout \<\< "Error loading image!" \<\< std::endl;
return EXIT_FAILURE;
}
if (!pieces\[3\]\[0\].loadFromFile("assets/b_rook.png")) {
std::cout \<\< "Error loading image!" \<\< std::endl;
return EXIT_FAILURE;
}
if (!pieces\[3\]\[7\].loadFromFile("assets/b_rook.png")) {
std::cout \<\< "Error loading image!" \<\< std::endl;
return EXIT_FAILURE;
}

//bishop

if (!pieces\[0\]\[2\].loadFromFile("assets/w_bishop.png")) {
std::cout \<\< "Error loading image!" \<\< std::endl;
return EXIT_FAILURE;
}
if (!pieces\[0\]\[5\].loadFromFile("assets/w_bishop.png")) {
std::cout \<\< "Error loading image!" \<\< std::endl;
return EXIT_FAILURE;
}
if (!pieces\[3\]\[2\].loadFromFile("assets/b_bishop.png")) {
std::cout \<\< "Error loading image!" \<\< std::endl;
return EXIT_FAILURE;
}
if (!pieces\[3\]\[5\].loadFromFile("assets/b_bishop.png")) {
std::cout \<\< "Error loading image!" \<\< std::endl;
return EXIT_FAILURE;
}

//horse
if (!pieces\[0\]\[1\].loadFromFile("assets/w_knight.png")) {
std::cout \<\< "Error loading image!" \<\< std::endl;
return EXIT_FAILURE;
}
if (!pieces\[0\]\[6\].loadFromFile("assets/w_knight.png")) {
std::cout \<\< "Error loading image!" \<\< std::endl;
return EXIT_FAILURE;
}
if (!pieces\[3\]\[1\].loadFromFile("assets/b_knight.png")) {
std::cout \<\< "Error loading image!" \<\< std::endl;
return EXIT_FAILURE;
}
if (!pieces\[3\]\[6\].loadFromFile("assets/b_knight.png")) {
std::cout \<\< "Error loading image!" \<\< std::endl;
return EXIT_FAILURE;
}

//queen

if (!pieces\[0\]\[4\].loadFromFile("assets/w_queen.png")) {
std::cout \<\< "Error loading image!" \<\< std::endl;
return EXIT_FAILURE;
}
if (!pieces\[0\]\[3\].loadFromFile("assets/w_king.png")) {
std::cout \<\< "Error loading image!" \<\< std::endl;
return EXIT_FAILURE;
}
if (!pieces\[3\]\[3\].loadFromFile("assets/b_queen.png")) {
std::cout \<\< "Error loading image!" \<\< std::endl;
return EXIT_FAILURE;
}
if (!pieces\[3\]\[4\].loadFromFile("assets/b_king.png")) {
std::cout \<\< "Error loading image!" \<\< std::endl;
return EXIT_FAILURE;
}

//king

// Create a sprite
sf::Sprite sprites\[4\]\[8\];
for (int i = 0; i \< 4; i++)
{
for (int j = 0; j \< 8; j++)
{
sprites\[i\]\[j\].setTexture(pieces\[i\]\[j\]);
sprites\[i\]\[j\].setScale(0.5,0.5);
sprites\[i\]\[j\].setOrigin(50,50);

    }

}

// Flag to check if a piece is selected

bool isPieceSelected = false;
sf::Sprite\* selectedPiece = nullptr;
sf::Vector2f clickOffset;

// Main loop
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) {

    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 8; j++) {
            if (sprites[i][j].getGlobalBounds().contains(window.mapPixelToCoords(sf::Mouse::getPosition(window)))) {
              
                isPieceSelected = true;
                selectedPiece = &sprites[i][j];
                clickOffset = window.mapPixelToCoords(sf::Mouse::getPosition(window)) - selectedPiece->getPosition();
                break;
            }
        }
        if (isPieceSelected) break;
    }

}
else if (event.type == sf::Event::MouseMoved && isPieceSelected) {

sf::Vector2f newPosition = window.mapPixelToCoords(sf::Mouse::getPosition(window)) - clickOffset;
std::cout \<\< "New Position: " \<\< newPosition.x \<\< ", " \<\< newPosition.y \<\< std::endl;
selectedPiece-\>setPosition(newPosition.x,newPosition.y);

}
else if (event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left) {

    isPieceSelected = false;

}

}

    // Clear window
    window.clear();
    
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
    {
        window.draw(board[i][j]);
        
        
        
        
    }
    }
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 4; j++)
    {
    
      if (j<2)
      {
        sprites[j][i].setPosition(sf::Vector2f(100*(i+1)-50,100*(j+1)-50));
      }else{
        sprites[j][i].setPosition(sf::Vector2f(100*(i+1)-50,100*(j+5)-50));
      }
      
        
      
     
      
       
        
        
        
        
    }
    }
    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 8; j++)
    {
        window.draw(sprites[i][j]);
        
        
        
        
    }
    }
    // Draw the sprite
    
    // Display window contents
    window.display();

}

return 0;
}

我正在使用 C++ 中的 SFML 开发国际象棋游戏,并且遇到了拖放精灵的问题。我实现了一项功能,允许玩家通过在棋盘上单击并拖动棋子来移动棋子。然而,精灵并没有像预期那样随着鼠标拖动而移动。

单击时会正确检测到精灵,并且 isPieceSelected 标志设置为 true。计算 clickOffset 是为了保持相对于鼠标光标的正确位置。但是,当我尝试拖动精灵时,它不跟随鼠标。窗口已聚焦,并且没有设置可能影响响应能力的帧速率限制。

我尝试过使用 setPosition 和 move 方法来更新精灵的位置,但都没有解决问题。尽管没有错误,但精灵根本没有移动,并且程序编译成功。

sfml chess
1个回答
0
投票

您的问题是您在窗口循环的每次迭代中将棋子的位置设置在其起始位置。这发生在循环中:

        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 4; j++)
            {

                if (j < 2)
                {
                    sprites[j][i].setPosition(sf::Vector2f(100 * (i + 1) - 50, 100 * (j + 1) - 50));
                }
                else {
                    sprites[j][i].setPosition(sf::Vector2f(100 * (i + 1) - 50, 100 * (j + 5) - 50));
                }
            }
        }

这可以通过引入一个初始化函数来轻松解决,该函数接受精灵矩阵并仅设置其原始位置一次。

创建一个这样的函数:

void setPiecesInStartingPosition(sf::Sprite(&sprites)[4][8])

并将初始化代码移至函数中,即:

    for (int i = 0; i < 8; i++)
{
    for (int j = 0; j < 4; j++)
    {

        if (j < 2)
        {
            sprites[j][i].setPosition(sf::Vector2f(100 * (i + 1) - 50, 100 * (j + 1) - 50));
        }
        else {
            sprites[j][i].setPosition(sf::Vector2f(100 * (i + 1) - 50, 100 * (j + 5) - 50));
        }
    }
}

最后,确保只在开始 while 循环之前的某个地方调用它一次。也许在创建精灵矩阵之后立即是一个好主意。

另外,我建议删除break语句并将布尔检查移到for循环上方

   for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 8; j++) {
        if (sprites[i][j].getGlobalBounds().contains(window.mapPixelToCoords(sf::Mouse::getPosition(window)))) {
          
            isPieceSelected = true;
            selectedPiece = &sprites[i][j];
            clickOffset = window.mapPixelToCoords(sf::Mouse::getPosition(window)) - selectedPiece->getPosition();
            break;
        }
    }
    if (isPieceSelected) break;

所以像这样:

if (!isPieceSelected) {
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 8; j++) {
                        if (sprites[i][j].getGlobalBounds().contains(window.mapPixelToCoords(sf::Mouse::getPosition(window)))) {

                            isPieceSelected = true;
                            selectedPiece = &sprites[i][j];
                            clickOffset = window.mapPixelToCoords(sf::Mouse::getPosition(window)) - selectedPiece->getPosition();
                            break;
                        }
                    }
                }
            }
© www.soinside.com 2019 - 2024. All rights reserved.