Fork Copy #include #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 679) { if(i == 0) { spriteRock[i].setPosition((rand() % 280), (rand() % 1) - 1000); } else { spriteRock[i].setPosition((rand() % 810) + 780, (rand() % 1) - 500); } } } for(int i=0; i 800) { if(i == 0) { spriteTree[i].setPosition((rand() % 280), (rand() % 1) - 200); } else { spriteTree[i].setPosition((rand() % 770) + 750, (rand() % 1) - 200); } } } // move the npc cars if(player.getCarState() == Player::CarState::ACCELERATE) { spriteNpc.setPosition(spriteNpc.getPosition().x, (spriteNpc.getPosition().y + (gameSpeed * dt.asSeconds()))); // move the odometer if((dometerNeedle.getRotation() >= 0 && dometerNeedle.getRotation() <= 120) || (dometerNeedle.getRotation() <= 360 && dometerNeedle.getRotation() >= 190)) { dometerNeedle.setRotation(dometerNeedle.getRotation() + (10 * dt.asSeconds())); } }else if(player.getCarState() == Player::CarState::DECELERATE) { if(spriteNpc.getPosition().y > -1000) { spriteNpc.setPosition(spriteNpc.getPosition().x, (spriteNpc.getPosition().y - (800 * dt.asSeconds()))); } // move the odometer the opposite way if((dometerNeedle.getRotation() >= 0 && dometerNeedle.getRotation() <= 121) || (dometerNeedle.getRotation() <= 360 && dometerNeedle.getRotation() >= 225)) { dometerNeedle.setRotation(dometerNeedle.getRotation() - (47 * dt.asSeconds())); } } // if the we pass the npc, move the npc back to the front of the road if(spriteNpc.getPosition().y > 800) { spriteNpc.setPosition((rand() % 345) + 344, (rand() % 1) - 2000); // set a new texture for the sprite int textureChoice = rand() % 8; // Sửa % 8 spriteNpc.setTexture(textureCars[textureChoice]); // increment player score player.setScore(player.getScore() + 1); } // here we are going to get the player boundaries and modify them // a little to get a little more realisic collision sf::FloatRect playerBounds = player.getSprite().getGlobalBounds(); sf::FloatRect playerModBounds = sf::FloatRect(playerBounds.left+10, playerBounds.top+10, playerBounds.width-10, playerBounds.height-10); // check for collision if(spriteNpc.getGlobalBounds().intersects(playerModBounds)) { crashSound.play(); if(carRevSound.getStatus() == sf::SoundSource::Status::Playing) { carRevSound.stop(); } gameState = GameState::OVER; message.setString("Game Over!"); message.setPosition(sf::VideoMode::getDesktopMode().width - (sf::VideoMode::getDesktopMode().width / 1.4f), sf::VideoMode::getDesktopMode().height - (sf::VideoMode::getDesktopMode().height / 1.45f)); } } /***************************** * Draw the scene ******************************/ // clear what is currently on the screen window.clear(); window.draw(spriteRoad); for(int i=0; i<5; i++) { window.draw(spriteRoadStripe[i]); } window.draw(player.getSprite()); for(int i=0; i