diff --git a/game.cpp b/game.cpp index e69de29..90ee2f8 100644 --- a/game.cpp +++ b/game.cpp @@ -0,0 +1,48 @@ +#include "game.h" + +#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") { + if (!font.loadFromFile("cascaydia.otf")) { + std::cerr << "Failed to load font" << std::endl; + exit(1); + } + + text.setString("o"); + text.setFont(font); + text.setCharacterSize(24); + text.setFillColor(sf::Color::White); +} + +void Game::run() { + while (window.isOpen()) { + sf::Event event; + while (window.pollEvent(event)) { + if (event.type == sf::Event::Closed) { + window.close(); + } + } + + 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++; + } + + window.clear(); + text.setPosition(x, y); + window.draw(text); + window.display(); + sf::sleep(sf::microseconds(DELAY)); + } +} + diff --git a/game.h b/game.h new file mode 100644 index 0000000..22de421 --- /dev/null +++ b/game.h @@ -0,0 +1,24 @@ +#ifndef GAME_H +#define GAME_H + +#include +#include + +class Game { +public: + Game(); + void run(); + +private: + int x; + int y; + int max_x; + int max_y; + int direction; + sf::RenderWindow window; + sf::Font font; + sf::Text text; +}; + +#endif // GAME_H + diff --git a/main.cpp b/main.cpp index ab0349a..a41e8aa 100644 --- a/main.cpp +++ b/main.cpp @@ -1,60 +1,11 @@ #include #include - -#define DELAY 30000 +#include "game.h" +#include "racecar.h" int main(int argc, char** argv) { - int x = 0, y = 0; - int max_x = 800, max_y = 600; - int direction = 0; - - sf::RenderWindow window(sf::VideoMode(max_x, max_y), "SFML Window"); - - sf::Font font; - if (!font.loadFromFile("cascaydia.otf")) { - std::cerr << "Failed to load font" << std::endl; - return 1; - } - - sf::Text text("o", font, 24); - text.setFillColor(sf::Color::White); - - while (window.isOpen()) { - sf::Event event; - while (window.pollEvent(event)) { - if (event.type == sf::Event::Closed) { - window.close(); - } - } - - 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++; - } - - // Clear the window - window.clear(); - - // Set the position of the text - text.setPosition(x, y); - - // Draw the text on the window - window.draw(text); - - // Display everything that was drawn - window.display(); - - sf::sleep(sf::microseconds(DELAY)); - } + Game game; + game.run(); return 0; } - diff --git a/racecar.cpp b/racecar.cpp new file mode 100644 index 0000000..3d49b03 --- /dev/null +++ b/racecar.cpp @@ -0,0 +1,7 @@ +#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. +} + diff --git a/racecar.h b/racecar.h new file mode 100644 index 0000000..c301646 --- /dev/null +++ b/racecar.h @@ -0,0 +1,11 @@ +#ifndef RACECAR_H +#define RACECAR_H + +class Racecar { +public: + Racecar(); + // Add any member functions or variables for the Racecar class here if needed. +}; + +#endif // RACECAR_H + diff --git a/racingcar.cpp b/racingcar.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/terminal_racer b/terminal_racer new file mode 100755 index 0000000..2287ac3 Binary files /dev/null and b/terminal_racer differ