游戏不从菜单启动



我是游戏开发的新手。我设法用C++和SFML创建了一个简单的游戏(比如《太空入侵者》(以及一个简单开始菜单然而,在按下";输入";在主菜单上,游戏没有启动如何正确链接?我感谢你的帮助。这不是家庭作业。

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"

但它并没有进入游戏。没有其他事情发生。

"窗口重新定义";由于两个RenderWindow对象都具有相同的标识符(即窗口(,因此发生错误。您可能想要更改第二个窗口的名称,或者最好使用同一个窗口。

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

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

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

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

编辑:你需要对你的主.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;

}

最新更新