rotations and teew some stats
This commit is contained in:
parent
ecd27fd8d9
commit
09f04ce82f
30
game.cpp
30
game.cpp
@ -3,15 +3,15 @@
|
|||||||
#define DELAY 30000
|
#define DELAY 30000
|
||||||
|
|
||||||
Game::Game()
|
Game::Game()
|
||||||
: x(0), y(0), max_x(800), max_y(600), direction(0), window(sf::VideoMode(max_x, max_y), "SFML Window"),
|
: x(0), y(0), max_x(1000), max_y(800), 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
|
racecar(300.0f, 30.0f, 1.7f) { // maxSpeed, acceleration, and steerSpeed values
|
||||||
if (!font.loadFromFile("cascaydia.otf")) {
|
if (!font.loadFromFile("cascaydia.otf")) {
|
||||||
std::cerr << "Failed to load font" << std::endl;
|
std::cerr << "Failed to load font" << std::endl;
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
sf::Text text("o", font, 24); // This is just for the track, you can modify it as needed
|
// Create a text object for displaying speed
|
||||||
text.setFillColor(sf::Color::White);
|
speedText.setFont(font);
|
||||||
|
|
||||||
racecar.setPosition(max_x / 2, max_y / 2); // Set initial position for the racecar
|
racecar.setPosition(max_x / 2, max_y / 2); // Set initial position for the racecar
|
||||||
}
|
}
|
||||||
@ -54,8 +54,28 @@ void Game::run() {
|
|||||||
|
|
||||||
racecar.update(dt);
|
racecar.update(dt);
|
||||||
|
|
||||||
|
speedText.setString(std::to_string(static_cast<int>(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.clear();
|
||||||
window.draw(racecar.getDrawable()); // Draw the racecar
|
window.draw(racecar.getDrawable()); // Draw the racecar
|
||||||
|
window.draw(speedText);
|
||||||
window.display();
|
window.display();
|
||||||
|
|
||||||
sf::sleep(sf::microseconds(DELAY));
|
sf::sleep(sf::microseconds(DELAY));
|
||||||
|
|||||||
1
game.h
1
game.h
@ -18,6 +18,7 @@ private:
|
|||||||
int direction;
|
int direction;
|
||||||
sf::RenderWindow window;
|
sf::RenderWindow window;
|
||||||
sf::Font font;
|
sf::Font font;
|
||||||
|
sf::Text speedText;
|
||||||
Racecar racecar;
|
Racecar racecar;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -7,10 +7,11 @@ Racecar::Racecar(float maxSpeed, float acceleration, float steerSpeed)
|
|||||||
exit(1);
|
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.setFont(font);
|
||||||
carText.setCharacterSize(24);
|
carText.setCharacterSize(12);
|
||||||
carText.setFillColor(sf::Color::White);
|
carText.setFillColor(sf::Color::White);
|
||||||
|
|
||||||
speed = 0;
|
speed = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,6 +35,7 @@ void Racecar::decelerate(float dt) {
|
|||||||
|
|
||||||
void Racecar::steer(float dt, float direction) {
|
void Racecar::steer(float dt, float direction) {
|
||||||
steeringAngle += steerSpeed * direction * dt;
|
steeringAngle += steerSpeed * direction * dt;
|
||||||
|
carText.setRotation(steeringAngle * 180 / M_PI);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Racecar::brake(float dt) {
|
void Racecar::brake(float dt) {
|
||||||
|
|||||||
BIN
terminal_racer
BIN
terminal_racer
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user