SFML C++ 游戏开发 - 洗牌过程中精灵放置和裁剪的问题

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

我正在使用 C++ 中的 SFML 开发一款游戏,其中遇到了与洗牌过程中精灵放置和裁剪相关的问题。精灵没有位于游戏板单元的中心,并且侧面似乎有裁剪。该问题特别出现在洗牌过程中,并且初始放置工作正常。我已经尝试调整放置逻辑和比例计算,但问题仍然存在。

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <cstdlib>
#include <iostream>
#include <iomanip>

static sf::RectangleShape board[8][8];
static sf::Texture textures[7];
static sf::Sprite sprites[8][8];
int highlightedRow = 0;
int highlightedCol = 0;
const float cellSize = 62.0f;


// Initialize random seed
void RandomSeed()
{
    std::srand(std::time(0));
}
// Center the sprite within the board block
void spriteScaleCenter(int row, int col, float scale)
{
    sprites[row][col].setScale(scale, scale);
    float xOffset = (cellSize - sprites[row][col].getGlobalBounds().width) / 2.0f;
    float yOffset = (cellSize - sprites[row][col].getGlobalBounds().height) / 2.0f;
    sprites[row][col].setPosition(board[row][col].getPosition().x + xOffset, board[row][col].getPosition().y + yOffset);
}

// Stores random images to textures and sets the sprite textures
void randTexturesSprites()
{
    // Stores images in textures array
    if (!textures[0].loadFromFile("image0.png"))
    {
        std::cout << "Failed to load texture: " << "image0.png" << std::endl;
        return;
    }
    if (!textures[1].loadFromFile("image1.png"))
    {
        std::cout << "Failed to load texture: " << "image1.png" << std::endl;
        return;
    }
    if (!textures[2].loadFromFile("image2.png"))
    {
        std::cout << "Failed to load texture: " << "image2.png" << std::endl;
        return;
    }
    if (!textures[3].loadFromFile("image3.png"))
    {
        std::cout << "Failed to load texture: " << "image3.png" << std::endl;
        return;
    }
    if (!textures[4].loadFromFile("image4.png"))
    {
        std::cout << "Failed to load texture: " << "image4.png" << std::endl;
        return;
    }
    if (!textures[5].loadFromFile("image5.png"))
    {
        std::cout << "Failed to load texture: " << "image5.png" << std::endl;
        return;
    }
    if (!textures[6].loadFromFile("image6.png"))
    {
        std::cout << "Failed to load texture: " << "image6.png" << std::endl;
        return;
    }


    // Assign random textures to each sprite and set scale
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            board[i][j].setSize(sf::Vector2f(cellSize, cellSize));
            board[i][j].setPosition((i + 7.9) * cellSize, (j + 0.2) * cellSize);
            if ((i + j) % 2 == 0)
            {
                board[i][j].setFillColor(sf::Color(43, 42, 42)); // Grey
            }
            else
            {
                board[i][j].setFillColor(sf::Color(0, 0, 0)); // Black
            }

            int randomTextureIndex = rand() % 7;
            sprites[i][j].setTexture(textures[randomTextureIndex]);

            // Set the scale of the sprites
            float scale = cellSize / static_cast<float>(textures[randomTextureIndex].getSize().x);
            scale -= 0.07f;
            sprites[i][j].setScale(scale, scale);

            spriteScaleCenter(i, j, scale);
        }
    }
}



int main()
{
    
    // Set up the window
    sf::RenderWindow window(sf::VideoMode(1000, 600), "Blitz", sf::Style::Close);

    RandomSeed();

    // Set up the board
    randTexturesSprites();


    // Game loop
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }
            else if (event.type == sf::Event::KeyPressed)
            {
                randTexturesSprites();
            }
        }

        // Clear the window
        window.clear();

        // Set the background color
        window.clear(sf::Color(234, 182, 118)); // Change to your desired background color

        // Draw the board with highlight
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                window.draw(board[i][j]);
                window.draw(sprites[i][j]);
            }
        }

        // Display the content
        window.display();

    }

    return 0;
}


程序第一次运行时: When the program is run for the first time

我希望在洗牌过程中精灵能够准确地居中于游戏板的每个单元格内,而不会在两侧进行任何裁剪。

当图像被打乱时: When images are shuffled

c++ sprite game-development sfml positioning
1个回答
0
投票

我在 SFML C++ 游戏开发项目中遇到了一个问题,游戏板上的精灵没有在单元格内居中,并且侧面有裁剪,特别是在洗牌期间。最初的放置工作正常,但洗牌导致了问题。

经过一番调试和调整,我找到了解决问题的方法。该问题与变量和数组的初始化有关。洗牌时,不仅要重置精灵位置,还要重置纹理、棋盘和精灵数组,以确保干净的状态。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.