Facebook
From valva-ro, 3 Years ago, written in C++.
This paste is a reply to Hacker's Maze - Game class from valva-ro - go back
Embed
#include 
#include 
#include "Player.hpp"
#include "Maze.hpp"

class Game {

private:
    sf::RenderWindow* window;
    Player player;
    Maze* maze;
    int width, height;

public:
    Game(int width, int height);
    ~Game();
    void start();
    void eventHandler();
    bool gameOver();
    sf::Text createText(const std::string &txt, const sf::Font &font, int size, sf::Color color, int posX = 0, int posY = 0);
};
  
onst const int FPS = 60;
const float CELL_SIZE = 50;
const float TEXT_SIZE = 40;
const float PLAYER_SPEED = 6;
const std::string WELCOME = "Welcome to the Hacker's Maze";
const std::string WELCOME_FONT_PATH = "../res/fonts/retro-pixel.ttf";
const std::string PLAYER_TEXTURE_PATH = "../res/textures/anonymous.png";

Game::Game(int width, int height) : player(sf::Color::Green, PLAYER_TEXTURE_PATH, PLAYER_SPEED, CELL_SIZE / 670) {
    this->width = width;
    this->height = height;
    this->window = new sf::RenderWindow(sf::VideoMode(width, height), "Hacker's Maze",
                                        sf::Style::Titlebar | sf::Style::Close);
    this->window->setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width * 0.5 - window->getSize().x * 0.5, sf::VideoMode::getDesktopMode().height * 0.5 - window->getSize().y * 0.5));
    this->window->setKeyRepeatEnabled(true);
    this->window->setFramerateLimit(FPS);

    int windowWidth = window->getSize().x;
    int windowHeight = window->getSize().y;
    const int mazeWidth = window->getSize().x - 100;
    const int mazeHeight = window->getSize().y - 150;

    this->player.setPosition(sf::Vector2f((5 + windowWidth - mazeWidth) / 2, (5 + windowHeight - mazeHeight + TEXT_SIZE) / 2));

    this->maze = new Maze(sf::Vector2f((windowWidth - mazeWidth) / 2, (windowHeight - mazeHeight + TEXT_SIZE) / 2),
                          sf::Vector2f(mazeWidth, mazeHeight), 4,
                          sf::Vector2f(mazeWidth / CELL_SIZE, mazeHeight / CELL_SIZE), sf::Color(0,0,0,0), sf::Color::Green);
}

Game::~Game() {
    delete maze;
    delete window;
}

void Game:: start() {
    while (window->isOpen() && !gameOver()) {
        eventHandler();
    }
}

bool Game::gameOver() {
    //TODO
}

void Game::eventHandler() {
    sf::Event event;
    while (window->pollEvent(event)) {
        if (event.type == sf::Event::Closed || (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape))
            window->close();
    }
    sf::Font welcomeFont;
    welcomeFont.loadFromFile(WELCOME_FONT_PATH);
    sf::Text welcome = createText(WELCOME, welcomeFont, TEXT_SIZE, sf::Color::Green, 100, 20);

    window->clear(sf::Color::Black);
    maze->update();
    player.move(maze);

    window->draw(welcome);
    player.draw(window);
    maze->draw(window);
    window->display();

}

sf::Text Game::createText(const std::string &txt, const sf::Font &font, int size, sf::Color color, int posX, int posY) {
    sf::Text text;
    text.setString(txt);
    text.setCharacterSize(size);
    text.setFont(font);
    text.setFillColor(color);
    text.setPosition(posX, posY);
    return text;
}