【问题标题】:Sprite Texture not dividing the pieces from the image C++Sprite Texture没有从图像C ++中分割碎片
【发布时间】:2020-03-09 05:54:51
【问题描述】:

美好的一天,我正在尝试使用 SFML 从头开始​​创建一个国际象棋游戏,但我遇到的问题是我最终得到了下面附上的图片,而不是一个个的国际象棋。现在我并不担心游戏的功能或用户命令,我只想运行我的代码并查看正常的棋盘。

这是我的代码输出:

这是我的 ma​​in.cpp

#include <SFML/Graphics.hpp>
#include <time.h>
using namespace sf;

int size = 56;
Sprite f[32];
int board[8][8] =
{ -1,-2,-3,-4,-5,-3,-2,-1,
 -6,-6,-6,-6,-6,-6,-6,-6,
  0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0,
  6, 6, 6, 6, 6, 6, 6, 6,
  1, 2, 3, 4, 5, 3, 2, 1 };

void loadPosition() {
    int num = 0 ;
    for(int i = 0; i < 8; i++)
        for (int j = 0; j < 8; j++) {
            int n = board[i][j];
            if (!n)continue;
            int x = abs(n) - 1;
            int y = n > 0 ? 1 : 0;
            f[num].setTextureRect(IntRect(size * x, size * y, size, size));
            f[num].setPosition(size * j, size * i);
            num++;
        }
}

int main()
{
    RenderWindow game(VideoMode(453, 453), "Suhaib-Chess");
    Texture t1,t2;
    t1.loadFromFile("img/pieces.png");
    t2.loadFromFile("img/board0.png");
    Sprite s;
    Sprite sBoard(t2);
    s.setTexture(t1);

    for (int i = 0; i < 32; i++)f[i].setTexture(t1);
    loadPosition;

    bool isMove = false;
    float dx = 0;
    float dy = 0;

    while (game.isOpen()) {
        Vector2i position = Mouse::getPosition(game);
        Event e;
        while (game.pollEvent(e)) {
            if (e.type == Event::Closed) 
                game.close();

            if(e.type == Event::MouseButtonPressed)
                if(e.key.code == Mouse::Left)
                    if (s.getGlobalBounds().contains(position.x, position.y)) {
                        isMove = true;
                        dx = position.x - s.getPosition().x;
                        dy = position.y - s.getPosition().y;
                    }
            if (e.type == Event::MouseButtonReleased)
                if (e.key.code == Mouse::Left)
                    isMove = false; 
        }
        if (isMove == true) s.setPosition(position.x - dx, position.y - dy);
        game.clear();
        game.draw(sBoard);
        for (int i = 0; i < 32; i++)game.draw(f[i]);
        game.display();

    }
    return 0;
}

我正在尝试使用的图片:

【问题讨论】:

  • 你能提供board0.png的链接吗?

标签: c++ textures sprite sfml chess


【解决方案1】:

board 数组初始化似乎错误。用这样的方法再试一次:

    int board[8][8] = {
      { -1,-2,-3,-4,-5,-3,-2,-1 },
      { -6,-6,-6,-6,-6,-6,-6,-6 },
      {  0, 0, 0, 0, 0, 0, 0, 0 },
      {  0, 0, 0, 0, 0, 0, 0, 0 },
      {  0, 0, 0, 0, 0, 0, 0, 0 },
      {  0, 0, 0, 0, 0, 0, 0, 0 },
      {  6, 6, 6, 6, 6, 6, 6, 6 },
      {  1, 2, 3, 4, 5, 3, 2, 1 }
    };

解释:您显然是在尝试声明一个二维数组,但您使用一个巨大的 64 项数组而不是 8 项 8 项数组来初始化它。它后来在 loadPosition 方法中弄乱了你的位置。

您的项目看起来很有希望!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-20
    • 2012-02-13
    • 2019-01-09
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    相关资源
    最近更新 更多