divide into files

This commit is contained in:
thatscringebro 2023-10-10 10:13:48 -04:00
parent 0c1d8211f3
commit f85888d35a
7 changed files with 94 additions and 53 deletions

View File

@ -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));
}
}

24
game.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef GAME_H
#define GAME_H
#include <SFML/Graphics.hpp>
#include <iostream>
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

View File

@ -1,60 +1,11 @@
#include <SFML/Graphics.hpp>
#include <iostream>
#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;
}

7
racecar.cpp Normal file
View File

@ -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.
}

11
racecar.h Normal file
View File

@ -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

View File

BIN
terminal_racer Executable file

Binary file not shown.