acceleration, deceleration and steer
This commit is contained in:
parent
2b7f650111
commit
c2da282e17
48
game.cpp
48
game.cpp
@ -3,21 +3,25 @@
|
||||
#define DELAY 30000
|
||||
|
||||
Game::Game()
|
||||
: x(0), y(0), max_x(800), max_y(600), direction(0),
|
||||
window(sf::VideoMode(max_x, max_y), "SFML Window") {
|
||||
: x(0), y(0), max_x(800), max_y(600), direction(0), window(sf::VideoMode(max_x, max_y), "SFML Window"),
|
||||
racecar(200.0f, 10.0f, 1.0f) { // Adjust maxSpeed, acceleration, and steerSpeed values as needed
|
||||
if (!font.loadFromFile("cascaydia.otf")) {
|
||||
std::cerr << "Failed to load font" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
text.setString("o");
|
||||
text.setFont(font);
|
||||
text.setCharacterSize(24);
|
||||
sf::Text text("o", font, 24); // This is just for the track, you can modify it as needed
|
||||
text.setFillColor(sf::Color::White);
|
||||
|
||||
racecar.setPosition(max_x / 2, max_y / 2); // Set initial position for the racecar
|
||||
}
|
||||
|
||||
void Game::run() {
|
||||
sf::Clock clock;
|
||||
|
||||
while (window.isOpen()) {
|
||||
float dt = clock.restart().asSeconds();
|
||||
|
||||
sf::Event event;
|
||||
while (window.pollEvent(event)) {
|
||||
if (event.type == sf::Event::Closed) {
|
||||
@ -25,23 +29,31 @@ void Game::run() {
|
||||
}
|
||||
}
|
||||
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && y > 0) {
|
||||
y--;
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && y < max_y - 1) {
|
||||
y++;
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && x > 0) {
|
||||
x--;
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && x < max_x - 1) {
|
||||
x++;
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
|
||||
racecar.accelerate(dt);
|
||||
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
|
||||
if (racecar.getSpeed() > 0) {
|
||||
racecar.brake(dt); // Apply brakes
|
||||
} else {
|
||||
racecar.reverse(dt); // Reverse the car
|
||||
}
|
||||
} else {
|
||||
racecar.decelerate(dt); // No key pressed, decelerate as usual
|
||||
}
|
||||
|
||||
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
|
||||
racecar.steer(dt, -1.0f); // Steer left
|
||||
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
|
||||
racecar.steer(dt, 1.0f); // Steer right
|
||||
}
|
||||
|
||||
racecar.update(dt);
|
||||
|
||||
window.clear();
|
||||
text.setPosition(x, y);
|
||||
window.draw(text);
|
||||
window.draw(racecar.getDrawable()); // Draw the racecar
|
||||
window.display();
|
||||
|
||||
sf::sleep(sf::microseconds(DELAY));
|
||||
}
|
||||
}
|
||||
|
||||
3
game.h
3
game.h
@ -3,6 +3,7 @@
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <iostream>
|
||||
#include "racecar.h"
|
||||
|
||||
class Game {
|
||||
public:
|
||||
@ -17,7 +18,7 @@ private:
|
||||
int direction;
|
||||
sf::RenderWindow window;
|
||||
sf::Font font;
|
||||
sf::Text text;
|
||||
Racecar racecar;
|
||||
};
|
||||
|
||||
#endif // GAME_H
|
||||
|
||||
63
racecar.cpp
63
racecar.cpp
@ -1,7 +1,64 @@
|
||||
#include "racecar.h"
|
||||
|
||||
Racecar::Racecar() {
|
||||
// Add the implementation for the Racecar class constructor here if needed.
|
||||
// You can include any member variables and functions specific to Racecar.
|
||||
Racecar::Racecar(float maxSpeed, float acceleration, float steerSpeed)
|
||||
: maxSpeed(maxSpeed), acceleration(acceleration), steerSpeed(steerSpeed), dx(0), dy(0), steeringAngle(0) {
|
||||
if (!font.loadFromFile("cascaydia.otf")) {
|
||||
std::cerr << "Failed to load font" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
carText.setString("0"); // Replace "o" with a racecar emoji or any desired character
|
||||
carText.setFont(font);
|
||||
carText.setCharacterSize(24);
|
||||
carText.setFillColor(sf::Color::White);
|
||||
speed = 0;
|
||||
}
|
||||
|
||||
void Racecar::update(float dt) {
|
||||
carText.move(speed * dt * cos(steeringAngle), speed * dt * sin(steeringAngle));
|
||||
}
|
||||
|
||||
void Racecar::accelerate(float dt) {
|
||||
if (speed < maxSpeed) {
|
||||
speed += acceleration * dt;
|
||||
}
|
||||
}
|
||||
|
||||
void Racecar::decelerate(float dt) {
|
||||
if (speed < 0) {
|
||||
speed += acceleration * dt;
|
||||
} else if (speed > 0) {
|
||||
speed -= acceleration * dt;
|
||||
} else if (speed > -maxSpeed / 2) {
|
||||
speed -= 1.5 * acceleration * dt;
|
||||
}
|
||||
}
|
||||
|
||||
void Racecar::steer(float dt, float direction) {
|
||||
steeringAngle += steerSpeed * direction * dt;
|
||||
}
|
||||
|
||||
void Racecar::brake(float dt) {
|
||||
if (speed > 0) {
|
||||
speed -= 3 * acceleration * dt;
|
||||
}
|
||||
}
|
||||
|
||||
void Racecar::reverse(float dt) {
|
||||
if (speed > -maxSpeed / 2) {
|
||||
speed -= 1.5 * acceleration * dt;
|
||||
}
|
||||
}
|
||||
|
||||
sf::Text Racecar::getDrawable() {
|
||||
return carText;
|
||||
}
|
||||
|
||||
void Racecar::setPosition(float x, float y) {
|
||||
carText.setPosition(x, y);
|
||||
}
|
||||
|
||||
float Racecar::getSpeed() const {
|
||||
return speed;
|
||||
}
|
||||
|
||||
|
||||
27
racecar.h
27
racecar.h
@ -1,10 +1,33 @@
|
||||
#ifndef RACECAR_H
|
||||
#define RACECAR_H
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
|
||||
class Racecar {
|
||||
public:
|
||||
Racecar();
|
||||
// Add any member functions or variables for the Racecar class here if needed.
|
||||
Racecar(float maxSpeed, float acceleration, float steerSpeed);
|
||||
void update(float dt);
|
||||
void accelerate(float dt);
|
||||
void decelerate(float dt);
|
||||
void brake(float dt); // Brake the car
|
||||
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);
|
||||
float getSpeed() const; // Get the current speed of the car
|
||||
|
||||
private:
|
||||
sf::Text carText;
|
||||
sf::Font font;
|
||||
float speed;
|
||||
float maxSpeed;
|
||||
float acceleration;
|
||||
float steerSpeed;
|
||||
float steeringAngle;
|
||||
float dx;
|
||||
float dy;
|
||||
};
|
||||
|
||||
#endif // RACECAR_H
|
||||
|
||||
BIN
terminal_racer
BIN
terminal_racer
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user