Untitled
plain_text
a month ago
6.1 kB
1
Indexable
Never
//menu.h #ifndef _MENU_H #define _MENU_H #include <SFML/Graphics.hpp> #include "chessGame.h" class Menu { private: sf::RenderWindow& m_window; sf::Font m_font; sf::Text m_newGameText; sf::Text m_playWithAIText; sf::Text m_exitText; void handleEvents(); void draw(); bool isGameStarted; //ChessGame chessGame; public: Menu(sf::RenderWindow& window); void run(); }; #endif // !_MENU_H //menu.cpp #include "menu.h" #include<iostream> Menu::Menu(sf::RenderWindow& window) : m_window(window), isGameStarted(false) { if (!m_font.loadFromFile("Textures//arial.ttf")) { std::cout << "Handle font loading error" <<std::endl; } m_newGameText.setString("NEW GAME"); m_playWithAIText.setString("PLAY COMPUTER"); m_exitText.setString("EXIT"); m_newGameText.setFont(m_font); m_playWithAIText.setFont(m_font); m_exitText.setFont(m_font); m_newGameText.setCharacterSize(30); m_playWithAIText.setCharacterSize(30); m_exitText.setCharacterSize(30); m_newGameText.setPosition(300, 200); m_playWithAIText.setPosition(300, 250); m_exitText.setPosition(300, 300); } void Menu::run() { while (m_window.isOpen()) { handleEvents(); draw(); if (isGameStarted) { m_window.close(); ChessGame chess(sf::Color(0xf3bc7aff), sf::Color(0xae722bff)); sf::RenderWindow window(sf::VideoMode(768, 512), "Chess", sf::Style::Titlebar | sf::Style::Close); sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); if (event.type == sf::Event::MouseButtonPressed) { if (event.mouseButton.button == sf::Mouse::Left) { if ((0 <= event.mouseButton.x) && (event.mouseButton.x <= 512) && (0 <= event.mouseButton.y) && (event.mouseButton.y <= 512)) { unsigned int buttonPos{ (event.mouseButton.x / 64) + ((event.mouseButton.y / 64) * (8 * (512 / window.getSize().y))) }; if (!chess.getSelected()) chess.selectPiece(buttonPos); else chess.moveSelected(buttonPos); } else if ((517 <= event.mouseButton.x) && (event.mouseButton.x <= 763) && (5 <= event.mouseButton.y) && (event.mouseButton.y <= 45)) { chess.restart(); } } } } window.clear(); window.draw(chess); window.display(); } } } void Menu::handleEvents() { sf::Event event; while (m_window.pollEvent(event)) { if (event.type == sf::Event::Closed) { m_window.close(); } if (event.type == sf::Event::MouseButtonPressed) { sf::Vector2i mousePos = sf::Mouse::getPosition(m_window); if (m_newGameText.getGlobalBounds().contains(mousePos.x, mousePos.y)) { // Handle "Chơi mới" option clicked // Start a new game or transition to game setup screen isGameStarted = true; } else if (m_playWithAIText.getGlobalBounds().contains(mousePos.x, mousePos.y)) { // Handle "Chơi với máy" option clicked // Transition to AI game mode setup screen isGameStarted = true; } else if (m_exitText.getGlobalBounds().contains(mousePos.x, mousePos.y)) { m_window.close(); } } } } void Menu::draw() { m_window.clear(); // Draw menu options m_window.draw(m_newGameText); m_window.draw(m_playWithAIText); m_window.draw(m_exitText); m_window.display(); } //main /* This code file (main.cpp) contains the main function that runs the game all libraries that must be used have to be included here. */ #include <iostream> #include <SFML/Graphics.hpp> #include <SFML/Audio.hpp> #include "chessGame.h" #include "menu.h" int main(){ //ChessGame chess(sf::Color(0xf3bc7aff),sf::Color(0xae722bff)); //sf::RenderWindow window(sf::VideoMode(768,512), "Chess", sf::Style::Titlebar | sf::Style::Close); sf::RenderWindow window_menu(sf::VideoMode(800, 600), "Chess Game Menu"); Menu menu(window_menu); menu.run(); //setup audio sf::Music musicBackGround; if (!musicBackGround.openFromFile("Audio//Aoe.ogg")) { std::cout << "ERROR" << std::endl; } musicBackGround.setVolume(50); //musicBackGround.play(); // window.setVerticalSyncEnabled(true); /* while(window.isOpen()){ sf::Event event; while(window.pollEvent(event)){ if(event.type == sf::Event::Closed) window.close(); if(event.type == sf::Event::MouseButtonPressed){ if(event.mouseButton.button == sf::Mouse::Left){ if((0 <= event.mouseButton.x) && (event.mouseButton.x <= 512) && (0 <= event.mouseButton.y) && (event.mouseButton.y <= 512)){ unsigned int buttonPos{(event.mouseButton.x/64) + ((event.mouseButton.y/64) * (8 * (512/window.getSize().y)))}; if(!chess.getSelected()) chess.selectPiece(buttonPos); else chess.moveSelected(buttonPos); } else if((517 <= event.mouseButton.x) && (event.mouseButton.x <= 763) && (5 <= event.mouseButton.y) && (event.mouseButton.y <= 45)){ chess.restart(); } } } } window.draw(chess); window.display(); }*/ /*sf::RenderWindow window(sf::VideoMode(800, 600), "Chess Game Menu"); Menu menu(window); menu.run();*/ return 0; }