首页 > 解决方案 > 游戏无法从菜单启动

问题描述

我是游戏开发的新手。我设法使用 C++ 和 SFML 创建了一个简单的游戏(如 Space Invaders)以及一个简单的开始菜单。但是,在主菜单上按“Enter”后,游戏并未启动。如何正确链接?我感谢您的帮助。这不是家庭作业。

main.cpp 代码

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include "GameObjectManager.h"
#include "Menu.h"
using namespace std;

int main()
{
    sf::Texture galaxyBackgroundTexture;
    sf::Sprite galaxyBackground;
    if (!galaxyBackgroundTexture.loadFromFile("Textures/galaxybackground.png")) {
        cout << "Failed to load Image" << endl;
    }

    galaxyBackground.setTexture(galaxyBackgroundTexture);
    sf::RenderWindow window(sf::VideoMode(1200, 800), "Space Invader Test");
    Menu menu(window.getSize().x, window.getSize().y);

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

        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Key::Return)
            {
                menu.GetPressedItem();
                cout << "Play button has been pressed." << endl;
                GameObjectManager* gameObjectManagerManager = new GameObjectManager(&window);
                gameObjectManager->update();
                gameObjectManager->render(window);
            }
            else if (event.type == sf::Event::Closed)
            {
                window.close();
            }
            else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
            {
                window.close();
            }
        }

        window.clear();
        window.draw(galaxyBackground);
        menu.draw(window);
        window.display();
    }
    return 0;
}

菜单.h

#pragma once
#include "SFML/Graphics.hpp"
#define MAX_NUMBER_OF_ITEMS 2

class Menu
{
public:
    Menu(float width, float height);
    ~Menu();

    void draw(sf::RenderWindow& window);
    int GetPressedItem() { return selectedItemIndex; }


private: 
    int selectedItemIndex;
    sf::Font font;
    sf::Text menu[MAX_NUMBER_OF_ITEMS];    
};

菜单.cpp

#include "Menu.h"
Menu::Menu(float width, float height)
{
    if (!font.loadFromFile("arial.ttf"))
    {
        cout << "can't load font" << endl;
    }

    // initialise Menu items
    menu[0].setFont(font);
    menu[0].setColor(sf::Color::Red);
    menu[0].setString("Play");
    menu[0].setPosition(sf::Vector2f(width / 2, height / (MAX_NUMBER_OF_ITEMS + 1) * 1));

    // EXIT
    menu[1].setFont(font);
    menu[1].setColor(sf::Color::White);
    menu[1].setString("Exit");
    menu[1].setPosition(sf::Vector2f(width / 2, height / (MAX_NUMBER_OF_ITEMS + 1) * 2));
}
selectedItemIndex = 0;

Menu::~Menu()
{
}

void Menu::draw(sf::RenderWindow &window)
{
    for (int i = 0; i < MAX_NUMBER_OF_ITEMS; i++)
    {
        window.draw(menu[i]);
    }
}

控制台窗口将打印:

"Play button has been pressed"

但它不会继续游戏。没有其他事情发生。

标签: c++sfmlgame-development

解决方案


发生“窗口重新定义”错误是因为您的两个 RenderWindow 对象具有相同的标识符(即窗口)。您可能想要更改第二个窗口的名称,或者更好地使用相同的窗口。

第二个错误,sf::Text::setColor()被弃用意味着它不再“可用”或“不建议使用”。SFML 有两个新的更好的功能:

sf::Text::setFillColor(): 设置文本的填充。

sf::Text::setOutlineColor(): 给你的文本一个轮廓(你还需要使用 改变粗细setOutlineThickness())。

此外,我建议您将状态机用于不同的场景,而不是两个单独的窗口。它真的没有那么难,并且会帮助你学习更多的东西。您已经通过 gameObjectManager 实现了这一点。您只需要抽象它并为您的菜单类实现它。由于您只有两个场景,您可以简单地使用整数或布尔值在这两个场景之间切换。

编辑:了解您需要对 main.cpp 文件执行的操作

int main()
{
sf::RenderWindow window(sf::VideoMode(1200, 800), "Space Invader Test");
GameObjectManager* gameObjectManagerManager = nullptr;
bool inGame = false; //true = game has started, false = menu screen

while (window.isOpen())//this is the main loop
{
    sf::Event event;

    while (window.pollEvent(event)) //this is the event loop
    {
        if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Key::Return) 
        {
            if(!inGame)
            { 
                if(menu.GetPressedItem() == PlayButton) //assuming your function returns which button in the menu has been pressed
                {
                    cout << "Play button has been pressed." << endl;
                    inGame = true;
                    gameObjectManagerManager = new GameObjectManager(&window);
                }
                else 
                {
                    window.close(); //since the other button is exit
                }
            }
        }
        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();
    window.draw(galaxyBackground);
    
    if(!inGame)menu.draw(window);

    if(inGame) gameObjectManagerManager->render();

    window.display();
}
return 0;

}


推荐阅读