started collissions
This commit is contained in:
parent
e3741047bc
commit
111446f8ac
25
game.cpp
25
game.cpp
@ -3,7 +3,7 @@
|
||||
#define DELAY 30000
|
||||
|
||||
Game::Game()
|
||||
:max_x(1000), max_y(800), direction(0), window(sf::VideoMode(max_x, max_y), "SFML Window"),
|
||||
: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
|
||||
if (!font.loadFromFile("cascaydia.otf")) {
|
||||
std::cerr << "Failed to load font" << std::endl;
|
||||
@ -19,7 +19,8 @@ Game::Game()
|
||||
mapText.setFillColor(sf::Color::White);
|
||||
mapText.setPosition(10, 10);
|
||||
|
||||
racecar.setPosition(max_x / 2, max_y / 2); // Set initial position for the racecar
|
||||
racecar.setPosition(1000, 80); // Set initial position for the racecar
|
||||
racecar.setRotation(180);
|
||||
}
|
||||
|
||||
void Game::run() {
|
||||
@ -44,13 +45,23 @@ void Game::run() {
|
||||
if (racecar.getSpeed() < 0) {
|
||||
racecar.brake(dt); // Apply brakes
|
||||
} else {
|
||||
racecar.accelerate(dt); // Reverse the car
|
||||
if(!isTrackCollision(racecar.getX(), racecar.getY())) {
|
||||
racecar.accelerate(dt); // accelerate the car
|
||||
}
|
||||
else {
|
||||
racecar.stop();
|
||||
}
|
||||
}
|
||||
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
|
||||
if (racecar.getSpeed() > 0) {
|
||||
racecar.brake(dt); // Apply brakes
|
||||
} else {
|
||||
racecar.reverse(dt); // Reverse the car
|
||||
if(!isTrackCollision(racecar.getX(), racecar.getY())) {
|
||||
racecar.reverse(dt); // Reverse the car
|
||||
}
|
||||
else {
|
||||
racecar.stop();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
racecar.decelerate(dt); // No key pressed, decelerate as usual
|
||||
@ -81,7 +92,6 @@ void Game::run() {
|
||||
speedText.setFillColor(sf::Color::Red);
|
||||
}
|
||||
|
||||
|
||||
// Update the view to follow the racecar
|
||||
view.setCenter(racecar.getX(), racecar.getY());
|
||||
window.setView(view);
|
||||
@ -97,3 +107,8 @@ 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 == '-');
|
||||
}
|
||||
|
||||
1
game.h
1
game.h
@ -9,6 +9,7 @@ class Game {
|
||||
public:
|
||||
Game();
|
||||
void run();
|
||||
bool isTrackCollision(float x, float y);
|
||||
|
||||
private:
|
||||
int max_x;
|
||||
|
||||
10
racecar.cpp
10
racecar.cpp
@ -1,7 +1,7 @@
|
||||
#include "racecar.h"
|
||||
|
||||
Racecar::Racecar(float maxSpeed, float acceleration, float steerSpeed)
|
||||
: maxSpeed(maxSpeed), acceleration(acceleration), steerSpeed(steerSpeed), dx(0), dy(0), steeringAngle(0) {
|
||||
: 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;
|
||||
exit(1);
|
||||
@ -52,6 +52,10 @@ void Racecar::brake(float dt) {
|
||||
}
|
||||
}
|
||||
|
||||
void Racecar::stop() {
|
||||
speed = 0;
|
||||
}
|
||||
|
||||
void Racecar::reverse(float dt) {
|
||||
if (speed > -maxSpeed / 2) {
|
||||
speed -= 1.5 * acceleration * dt;
|
||||
@ -66,6 +70,10 @@ void Racecar::setPosition(float x, float y) {
|
||||
carText.setPosition(x, y);
|
||||
}
|
||||
|
||||
void Racecar::setRotation(float value) {
|
||||
carText.setRotation(value);
|
||||
}
|
||||
|
||||
float Racecar::getSpeed() const {
|
||||
return speed;
|
||||
}
|
||||
|
||||
@ -12,10 +12,12 @@ public:
|
||||
void accelerate(float dt);
|
||||
void decelerate(float dt);
|
||||
void brake(float dt); // Brake the car
|
||||
void stop();
|
||||
void reverse(float dt); // Reverse the car
|
||||
void steer(float dt, float direction); // -1 for left, 1 for right
|
||||
sf::Text getDrawable();
|
||||
void setPosition(float x, float y);
|
||||
void setRotation(float value);
|
||||
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