diff --git a/game.cpp b/game.cpp index deab20a..21c4e0a 100644 --- a/game.cpp +++ b/game.cpp @@ -3,15 +3,15 @@ #define DELAY 30000 Game::Game() - : x(0), y(0), max_x(800), max_y(600), direction(0), window(sf::VideoMode(max_x, max_y), "SFML Window"), - racecar(200.0f, 20.0f, 1.0f) { // Adjust maxSpeed, acceleration, and steerSpeed values as needed + : x(0), y(0), max_x(1000), max_y(800), direction(0), window(sf::VideoMode(max_x, max_y), "SFML Window"), + racecar(300.0f, 30.0f, 1.7f) { // maxSpeed, acceleration, and steerSpeed values if (!font.loadFromFile("cascaydia.otf")) { std::cerr << "Failed to load font" << std::endl; exit(1); } - - sf::Text text("o", font, 24); // This is just for the track, you can modify it as needed - text.setFillColor(sf::Color::White); + + // Create a text object for displaying speed + speedText.setFont(font); racecar.setPosition(max_x / 2, max_y / 2); // Set initial position for the racecar } @@ -54,8 +54,28 @@ void Game::run() { racecar.update(dt); + speedText.setString(std::to_string(static_cast(racecar.getSpeed())) + " km/h"); + speedText.setCharacterSize(24); + speedText.setFillColor(sf::Color::Green); + speedText.setPosition(10, 770); + if(racecar.getSpeed() < 0){ + speedText.setFillColor(sf::Color::Blue); + } + if(racecar.getSpeed() >= 150){ + speedText.setPosition(10, 767); + speedText.setCharacterSize(27); + speedText.setFillColor(sf::Color::Yellow); + } + if(racecar.getSpeed() >= 260){ + speedText.setPosition(10, 764); + speedText.setCharacterSize(30); + speedText.setFillColor(sf::Color::Red); + } + + window.clear(); window.draw(racecar.getDrawable()); // Draw the racecar + window.draw(speedText); window.display(); sf::sleep(sf::microseconds(DELAY)); diff --git a/game.h b/game.h index 97b09c9..186fab8 100644 --- a/game.h +++ b/game.h @@ -18,6 +18,7 @@ private: int direction; sf::RenderWindow window; sf::Font font; + sf::Text speedText; Racecar racecar; }; diff --git a/racecar.cpp b/racecar.cpp index b77b3ce..31259d5 100644 --- a/racecar.cpp +++ b/racecar.cpp @@ -7,10 +7,11 @@ Racecar::Racecar(float maxSpeed, float acceleration, float steerSpeed) exit(1); } - carText.setString("0"); // Replace "o" with a racecar emoji or any desired character + carText.setString("|:=:>"); // Replace "o" with a racecar emoji or any desired character carText.setFont(font); - carText.setCharacterSize(24); + carText.setCharacterSize(12); carText.setFillColor(sf::Color::White); + speed = 0; } @@ -34,6 +35,7 @@ void Racecar::decelerate(float dt) { void Racecar::steer(float dt, float direction) { steeringAngle += steerSpeed * direction * dt; + carText.setRotation(steeringAngle * 180 / M_PI); } void Racecar::brake(float dt) { diff --git a/terminal_racer b/terminal_racer index d156ace..05a84f9 100755 Binary files a/terminal_racer and b/terminal_racer differ