nitro
This commit is contained in:
parent
19d0b0e8e0
commit
52342fc91d
48
game.cpp
48
game.cpp
@ -23,6 +23,7 @@ Game::Game()
|
||||
|
||||
// Create a text object for displaying speed
|
||||
speedText.setFont(font);
|
||||
nitroText.setFont(font);
|
||||
|
||||
// Create a map text object and set its properties
|
||||
mapText.setFont(font);
|
||||
@ -93,7 +94,42 @@ void Game::run() {
|
||||
}
|
||||
|
||||
if(lapPoints.size() < 3) {
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift)) {
|
||||
if(nitro > 0) {
|
||||
nitro--;
|
||||
float newX = racecar.getX() + ((racecar.getSpeed() + 1.1) * dt * cos(racecar.getRotation()));
|
||||
float newY = racecar.getY() + ((racecar.getSpeed() + 1.1) * dt * sin(racecar.getRotation()));
|
||||
//std::cout << "New: " << newX << "," << newY << " Old: " << racecar.getX() << "," << racecar.getY() << std::endl;
|
||||
if (!isTrackCollision(newX, newY)) {
|
||||
racecar.boost(dt);
|
||||
//std::cout << nitro << std::endl;
|
||||
mapText.setOutlineThickness(0);
|
||||
} else {
|
||||
//racecar.stop();
|
||||
racecar.boost(dt);
|
||||
modifier += 0.01;
|
||||
mapText.setOutlineThickness(6);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (racecar.getSpeed() < 0) {
|
||||
racecar.brake(dt);
|
||||
} else {
|
||||
float newX = racecar.getX() + ((racecar.getSpeed() + 1.1) * dt * cos(racecar.getRotation()));
|
||||
float newY = racecar.getY() + ((racecar.getSpeed() + 1.1) * dt * sin(racecar.getRotation()));
|
||||
//std::cout << "New: " << newX << "," << newY << " Old: " << racecar.getX() << "," << racecar.getY() << std::endl;
|
||||
|
||||
if (!isTrackCollision(newX, newY)) {
|
||||
racecar.accelerate(dt);
|
||||
mapText.setOutlineThickness(0);
|
||||
} else {
|
||||
//racecar.stop();
|
||||
modifier += 0.01;
|
||||
mapText.setOutlineThickness(6);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
|
||||
if (racecar.getSpeed() < 0) {
|
||||
racecar.brake(dt);
|
||||
} else {
|
||||
@ -139,6 +175,11 @@ void Game::run() {
|
||||
|
||||
racecar.update(dt);
|
||||
|
||||
nitroText.setString("Nitro: " + std::to_string(nitro));
|
||||
nitroText.setCharacterSize(24);
|
||||
nitroText.setFillColor(sf::Color::White);
|
||||
nitroText.setPosition(racecar.getX() - 475, racecar.getY() + 300);
|
||||
|
||||
speedText.setString(std::to_string(static_cast<int>(racecar.getSpeed())) + " km/h");
|
||||
speedText.setCharacterSize(24);
|
||||
speedText.setFillColor(sf::Color::Green);
|
||||
@ -166,6 +207,7 @@ void Game::run() {
|
||||
window.draw(mapText);
|
||||
window.draw(racecar.getDrawable());
|
||||
window.draw(speedText);
|
||||
window.draw(nitroText);
|
||||
|
||||
displaySavedTimes();
|
||||
|
||||
@ -206,11 +248,11 @@ bool Game::isTrackCollision(float x, float y) {
|
||||
int currX = (x / 13.85) - 1;
|
||||
int currY = (y / 27.88) - 1;
|
||||
char currentChar;
|
||||
if (currY * 194 + currX < 6478)
|
||||
if (currY * 194 + currX < 6378)
|
||||
currentChar = asciiMap[currY * 194 + currX];
|
||||
else
|
||||
return true;
|
||||
//std::cout << x << ", " << y << std::endl;
|
||||
//td::cout << currY * 194 + currX << std::endl;
|
||||
if (currentChar == '\\' || currentChar == '|' || currentChar == '/' || currentChar == '-' || currentChar == '_') {
|
||||
return true;
|
||||
}
|
||||
|
||||
2
game.h
2
game.h
@ -28,6 +28,7 @@ private:
|
||||
sf::RenderWindow window;
|
||||
sf::Font font;
|
||||
sf::Text speedText;
|
||||
sf::Text nitroText;
|
||||
sf::Text mapText;
|
||||
sf::Text timerText;
|
||||
sf::Clock gameTimeClock;
|
||||
@ -41,6 +42,7 @@ private:
|
||||
std::vector<unsigned int> lapPoints; // Store lap times
|
||||
float modifier = 1.0f;
|
||||
sf::Music backgroundMusic;
|
||||
int nitro = 300;
|
||||
std::string asciiMap = " /------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\\ \n"
|
||||
" / \\ \n"
|
||||
" / | \n"
|
||||
|
||||
@ -23,6 +23,9 @@ void Racecar::accelerate(float dt) {
|
||||
if (speed < maxSpeed) {
|
||||
speed += acceleration * dt;
|
||||
}
|
||||
else {
|
||||
decelerate(dt);
|
||||
}
|
||||
}
|
||||
|
||||
void Racecar::decelerate(float dt) {
|
||||
@ -89,3 +92,9 @@ float Racecar::getX() const {
|
||||
float Racecar::getY() const {
|
||||
return carText.getPosition().y;
|
||||
}
|
||||
|
||||
void Racecar::boost(float dt) {
|
||||
if (speed < maxSpeed * 2) {
|
||||
speed += (acceleration * dt) * 5;
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@ public:
|
||||
Racecar(float maxSpeed, float acceleration, float steerSpeed);
|
||||
void update(float dt);
|
||||
void accelerate(float dt);
|
||||
void boost(float dt);
|
||||
void decelerate(float dt);
|
||||
void brake(float dt); // Brake the car
|
||||
void stop();
|
||||
|
||||
BIN
terminal_racer
BIN
terminal_racer
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user