Fork Copy #include #include #include #include #include #include "Player.h" int main() { sf::VideoMode vm(1086, 679); sf::RenderWindow window(vm, "Highway Racer", sf::Style::Titlebar); // Load textures here sf::Texture textureRoad; if (!textureRoad.loadFromFile("assets/imgs/background.png")) { std::cerr << "Error loading background.png" << std::endl; return 1; } sf::Texture textureRoadStripe; if (!textureRoadStripe.loadFromFile("assets/imgs/roadStripe.png")) { std::cerr << "Error loading roadStripe.png" << std::endl; return 1; } sf::Texture textureRock; if (!textureRock.loadFromFile("assets/imgs/rock.png")) { std::cerr << "Error loading rock.png" << std::endl; return 1; } sf::Texture textureCars[8]; // Chỉ 8 để khớp số tải if (!textureCars[0].loadFromFile("assets/imgs/carOne.png")) { std::cerr << "Error loading carOne.png" << std::endl; return 1; } if (!textureCars[1].loadFromFile("assets/imgs/carTwo.png")) { std::cerr << "Error loading carTwo.png" << std::endl; return 1; } if (!textureCars[2].loadFromFile("assets/imgs/carThree.png")) { std::cerr << "Error loading carThree.png" << std::endl; return 1; } if (!textureCars[3].loadFromFile("assets/imgs/carFour.png")) { std::cerr << "Error loading carFour.png" << std::endl; return 1; } if (!textureCars[4].loadFromFile("assets/imgs/carFive.png")) { std::cerr << "Error loading carFive.png" << std::endl; return 1; } if (!textureCars[5].loadFromFile("assets/imgs/carSix.png")) { std::cerr << "Error loading carSix.png" << std::endl; return 1; } if (!textureCars[6].loadFromFile("assets/imgs/carSeven.png")) { std::cerr << "Error loading carSeven.png" << std::endl; return 1; } if (!textureCars[7].loadFromFile("assets/imgs/carEight.png")) { std::cerr << "Error loading carEight.png" << std::endl; return 1; } sf::Texture textureTree; if (!textureTree.loadFromFile("assets/imgs/tree.png")) { std::cerr << "Error loading tree.png" << std::endl; return 1; } sf::Texture textureSpeedometer; if (!textureSpeedometer.loadFromFile("assets/imgs/speedometer.png")) { std::cerr << "Error loading speedometer.png" << std::endl; return 1; } // set the road up sf::Sprite spriteRoad; spriteRoad.setTexture(textureRoad); sf::Sprite spriteRoadStripe[5]; for(int i=0; i<5; i++) { spriteRoadStripe[i].setTexture(textureRoadStripe); spriteRoadStripe[i].setPosition(525, i*150); } // environment details //rocks const int NUMBEROFROCKS = 2; sf::Sprite spriteRock[NUMBEROFROCKS]; srand(static_cast(time(nullptr))); for(int i=0; i 50) { carRevSound.setPlayingOffset(sf::seconds(10)); }else { carRevSound.setPlayingOffset(sf::seconds(0)); } carRevSound.play(); } // check where we are at in the car rev sound file and // loop it when it gets to a certain time if(static_cast(carRevSound.getPlayingOffset().asSeconds()) == 30) { carRevSound.setPlayingOffset(sf::seconds(20)); } if(gameSpeed < MAXGAMESPEED) { gameSpeed += 0.55f; } }else if(!(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) && gameState == GameState::RUNNING) { if(player.getCarState() == Player::CarState::ACCELERATE) { player.setCarState(Player::CarState::DECELERATE); if(carRevSound.getStatus() == sf::SoundSource::Status::Playing) { carRevSound.stop(); } carRevReverseSound.play(); } if(gameSpeed > 0.0f) { gameSpeed -= 0.38f; } } if(gameSpeed <= 0.0f) { player.setCarState(Player::CarState::STOPPED); if(carRevReverseSound.getStatus() == sf::SoundSource::Status::Playing) { carRevReverseSound.stop(); } if(carIdleSound.getStatus() == sf::SoundSource::Status::Stopped) { carIdleSound.play(); } } // control player car if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && gameState == GameState::RUNNING) { // make sure that the car can't leave the road if(player.getPlayerPosition().x > 370) { player.setPlayerPosition(player.getPlayerPosition().x - 1.2f, player.getPlayerPosition().y); } }else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && gameState == GameState::RUNNING) { if(player.getPlayerPosition().x < 633) { player.setPlayerPosition(player.getPlayerPosition().x + 1.2f, player.getPlayerPosition().y); } } /***************************** * Update the scene ******************************/ sf::Time dt = clock.restart(); std::stringstream ss; ss << "Score: " << player.getScore(); score.setString(ss.str()); if(gameState == GameState::RUNNING) { for(int i=0; i<5; i++) { // road stripes spriteRoadStripe[i].setPosition(spriteRoadStripe[i].getPosition().x, (spriteRoadStripe[i].getPosition().y + (gameSpeed * dt.asSeconds()))); /* when the stripe gets off screen, put it back at the top to give the illusion of * endless road */ if(spriteRoadStripe[i].getPosition().y > 679) { spriteRoadStripe[i].setPosition(spriteRoadStripe[i].getPosition().x, -70); } } // do the same for the environment detail but move them at a slightly slower speed for(int i=0; i