save
This commit is contained in:
parent
111446f8ac
commit
e397e49b58
129
game.cpp
129
game.cpp
@ -17,23 +17,53 @@ Game::Game()
|
||||
mapText.setFont(font);
|
||||
mapText.setCharacterSize(24);
|
||||
mapText.setFillColor(sf::Color::White);
|
||||
mapText.setPosition(10, 10);
|
||||
mapText.setPosition(0, 0);
|
||||
|
||||
timerText.setFont(font);
|
||||
timerText.setCharacterSize(24);
|
||||
timerText.setFillColor(sf::Color::White);
|
||||
|
||||
racecar.setPosition(1000, 80); // Set initial position for the racecar
|
||||
racecar.setRotation(180);
|
||||
|
||||
// Initialize the times and quadrants data structure
|
||||
timesAndQuadrants.push_back(std::make_pair("Quadrant 1", "00:00:00.000"));
|
||||
}
|
||||
|
||||
void Game::run() {
|
||||
sf::Clock clock;
|
||||
|
||||
// Create a view that will follow the racecar
|
||||
sf::View view(sf::FloatRect(0, 0, max_x, max_y));
|
||||
view = sf::View(sf::FloatRect(0, 0, max_x, max_y));
|
||||
view.setCenter(racecar.getX(), racecar.getY()); // Center the view on the racecar
|
||||
window.setView(view);
|
||||
|
||||
|
||||
while (window.isOpen()) {
|
||||
float dt = clock.restart().asSeconds();
|
||||
|
||||
// Update game time
|
||||
sf::Time gameTime = gameTimeClock.getElapsedTime();
|
||||
int milis = static_cast<int>(gameTime.asMilliseconds());
|
||||
int seconds = static_cast<int>(gameTime.asSeconds());
|
||||
int minutes = seconds / 60;
|
||||
seconds %= 60;
|
||||
milis %= 1000;
|
||||
|
||||
// Use std::setw and std::setfill to format numbers as two digits
|
||||
std::ostringstream timerStream;
|
||||
timerStream << std::setw(2) << std::setfill('0') << minutes << ":"
|
||||
<< std::setw(2) << std::setfill('0') << seconds << ":"
|
||||
<< std::setw(2) << std::setfill('0') << milis;
|
||||
|
||||
std::string timerString = timerStream.str();
|
||||
timerText.setString(timerString);
|
||||
|
||||
// Detect when the racecar changes quadrants and save the time
|
||||
std::string currentQuadrant = determineQuadrant(racecar.getX(), racecar.getY());
|
||||
if (currentQuadrant != timesAndQuadrants.back().first) {
|
||||
timesAndQuadrants.push_back(std::make_pair(currentQuadrant, timerString));
|
||||
}
|
||||
|
||||
sf::Event event;
|
||||
while (window.pollEvent(event)) {
|
||||
if (event.type == sf::Event::Closed) {
|
||||
@ -43,31 +73,36 @@ void Game::run() {
|
||||
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
|
||||
if (racecar.getSpeed() < 0) {
|
||||
racecar.brake(dt); // Apply brakes
|
||||
racecar.brake(dt);
|
||||
} else {
|
||||
if(!isTrackCollision(racecar.getX(), racecar.getY())) {
|
||||
racecar.accelerate(dt); // accelerate the car
|
||||
}
|
||||
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);
|
||||
} else {
|
||||
racecar.stop();
|
||||
}
|
||||
}
|
||||
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
|
||||
if (racecar.getSpeed() > 0) {
|
||||
racecar.brake(dt); // Apply brakes
|
||||
racecar.brake(dt);
|
||||
} else {
|
||||
if(!isTrackCollision(racecar.getX(), racecar.getY())) {
|
||||
racecar.reverse(dt); // Reverse the car
|
||||
}
|
||||
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.reverse(dt);
|
||||
} else {
|
||||
racecar.stop();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
racecar.decelerate(dt); // No key pressed, decelerate as usual
|
||||
racecar.decelerate(dt);
|
||||
}
|
||||
|
||||
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
|
||||
racecar.steer(dt, -1.0f); // Steer left
|
||||
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
|
||||
@ -91,6 +126,7 @@ void Game::run() {
|
||||
speedText.setCharacterSize(30);
|
||||
speedText.setFillColor(sf::Color::Red);
|
||||
}
|
||||
timerText.setPosition(racecar.getX() - 80, racecar.getY() - 350);
|
||||
|
||||
// Update the view to follow the racecar
|
||||
view.setCenter(racecar.getX(), racecar.getY());
|
||||
@ -101,6 +137,8 @@ void Game::run() {
|
||||
window.draw(mapText);
|
||||
window.draw(racecar.getDrawable());
|
||||
window.draw(speedText);
|
||||
window.draw(timerText);
|
||||
displaySavedTimes();
|
||||
window.display();
|
||||
|
||||
sf::sleep(sf::microseconds(DELAY));
|
||||
@ -108,7 +146,62 @@ void Game::run() {
|
||||
}
|
||||
|
||||
bool Game::isTrackCollision(float x, float y) {
|
||||
// Check if the racecar's position collides with the track
|
||||
char trackChar = asciiMap[x];
|
||||
return (trackChar == '/' || trackChar == '\\' || trackChar == '|' || trackChar == '_' || trackChar == '-');
|
||||
if (x <= 0) {
|
||||
// return true;
|
||||
} else if (y <= 0) {
|
||||
// return true;
|
||||
} else {
|
||||
// int currX = (x / 13.85) - 1;
|
||||
// int currY = (y / 27.88) - 1;
|
||||
// char currentChar = asciiMap[currY * 194 + currX];
|
||||
// //std::cout << x << ", " << y << std::endl;
|
||||
// if (currentChar == '\\' || currentChar == '|' || currentChar == '/' || currentChar == '-' || currentChar == '_') {
|
||||
// //return true;
|
||||
// }
|
||||
}
|
||||
|
||||
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])
|
||||
|
||||
}
|
||||
|
||||
if (y <= 460 && x <= 1150)
|
||||
return "Quadrant 1";
|
||||
if (y > 460 && x <= 1150)
|
||||
return "Quadrant 2";
|
||||
if (y > 460 && x > 1150)
|
||||
return "Quadrant 3";
|
||||
if (y <= 460 && x > 1150)
|
||||
return "Quadrant 4";
|
||||
}
|
||||
|
||||
void Game::displaySavedTimes() {
|
||||
// Display the saved times on the left side of the screen
|
||||
int textY = 10; // Adjust the vertical position as needed
|
||||
for (const auto& lapTime : lapTimes) {
|
||||
std::string timeString = "Lap Time: " + lapTime;
|
||||
sf::Text text;
|
||||
text.setFont(font);
|
||||
text.setCharacterSize(18);
|
||||
text.setFillColor(sf::Color::White);
|
||||
text.setPosition(racecar.getX() - 475, (racecar.getY() - 300) + textY);
|
||||
text.setString(timeString);
|
||||
window.draw(text);
|
||||
textY += 30; // Adjust the spacing as needed
|
||||
}
|
||||
for (const auto& timeAndQuadrant : timesAndQuadrants) {
|
||||
std::string timeString = timeAndQuadrant.first + ": " + timeAndQuadrant.second;
|
||||
sf::Text text;
|
||||
text.setFont(font);
|
||||
text.setCharacterSize(18);
|
||||
text.setFillColor(sf::Color::White);
|
||||
text.setPosition(racecar.getX() - 475, (racecar.getY() - 300) + textY);
|
||||
text.setString(timeString);
|
||||
window.draw(text);
|
||||
textY += 30; // Adjust the spacing as needed
|
||||
}
|
||||
}
|
||||
|
||||
11
game.h
11
game.h
@ -3,6 +3,7 @@
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include "racecar.h"
|
||||
|
||||
class Game {
|
||||
@ -10,6 +11,8 @@ public:
|
||||
Game();
|
||||
void run();
|
||||
bool isTrackCollision(float x, float y);
|
||||
std::string determineQuadrant(float x, float y);
|
||||
void displaySavedTimes();
|
||||
|
||||
private:
|
||||
int max_x;
|
||||
@ -19,8 +22,14 @@ private:
|
||||
sf::Font font;
|
||||
sf::Text speedText;
|
||||
sf::Text mapText;
|
||||
sf::Text timerText;
|
||||
sf::Clock gameTimeClock;
|
||||
sf::View view;
|
||||
Racecar racecar;
|
||||
std::string asciiMap = " /------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\\ \n"
|
||||
std::vector<std::pair<std::string, std::string>> timesAndQuadrants; // Store times and quadrants
|
||||
bool lapStarted; // Indicates if a lap has started
|
||||
std::vector<std::string> lapTimes; // Store lap times
|
||||
std::string asciiMap = " /------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\\ \n"
|
||||
" / \\ \n"
|
||||
" / | \n"
|
||||
" / | \n"
|
||||
|
||||
BIN
micrenc.ttf
Normal file
BIN
micrenc.ttf
Normal file
Binary file not shown.
@ -74,6 +74,10 @@ void Racecar::setRotation(float value) {
|
||||
carText.setRotation(value);
|
||||
}
|
||||
|
||||
float Racecar::getRotation() {
|
||||
return steeringAngle;
|
||||
}
|
||||
|
||||
float Racecar::getSpeed() const {
|
||||
return speed;
|
||||
}
|
||||
|
||||
@ -18,6 +18,7 @@ public:
|
||||
sf::Text getDrawable();
|
||||
void setPosition(float x, float y);
|
||||
void setRotation(float value);
|
||||
float getRotation();
|
||||
float getSpeed() const; // Get the current speed of the car
|
||||
float getX() const;
|
||||
float getY() const;
|
||||
|
||||
BIN
terminal_racer
BIN
terminal_racer
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user