Fork Copy #include "Loop.h" // Initialization functions void Loop::initVariables() { srand(static_cast(time(nullptr))); this->isEnd = false; this->isMenu = true; this->isPlaying = false; this->isMultiplayer = false; this->isPaused = false; this->isGameOverScreen = false; this->color_background = sf::Color(250, 248, 239); this->background_moving_speed = 300.f; this->currentLocation = 0; this->distance = 0.f; this->speedMultiplier = 1.f; this->scoreMultiplier = 1.f; this->score = 0.f; this->speed = 100.f; this->text_test.setFont(this->font); this->text_test.setCharacterSize(32u); this->text_test.setFillColor(sf::Color::White); this->text_test.setPosition(sf::Vector2f(20.f, 20.f)); // SỬA: Căn chỉnh văn bản tạm dừng và tối ưu giao diện this->pause_text.setFont(this->font); this->pause_text.setString("Paused"); this->pause_text.setCharacterSize(64u); this->pause_text.setFillColor(sf::Color::Red); this->pause_text.setPosition(sf::Vector2f(860.f, 300.f)); // Căn giữa hơn (1920/2 - độ dài text) this->pause_resume_text.setFont(this->font); this->pause_resume_text.setString("Resume"); this->pause_resume_text.setCharacterSize(48u); this->pause_resume_text.setFillColor(sf::Color::White); this->pause_resume_text.setPosition(sf::Vector2f(880.f, 450.f)); // Căn giữa, cách đều this->pause_exit_text.setFont(this->font); this->pause_exit_text.setString("Exit"); this->pause_exit_text.setCharacterSize(48u); this->pause_exit_text.setFillColor(sf::Color::White); this->pause_exit_text.setPosition(sf::Vector2f(880.f, 550.f)); // Căn giữa, cách đều this->pause_overlay.setSize(sf::Vector2f(static_cast(this->window->getSize().x), static_cast(this->window->getSize().y))); this->pause_overlay.setFillColor(sf::Color(0, 0, 0, 150)); // SỬA: Tăng độ mờ cho rõ hơn this->selected_pause_option = 0; // Mặc định chọn Resume this->car = new PlayerCar(AssetManager::GetTexture("../Textures/Samochody/player1.png"), sf::Vector2f(250, 250), this->window, &this->isMenu, sf::Keyboard::A, sf::Keyboard::D, sf::Keyboard::W, sf::Keyboard::S); this->car2 = new PlayerCar(AssetManager::GetTexture("../Textures/Samochody/player2.png"), sf::Vector2f(250, 250), this->window, &this->isMenu, sf::Keyboard::Left, sf::Keyboard::Right, sf::Keyboard::Up, sf::Keyboard::Down); this->background = new Background(this->window->getSize(), &this->currentLocation, &this->distance, &this->speedMultiplier, &this->scoreMultiplier, &this->score, &this->speed); this->collisionProcessing = new CollisionProcessing(this->car, this->background, this->background->getMapBorders(), this->background->getRoadBorders(), &this->currentLocation); this->collisionProcessing2 = new CollisionProcessing(this->car2, this->background, this->background->getMapBorders(), this->background->getRoadBorders(), &this->currentLocation); this->gui = new GUI(sf::Vector2f(static_cast(this->window->getSize().x), static_cast(this->window->getSize().y)), this->background->getMapBorders(), this->locationNames, &this->currentLocation, &this->score, &this->scoreMultiplier, &this->distance, &this->speed, this->car, &this->isEnd); this->window->setMouseCursorVisible(false); this->backgroundMusic.openFromFile("../sound/menu.wav"); this->backgroundMusic.setLoop(true); this->ingameMusic.openFromFile("../sound/ingame.wav"); this->ingameMusic.setLoop(true); } void Loop::updatePollEvents() { sf::Event ev; while (this->window->pollEvent(ev)) { if (ev.type == sf::Event::Closed) { this->window->close(); } if (!this->isEnd && !this->isGameOverScreen && this->isPlaying) { if (ev.type == sf::Event::KeyPressed) { // SỬA: Chỉ xử lý khi phím được nhấn (không lặp lại) if (ev.key.code == sf::Keyboard::Escape) { this->isPaused = !this->isPaused; if (this->isPaused) { this->selected_pause_option = 0; // Đặt lại về Resume std::cout << "Paused: Showing pause menu" << std::endl; // Debug } } if (this->isPaused) { if (ev.key.code == sf::Keyboard::Up && this->selected_pause_option > 0) { this->selected_pause_option = 0; // Resume std::cout << "Selected: Resume" << std::endl; // Debug } else if (ev.key.code == sf::Keyboard::Down && this->selected_pause_option < 1) { this->selected_pause_option = 1; // Exit std::cout << "Selected: Exit" << std::endl; // Debug } else if (ev.key.code == sf::Keyboard::Enter) { if (this->selected_pause_option == 0) { this->isPaused = false; std::cout << "Resume game" << std::endl; // Debug } else if (this->selected_pause_option == 1) { this->isPaused = false; this->isPlaying = false; this->isMenu = true; this->backgroundMusic.play(); this->ingameMusic.stop(); // Reset game state delete this->background; this->background = nullptr; delete this->collisionProcessing; this->collisionProcessing = nullptr; if (this->isMultiplayer) { delete this->collisionProcessing2; this->collisionProcessing2 = nullptr; if (this->car2 != nullptr) { this->car2->reset(); // SỬA: Kiểm tra isMultiplayer } } delete this->gui; this->gui = nullptr; if (this->car != nullptr) { this->car->reset(); } std::cout << "Exit to menu" << std::endl; // Debug } } } } } } } void Loop::render() { this->window->clear(sf::Color::Black); if (this->isGameOverScreen) { std::cout << "Rendering game over sprite" << std::endl; this->window->draw(this->game_over_sprite); sf::Text enterText; enterText.setFont(this->font); enterText.setString("Nhấn Enter để tiếp tục"); enterText.setCharacterSize(32u); enterText.setFillColor(sf::Color::White); enterText.setPosition(700.f, 600.f); this->window->draw(enterText); } else { this->background->render(*this->window); this->car->render(*this->window); if (this->isMultiplayer) this->car2->render(*this->window); this->gui->render(*this->window); this->window->draw(this->text_test); if (this->isPaused) { this->window->draw(this->pause_overlay); this->window->draw(this->pause_text); // SỬA: Tô màu tùy chọn được chọn và căn chỉnh this->pause_resume_text.setFillColor(this->selected_pause_option == 0 ? sf::Color::Yellow : sf::Color::White); this->pause_exit_text.setFillColor(this->selected_pause_option == 1 ? sf::Color::Yellow : sf::Color::White); this->window->draw(this->pause_resume_text); this->window->draw(this->pause_exit_text); } } this->window->display(); }