lap times
This commit is contained in:
parent
e397e49b58
commit
11e7558feb
41
game.cpp
41
game.cpp
@ -27,7 +27,7 @@ Game::Game()
|
||||
racecar.setRotation(180);
|
||||
|
||||
// Initialize the times and quadrants data structure
|
||||
timesAndQuadrants.push_back(std::make_pair("Quadrant 1", "00:00:00.000"));
|
||||
timesAndQuadrants.push_back(std::make_pair(1, "00:00:00.000"));
|
||||
}
|
||||
|
||||
void Game::run() {
|
||||
@ -59,11 +59,13 @@ void Game::run() {
|
||||
timerText.setString(timerString);
|
||||
|
||||
// Detect when the racecar changes quadrants and save the time
|
||||
std::string currentQuadrant = determineQuadrant(racecar.getX(), racecar.getY());
|
||||
int currentQuadrant = determineQuadrant(racecar.getX(), racecar.getY());
|
||||
if (currentQuadrant != timesAndQuadrants.back().first) {
|
||||
timesAndQuadrants.push_back(std::make_pair(currentQuadrant, timerString));
|
||||
}
|
||||
|
||||
determineLap(timerString);
|
||||
|
||||
sf::Event event;
|
||||
while (window.pollEvent(event)) {
|
||||
if (event.type == sf::Event::Closed) {
|
||||
@ -163,20 +165,37 @@ bool Game::isTrackCollision(float x, float y) {
|
||||
return false; // No collision with track or out of bounds
|
||||
}
|
||||
|
||||
std::string Game::determineQuadrant(float x, float y) {
|
||||
for (int i = 0; i < timesAndQuadrants.size(); i++) {
|
||||
if (timesAndQuadrants[i] != requiredOrder[i])
|
||||
|
||||
void Game::determineLap(std::string timerString) {
|
||||
lapFinished = true;
|
||||
int requiredOrder[] = {1,2,1,2,3,4,1};
|
||||
if(timesAndQuadrants.size() >= 7) {
|
||||
for (int i = 0; i < 7; i++) {
|
||||
if (std::get<0>(timesAndQuadrants[i]) != requiredOrder[i])
|
||||
lapFinished = false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
lapFinished = false;
|
||||
}
|
||||
|
||||
if (lapFinished) {
|
||||
lapTimes.push_back(timerString);
|
||||
timesAndQuadrants.clear();
|
||||
timesAndQuadrants.push_back(std::make_pair(1, timerString));
|
||||
}
|
||||
}
|
||||
|
||||
int Game::determineQuadrant(float x, float y) {
|
||||
if (y <= 460 && x <= 1150)
|
||||
return "Quadrant 1";
|
||||
return 1;
|
||||
if (y > 460 && x <= 1150)
|
||||
return "Quadrant 2";
|
||||
return 2;
|
||||
if (y > 460 && x > 1150)
|
||||
return "Quadrant 3";
|
||||
return 3;
|
||||
if (y <= 460 && x > 1150)
|
||||
return "Quadrant 4";
|
||||
return 4;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Game::displaySavedTimes() {
|
||||
@ -194,7 +213,7 @@ void Game::displaySavedTimes() {
|
||||
textY += 30; // Adjust the spacing as needed
|
||||
}
|
||||
for (const auto& timeAndQuadrant : timesAndQuadrants) {
|
||||
std::string timeString = timeAndQuadrant.first + ": " + timeAndQuadrant.second;
|
||||
std::string timeString = "Quadrant " + std::to_string(timeAndQuadrant.first) + ": " + timeAndQuadrant.second;
|
||||
sf::Text text;
|
||||
text.setFont(font);
|
||||
text.setCharacterSize(18);
|
||||
|
||||
7
game.h
7
game.h
@ -11,8 +11,9 @@ public:
|
||||
Game();
|
||||
void run();
|
||||
bool isTrackCollision(float x, float y);
|
||||
std::string determineQuadrant(float x, float y);
|
||||
int determineQuadrant(float x, float y);
|
||||
void displaySavedTimes();
|
||||
void determineLap(std::string timerString);
|
||||
|
||||
private:
|
||||
int max_x;
|
||||
@ -26,8 +27,8 @@ private:
|
||||
sf::Clock gameTimeClock;
|
||||
sf::View view;
|
||||
Racecar racecar;
|
||||
std::vector<std::pair<std::string, std::string>> timesAndQuadrants; // Store times and quadrants
|
||||
bool lapStarted; // Indicates if a lap has started
|
||||
std::vector<std::pair<int, std::string>> timesAndQuadrants; // Store times and quadrants
|
||||
bool lapFinished;
|
||||
std::vector<std::string> lapTimes; // Store lap times
|
||||
std::string asciiMap = " /------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\\ \n"
|
||||
" / \\ \n"
|
||||
|
||||
BIN
terminal_racer
BIN
terminal_racer
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user