Menu.cpp

 avataruser_6225994
plain_text
a month ago
2.9 kB
2
Indexable
Never
#include <SFML/Graphics.hpp>
#include <iostream>

class Menu {
public:
    Menu(sf::RenderWindow& window) : window_(window) {
        font_.loadFromFile("font.ttf");
        
        title_.setFont(font_);
        title_.setCharacterSize(50);
        title_.setFillColor(sf::Color::White);
        title_.setString("Bomberman Game Menu");
        title_.setPosition(200.f, 100.f);

        startButton_.setFont(font_);
        startButton_.setCharacterSize(30);
        startButton_.setFillColor(sf::Color::White);
        startButton_.setString("Start Game");
        startButton_.setPosition(300.f, 200.f);

        exitButton_.setFont(font_);
        exitButton_.setCharacterSize(30);
        exitButton_.setFillColor(sf::Color::White);
        exitButton_.setString("Exit");
        exitButton_.setPosition(300.f, 250.f);

        selectedItemIndex_ = 0;
    }

    void draw() {
        window_.draw(title_);
        window_.draw(startButton_);
        window_.draw(exitButton_);
    }

    void moveUp() {
        if (selectedItemIndex_ > 0) {
            selectedItemIndex_--;
            updateSelectedItem();
        }
    }

    void moveDown() {
        if (selectedItemIndex_ < 1) {
            selectedItemIndex_++;
            updateSelectedItem();
        }
    }

    int getSelectedItemIndex() const {
        return selectedItemIndex_;
    }

private:
    void updateSelectedItem() {
        if (selectedItemIndex_ == 0) {
            startButton_.setFillColor(sf::Color::Red);
            exitButton_.setFillColor(sf::Color::White);
        } else {
            startButton_.setFillColor(sf::Color::White);
            exitButton_.setFillColor(sf::Color::Red);
        }
    }

    sf::RenderWindow& window_;
    sf::Font font_;
    sf::Text title_;
    sf::Text startButton_;
    sf::Text exitButton_;
    int selectedItemIndex_;
};

int main() {
    sf::RenderWindow window(sf::VideoMode(800, 600), "Bomberman Game");

    Menu menu(window);

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }

            if (event.type == sf::Event::KeyPressed) {
                if (event.key.code == sf::Keyboard::Up) {
                    menu.moveUp();
                }
                if (event.key.code == sf::Keyboard::Down) {
                    menu.moveDown();
                }
                if (event.key.code == sf::Keyboard::Return) {
                    if (menu.getSelectedItemIndex() == 0) {
                        // Start the game
                        std::cout << "Starting the game!" << std::endl;
                    } else {
                        // Exit the game
                        window.close();
                    }
                }
            }
        }

        window.clear();
        menu.draw();
        window.display();
    }

    return 0;
}