music
This commit is contained in:
parent
11e7558feb
commit
6948896d9f
BIN
background_music.wav
Normal file
BIN
background_music.wav
Normal file
Binary file not shown.
152
game.cpp
152
game.cpp
@ -9,6 +9,17 @@ Game::Game()
|
||||
std::cerr << "Failed to load font" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (!backgroundMusic.openFromFile("background_music.wav")) {
|
||||
std::cerr << "Failed to load background music" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Set the volume (adjust as needed)
|
||||
backgroundMusic.setVolume(50);
|
||||
|
||||
// Play the background music
|
||||
backgroundMusic.play();
|
||||
|
||||
// Create a text object for displaying speed
|
||||
speedText.setFont(font);
|
||||
@ -18,6 +29,7 @@ Game::Game()
|
||||
mapText.setCharacterSize(24);
|
||||
mapText.setFillColor(sf::Color::White);
|
||||
mapText.setPosition(0, 0);
|
||||
mapText.setOutlineColor(sf::Color::Red);
|
||||
|
||||
timerText.setFont(font);
|
||||
timerText.setCharacterSize(24);
|
||||
@ -27,7 +39,7 @@ Game::Game()
|
||||
racecar.setRotation(180);
|
||||
|
||||
// Initialize the times and quadrants data structure
|
||||
timesAndQuadrants.push_back(std::make_pair(1, "00:00:00.000"));
|
||||
timesAndQuadrants.push_back(std::make_pair(1, "00:00:000"));
|
||||
}
|
||||
|
||||
void Game::run() {
|
||||
@ -37,10 +49,15 @@ void Game::run() {
|
||||
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);
|
||||
|
||||
backgroundMusic.setLoop(true);
|
||||
|
||||
while (window.isOpen()) {
|
||||
float dt = clock.restart().asSeconds();
|
||||
|
||||
if(lapPoints.size() < 3) {
|
||||
|
||||
|
||||
// Update game time
|
||||
sf::Time gameTime = gameTimeClock.getElapsedTime();
|
||||
int milis = static_cast<int>(gameTime.asMilliseconds());
|
||||
@ -64,15 +81,18 @@ void Game::run() {
|
||||
timesAndQuadrants.push_back(std::make_pair(currentQuadrant, timerString));
|
||||
}
|
||||
|
||||
determineLap(timerString);
|
||||
determineLap(static_cast<int>(gameTime.asMilliseconds()), timerString);
|
||||
}
|
||||
|
||||
sf::Event event;
|
||||
while (window.pollEvent(event)) {
|
||||
if (event.type == sf::Event::Closed) {
|
||||
window.close();
|
||||
backgroundMusic.stop();
|
||||
}
|
||||
}
|
||||
|
||||
if(lapPoints.size() < 3) {
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
|
||||
if (racecar.getSpeed() < 0) {
|
||||
racecar.brake(dt);
|
||||
@ -83,8 +103,11 @@ void Game::run() {
|
||||
|
||||
if (!isTrackCollision(newX, newY)) {
|
||||
racecar.accelerate(dt);
|
||||
mapText.setOutlineThickness(0);
|
||||
} else {
|
||||
racecar.stop();
|
||||
//racecar.stop();
|
||||
modifier += 0.01;
|
||||
mapText.setOutlineThickness(6);
|
||||
}
|
||||
}
|
||||
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
|
||||
@ -97,8 +120,11 @@ void Game::run() {
|
||||
|
||||
if (!isTrackCollision(newX, newY)) {
|
||||
racecar.reverse(dt);
|
||||
mapText.setOutlineThickness(0);
|
||||
} else {
|
||||
racecar.stop();
|
||||
//racecar.stop();
|
||||
modifier += 0.01;
|
||||
mapText.setOutlineThickness(6);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@ -133,13 +159,21 @@ void Game::run() {
|
||||
// Update the view to follow the racecar
|
||||
view.setCenter(racecar.getX(), racecar.getY());
|
||||
window.setView(view);
|
||||
|
||||
}
|
||||
|
||||
window.clear();
|
||||
mapText.setString(asciiMap);
|
||||
window.draw(mapText);
|
||||
window.draw(racecar.getDrawable());
|
||||
window.draw(speedText);
|
||||
window.draw(timerText);
|
||||
|
||||
|
||||
if (lapPoints.size() == 3) {
|
||||
displayEndGame();
|
||||
}
|
||||
else {
|
||||
window.draw(timerText);
|
||||
}
|
||||
displaySavedTimes();
|
||||
window.display();
|
||||
|
||||
@ -149,23 +183,23 @@ void Game::run() {
|
||||
|
||||
bool Game::isTrackCollision(float x, float y) {
|
||||
if (x <= 0) {
|
||||
// return true;
|
||||
return true;
|
||||
} else if (y <= 0) {
|
||||
// return true;
|
||||
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;
|
||||
// }
|
||||
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
|
||||
}
|
||||
|
||||
void Game::determineLap(std::string timerString) {
|
||||
void Game::determineLap(int millis, std::string timerString) {
|
||||
lapFinished = true;
|
||||
int requiredOrder[] = {1,2,1,2,3,4,1};
|
||||
if(timesAndQuadrants.size() >= 7) {
|
||||
@ -179,9 +213,14 @@ void Game::determineLap(std::string timerString) {
|
||||
}
|
||||
|
||||
if (lapFinished) {
|
||||
lapTimes.push_back(timerString);
|
||||
int penality = (millis - lastRound) * modifier;
|
||||
if (penality > 100000 ) penality = 100000;
|
||||
unsigned int points = (100000 - penality);
|
||||
lapPoints.push_back(points);
|
||||
timesAndQuadrants.clear();
|
||||
timesAndQuadrants.push_back(std::make_pair(1, timerString));
|
||||
lastRound = millis;
|
||||
modifier = 1.0f;
|
||||
}
|
||||
}
|
||||
|
||||
@ -201,8 +240,8 @@ int Game::determineQuadrant(float x, float y) {
|
||||
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;
|
||||
for (const auto& lapTime : lapPoints) {
|
||||
std::string timeString = "Lap points: " + std::to_string(lapTime);
|
||||
sf::Text text;
|
||||
text.setFont(font);
|
||||
text.setCharacterSize(18);
|
||||
@ -224,3 +263,78 @@ void Game::displaySavedTimes() {
|
||||
textY += 30; // Adjust the spacing as needed
|
||||
}
|
||||
}
|
||||
|
||||
void Game::displayEndGame() {
|
||||
// Load high scores from a file
|
||||
loadHighScores();
|
||||
|
||||
// Get the best score from the current run
|
||||
std::sort(lapPoints.begin(), lapPoints.end());
|
||||
int bestScore = lapPoints.back();
|
||||
|
||||
// If the new score is in the top ten, add it
|
||||
if (highScores.size() < 10 || bestScore > highScores.back()) {
|
||||
highScores.push_back(bestScore);
|
||||
}
|
||||
|
||||
// Sort the high scores in descending order
|
||||
std::sort(highScores.rbegin(), highScores.rend());
|
||||
|
||||
// Keep only the top ten scores
|
||||
if (highScores.size() > 10) {
|
||||
highScores.pop_back();
|
||||
}
|
||||
|
||||
// Write the updated high scores back to the file
|
||||
writeHighScores();
|
||||
|
||||
// Display the best score from the current run and the top ten high scores
|
||||
int textY = 10; // Adjust the vertical position as needed
|
||||
|
||||
// Display the best score from the current run
|
||||
std::string bestScoreString = "Best Lap: " + std::to_string(bestScore);
|
||||
sf::Text bestScoreText;
|
||||
bestScoreText.setFont(font);
|
||||
bestScoreText.setCharacterSize(20);
|
||||
bestScoreText.setFillColor(sf::Color::White);
|
||||
bestScoreText.setPosition(racecar.getX(), (racecar.getY() - 300) + textY);
|
||||
bestScoreText.setString(bestScoreString);
|
||||
window.draw(bestScoreText);
|
||||
textY += 30; // Adjust the spacing as needed
|
||||
|
||||
// Display the top ten high scores
|
||||
for (size_t i = 0; i < highScores.size(); ++i) {
|
||||
std::string scoreString = "High Score #" + std::to_string(i + 1) + ": " + std::to_string(highScores[i]);
|
||||
sf::Text scoreText;
|
||||
scoreText.setFont(font);
|
||||
scoreText.setCharacterSize(20);
|
||||
scoreText.setFillColor(sf::Color::White);
|
||||
scoreText.setPosition(racecar.getX(), (racecar.getY() - 300) + textY);
|
||||
scoreText.setString(scoreString);
|
||||
window.draw(scoreText);
|
||||
textY += 30; // Adjust the spacing as needed
|
||||
}
|
||||
}
|
||||
|
||||
void Game::loadHighScores() {
|
||||
std::ifstream file(highScoresFile);
|
||||
if (file.is_open()) {
|
||||
int score;
|
||||
while (file >> score) {
|
||||
highScores.push_back(score);
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
// Function to write high scores to a file
|
||||
void Game::writeHighScores() {
|
||||
std::ofstream file(highScoresFile);
|
||||
if (file.is_open()) {
|
||||
for (const int& score : highScores) {
|
||||
file << score << std::endl;
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
15
game.h
15
game.h
@ -2,8 +2,11 @@
|
||||
#define GAME_H
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <SFML/Audio.hpp>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <fstream>
|
||||
#include <algorithm>
|
||||
#include "racecar.h"
|
||||
|
||||
class Game {
|
||||
@ -13,7 +16,10 @@ public:
|
||||
bool isTrackCollision(float x, float y);
|
||||
int determineQuadrant(float x, float y);
|
||||
void displaySavedTimes();
|
||||
void determineLap(std::string timerString);
|
||||
void determineLap(int millis, std::string timerString);
|
||||
void displayEndGame();
|
||||
void loadHighScores();
|
||||
void writeHighScores();
|
||||
|
||||
private:
|
||||
int max_x;
|
||||
@ -29,7 +35,12 @@ private:
|
||||
Racecar racecar;
|
||||
std::vector<std::pair<int, std::string>> timesAndQuadrants; // Store times and quadrants
|
||||
bool lapFinished;
|
||||
std::vector<std::string> lapTimes; // Store lap times
|
||||
unsigned int lastRound = 0;
|
||||
const std::string highScoresFile = "high_scores.txt";
|
||||
std::vector<int> highScores;
|
||||
std::vector<unsigned int> lapPoints; // Store lap times
|
||||
float modifier = 1.0f;
|
||||
sf::Music backgroundMusic;
|
||||
std::string asciiMap = " /------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\\ \n"
|
||||
" / \\ \n"
|
||||
" / | \n"
|
||||
|
||||
1521272
high_scores.txt
Normal file
1521272
high_scores.txt
Normal file
File diff suppressed because it is too large
Load Diff
BIN
terminal_racer
BIN
terminal_racer
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user