【问题标题】:Background Image overlapping Main Menu Items背景图像与主菜单项重叠
【发布时间】:2021-03-29 06:44:46
【问题描述】:

我最近才开始学习游戏开发。我设法使用 C++ 和 SFML 开发了一个简单的游戏(类似于 Space Shooters)和一个简单的开始菜单。 (这不是家庭作业)

问题:主菜单屏幕的背景图像与我的文本/菜单项重叠。但是游戏屏幕的背景图片没有与我的游戏项目重叠。

相关代码: main.cpp

int main()
{
    //Background Image for Game Screen
    sf::Texture galaxyBackgroundTexture, menuBackgroundTexture;
    sf::Sprite galaxyBackground, menuBackground;
    sf::Vector2u TextureSize;  //Added to store texture size.
    sf::Vector2u WindowSize;   //Added to store window size.

    sf::RenderWindow window(sf::VideoMode(1200, 800), "Space Invader Team 62");

    if (!galaxyBackgroundTexture.loadFromFile("Textures/galaxy_background.png"))
    {
        cout << "Failed to load Image" << endl;
    }
    else
    {
        TextureSize = galaxyBackgroundTexture.getSize(); //Get size of texture
        WindowSize = window.getSize();             //Get size of window

        float ScaleX = (float)WindowSize.x / TextureSize.x;
        float ScaleY = (float)WindowSize.y / TextureSize.y;     //Calculate scale

        galaxyBackground.setTexture(galaxyBackgroundTexture);
        galaxyBackground.setScale(ScaleX, ScaleY);      //Set scale
    }


    // Menu
    menuBackground.setTexture(menuBackgroundTexture);
    Menu menu(window.getSize().x, window.getSize().y);
    GameObjectManager* gameObjectManagerManager = nullptr;

    bool inGame = false; //true = game has started, false = menu screen

    //  window.setFramerateLimit(144);
    while (window.isOpen()) // main loop
    {
        sf::Event event;

        while (window.pollEvent(event)) // event loop
        {
            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Key::Return)
            {
                if (!inGame)
                {
                    cout << "Play button has been pressed." << endl;
                    inGame = true;
                    gameObjectManagerManager = new GameObjectManager(&window);
                }
            }
            if (event.type == sf::Event::Closed) window.close();

            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) window.close();

        }

        //this is the place where you call your updates
        if (inGame) gameObjectManagerManager->update();

        window.clear();

        if (!inGame)menu.draw(window);
        {
            window.draw(menuBackground);
        }

        if (inGame)
        {
            window.draw(galaxyBackground);
            gameObjectManagerManager->render(window);
        }

        window.display();
    }
    return 0;
}

【问题讨论】:

    标签: c++ visual-studio sfml game-development


    【解决方案1】:

    您的背景图片是在项目之后绘制的。您需要以正确的顺序绘制它们。 改变这个:

    if (!inGame)menu.draw(window);
        {
            window.draw(menuBackground);
        }
    

    对此:

    if (!inGame)
        {
            window.draw(menuBackground);
            menu.draw(window);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 2018-04-23
      相关资源
      最近更新 更多