follow the player
This commit is contained in:
parent
abacd48eee
commit
defc393748
19
game.cpp
19
game.cpp
@ -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
34
game.h
@ -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
|
||||
|
||||
15
map
15
map
@ -31,18 +31,3 @@
|
||||
\ / \ /
|
||||
\ / \ /
|
||||
\____________________/ \_________________________________________________________________________________________________________/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
BIN
terminal_racer
BIN
terminal_racer
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user