follow the player

This commit is contained in:
thatscringebro 2023-10-10 14:53:20 -04:00
parent abacd48eee
commit defc393748
6 changed files with 94 additions and 49 deletions

View File

@ -13,12 +13,23 @@ Game::Game()
// Create a text object for displaying speed
speedText.setFont(font);
// Create a map text object and set its properties
mapText.setFont(font);
mapText.setCharacterSize(24);
mapText.setFillColor(sf::Color::White);
mapText.setPosition(10, 10);
racecar.setPosition(max_x / 2, max_y / 2); // Set initial position for the racecar
}
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.setCenter(racecar.getX(), racecar.getY()); // Center the view on the racecar
window.setView(view);
while (window.isOpen()) {
float dt = clock.restart().asSeconds();
@ -73,8 +84,14 @@ void Game::run() {
}
// Update the view to follow the racecar
view.setCenter(racecar.getX(), racecar.getY());
window.setView(view);
window.clear();
window.draw(racecar.getDrawable()); // Draw the racecar
mapText.setString(asciiMap);
window.draw(mapText);
window.draw(racecar.getDrawable());
window.draw(speedText);
window.display();

34
game.h
View File

@ -17,7 +17,41 @@ private:
sf::RenderWindow window;
sf::Font font;
sf::Text speedText;
sf::Text mapText;
Racecar racecar;
std::string asciiMap = " /------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\\ \n"
" / \\ \n"
" / | \n"
" / | \n"
" / _____________________________________________________________________________________________________________________________________________________________ | \n"
" / / \\ | \n"
" / / | | \n"
" / | /------------------------------------------------------\\ / | \n"
"/ | / \\ _____ __________________ .___.___ / / \n"
"| || | / _ \\ / _____/\\_ ___ \\| | | / / \n"
"| || | / /_\\ \\ \\_____ \\ / \\ \\/| | | / / \n"
"| || __________________________ | / | \\/ \\ \\___| | | / / \n"
"| || / \\ | \\____|__ /_______ / \\______ /___|___| / / \n"
"| || / | | \\/ \\/ \\/ / / \n"
"| || | | | __________ _____ _________ _____________________ / / \n"
"| || | / / \\______ \\ / _ \\ \\_ ___ \\_ _____/\\______ \\ / / \n"
"| || | / / | _/ / /_\\ \\/ \\ \\/ | __)_ | _/ / / \n"
"| || | / / | | \\/ | \\ \\____| \\ | | \\ / / \n"
"| || | / / |____|_ /\\____|__ /\\______ /_______ / |____|_ / / / \n"
"| || | / / \\/ \\/ \\/ \\/ \\/ / / \n"
"| || | / / / / \n"
"| || | / / / / \n"
"| || | / / / / \n"
"| || | | | / / \n"
"| || | | | / / \n"
"| || | \\ \\ / / \n"
"| || | \\ \\ / / \n"
"\\ \\/ / \\ \\____________________________________________________________________________________/ / \n"
" \\ / \\ / \n"
" \\ / \\ / \n"
" \\ / \\ / \n"
" \\ / \\ / \n"
" \\____________________/ \\_________________________________________________________________________________________________________/ ";
};
#endif // GAME_H

81
map
View File

@ -1,48 +1,33 @@
/¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯\
/ \
/ |
/ |
/ _____________________________________________________________________________________________________________________________________________________________ |
/ / \ |
/ / | |
/ | /¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯\ / |
/ | / \ _____ __________________ .___.___ / /
| || | / _ \ / _____/\_ ___ \| | | / /
| || | / /_\ \ \_____ \ / \ \/| | | / /
| || __________________________ | / | \/ \\ \___| | | / /
| || / \ | \____|__ /_______ / \______ /___|___| / /
| || / | | \/ \/ \/ / /
| || | | | __________ _____ _________ _____________________ / /
| || | / / \______ \ / _ \ \_ ___ \\_ _____/\______ \ / /
| || | / / | _/ / /_\ \/ \ \/ | __)_ | _/ / /
| || | / / | | \/ | \ \____| \ | | \ / /
| || | / / |____|_ /\____|__ /\______ /_______ / |____|_ / / /
| || | / / \/ \/ \/ \/ \/ / /
| || | / / / /
| || | / / / /
| || | / / / /
| || | | | / /
| || | | | / /
| || | \ \ / /
| || | \ \ / /
\ \/ / \ \____________________________________________________________________________________/ /
\ / \ /
\ / \ /
\ / \ /
\ / \ /
\____________________/ \_________________________________________________________________________________________________________/
/¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯\
/ \
/ |
/ |
/ _____________________________________________________________________________________________________________________________________________________________ |
/ / \ |
/ / | |
/ | /¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯\ / |
/ | / \ _____ __________________ .___.___ / /
| || | / _ \ / _____/\_ ___ \| | | / /
| || | / /_\ \ \_____ \ / \ \/| | | / /
| || __________________________ | / | \/ \\ \___| | | / /
| || / \ | \____|__ /_______ / \______ /___|___| / /
| || / | | \/ \/ \/ / /
| || | | | __________ _____ _________ _____________________ / /
| || | / / \______ \ / _ \ \_ ___ \\_ _____/\______ \ / /
| || | / / | _/ / /_\ \/ \ \/ | __)_ | _/ / /
| || | / / | | \/ | \ \____| \ | | \ / /
| || | / / |____|_ /\____|__ /\______ /_______ / |____|_ / / /
| || | / / \/ \/ \/ \/ \/ / /
| || | / / / /
| || | / / / /
| || | / / / /
| || | | | / /
| || | | | / /
| || | \ \ / /
| || | \ \ / /
\ \/ / \ \____________________________________________________________________________________/ /
\ / \ /
\ / \ /
\ / \ /
\ / \ /
\____________________/ \_________________________________________________________________________________________________________/

View File

@ -65,3 +65,10 @@ float Racecar::getSpeed() const {
return speed;
}
float Racecar::getX() const {
return carText.getPosition().x;
}
float Racecar::getY() const {
return carText.getPosition().y;
}

View File

@ -17,6 +17,8 @@ public:
sf::Text getDrawable();
void setPosition(float x, float y);
float getSpeed() const; // Get the current speed of the car
float getX() const;
float getY() const;
private:
sf::Text carText;

Binary file not shown.