bot
This commit is contained in:
parent
52342fc91d
commit
151877b298
54
game.cpp
54
game.cpp
@ -4,7 +4,7 @@
|
||||
|
||||
Game::Game()
|
||||
:max_x(1000), max_y(800), direction(0), window(sf::VideoMode(max_x, max_y), "ASCII RACER"),
|
||||
racecar(400.0f, 30.0f, 1.7f) { // maxSpeed, acceleration, and steerSpeed values
|
||||
racecar(400.0f, 30.0f, 1.7f, sf::Color::Red), bot(400.0f, 30.0f, 1.7f, sf::Color::Green) { // maxSpeed, acceleration, and steerSpeed values
|
||||
if (!font.loadFromFile("cascaydia.otf")) {
|
||||
std::cerr << "Failed to load font" << std::endl;
|
||||
exit(1);
|
||||
@ -39,6 +39,9 @@ Game::Game()
|
||||
racecar.setPosition(1000, 80); // Set initial position for the racecar
|
||||
racecar.setRotation(180);
|
||||
|
||||
bot.setPosition(1000, 80); // Set initial position for the bot
|
||||
bot.setRotation(180);
|
||||
|
||||
// Initialize the times and quadrants data structure
|
||||
timesAndQuadrants.push_back(std::make_pair(1, "00:00:000"));
|
||||
}
|
||||
@ -46,6 +49,8 @@ Game::Game()
|
||||
void Game::run() {
|
||||
sf::Clock clock;
|
||||
|
||||
loadPlayerMovesFromFile("player_moves.txt");
|
||||
|
||||
// Create a view that will follow the racecar
|
||||
view = sf::View(sf::FloatRect(0, 0, max_x, max_y));
|
||||
view.setCenter(racecar.getX(), racecar.getY()); // Center the view on the racecar
|
||||
@ -53,11 +58,15 @@ void Game::run() {
|
||||
|
||||
backgroundMusic.setLoop(true);
|
||||
|
||||
int currentMove = 0;
|
||||
|
||||
while (window.isOpen()) {
|
||||
float dt = clock.restart().asSeconds();
|
||||
|
||||
if(lapPoints.size() < 3) {
|
||||
|
||||
botMove(currentMove, dt);
|
||||
currentMove++;
|
||||
|
||||
// Update game time
|
||||
sf::Time gameTime = gameTimeClock.getElapsedTime();
|
||||
@ -174,6 +183,7 @@ void Game::run() {
|
||||
}
|
||||
|
||||
racecar.update(dt);
|
||||
bot.update(dt);
|
||||
|
||||
nitroText.setString("Nitro: " + std::to_string(nitro));
|
||||
nitroText.setCharacterSize(24);
|
||||
@ -206,6 +216,7 @@ void Game::run() {
|
||||
mapText.setString(asciiMap);
|
||||
window.draw(mapText);
|
||||
window.draw(racecar.getDrawable());
|
||||
window.draw(bot.getDrawable());
|
||||
window.draw(speedText);
|
||||
window.draw(nitroText);
|
||||
|
||||
@ -400,3 +411,44 @@ void Game::writeHighScores() {
|
||||
}
|
||||
}
|
||||
|
||||
void Game::loadPlayerMovesFromFile(const std::string& filename) {
|
||||
std::ifstream file(filename);
|
||||
if (file.is_open()) {
|
||||
int move;
|
||||
while (file >> move) {
|
||||
lastPlayerMoves.push_back(move);
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
void Game::botMove(int i, float dt) {
|
||||
int move = lastPlayerMoves[i];
|
||||
switch (move) {
|
||||
case 0:
|
||||
bot.brake(dt);
|
||||
break;
|
||||
case 1:
|
||||
bot.accelerate(dt);
|
||||
break;
|
||||
case 2:
|
||||
bot.decelerate(dt);
|
||||
break;
|
||||
case 3:
|
||||
bot.decelerate(dt);
|
||||
bot.steer(dt, -1.0f);
|
||||
break;
|
||||
case 4:
|
||||
bot.decelerate(dt);
|
||||
bot.steer(dt, 1.0f);
|
||||
break;
|
||||
case 5:
|
||||
bot.accelerate(dt);
|
||||
bot.steer(dt, -1.0f);
|
||||
break;
|
||||
case 6:
|
||||
bot.accelerate(dt);
|
||||
bot.steer(dt, 1.0f);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
4
game.h
4
game.h
@ -20,6 +20,8 @@ public:
|
||||
void displayEndGame();
|
||||
void loadHighScores();
|
||||
void writeHighScores();
|
||||
void loadPlayerMovesFromFile(const std::string& filename);
|
||||
void botMove(int i, float dt);
|
||||
|
||||
private:
|
||||
int max_x;
|
||||
@ -34,6 +36,8 @@ private:
|
||||
sf::Clock gameTimeClock;
|
||||
sf::View view;
|
||||
Racecar racecar;
|
||||
Racecar bot;
|
||||
std::vector<int> lastPlayerMoves;
|
||||
std::vector<std::pair<int, std::string>> timesAndQuadrants; // Store times and quadrants
|
||||
bool lapFinished;
|
||||
unsigned int lastRound = 0;
|
||||
|
||||
@ -1,2 +1,3 @@
|
||||
79725
|
||||
79512
|
||||
78055
|
||||
|
||||
1999
player_moves.txt
Normal file
1999
player_moves.txt
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
||||
#include "racecar.h"
|
||||
|
||||
Racecar::Racecar(float maxSpeed, float acceleration, float steerSpeed)
|
||||
Racecar::Racecar(float maxSpeed, float acceleration, float steerSpeed, sf::Color color)
|
||||
: maxSpeed(maxSpeed), acceleration(acceleration), steerSpeed(steerSpeed), dx(0), dy(0), steeringAngle(3.13) {
|
||||
if (!font.loadFromFile("cascaydia.otf")) {
|
||||
std::cerr << "Failed to load font" << std::endl;
|
||||
@ -10,7 +10,7 @@ Racecar::Racecar(float maxSpeed, float acceleration, float steerSpeed)
|
||||
carText.setString("|:=:>"); // Replace "o" with a racecar emoji or any desired character
|
||||
carText.setFont(font);
|
||||
carText.setCharacterSize(12);
|
||||
carText.setFillColor(sf::Color::White);
|
||||
carText.setFillColor(color);
|
||||
|
||||
speed = 0;
|
||||
}
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
|
||||
class Racecar {
|
||||
public:
|
||||
Racecar(float maxSpeed, float acceleration, float steerSpeed);
|
||||
Racecar(float maxSpeed, float acceleration, float steerSpeed, sf::Color color);
|
||||
void update(float dt);
|
||||
void accelerate(float dt);
|
||||
void boost(float dt);
|
||||
|
||||
BIN
terminal_racer
BIN
terminal_racer
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user