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