Facebook
From valva-ro, 1 Month ago, written in C++.
This paste is a reply to Hacker's Maze - Game class from valva-ro - view diff
Embed
Download Paste or View Raw
Hits: 40
  1. #include <SFML/Window.hpp>
  2. #include <SFML/Graphics.hpp>
  3. #include "Player.hpp"
  4. #include "Maze.hpp"
  5.  
  6. class Game {
  7.  
  8. private:
  9.     sf::RenderWindow* window;
  10.     Player player;
  11.     Maze* maze;
  12.     int width, height;
  13.  
  14. public:
  15.     Game(int width, int height);
  16.     ~Game();
  17.     void start();
  18.     void eventHandler();
  19.     bool gameOver();
  20.     sf::Text createText(const std::string &txt, const sf::Font &font, int size, sf::Color color, int posX = 0, int posY = 0);
  21. };
  22.  
  23. const int FPS = 60;
  24. const float CELL_SIZE = 50;
  25. const float TEXT_SIZE = 40;
  26. const float PLAYER_SPEED = 6;
  27. const std::string WELCOME = "Welcome to the Hacker's Maze";
  28. const std::string WELCOME_FONT_PATH = "../res/fonts/retro-pixel.ttf";
  29. const std::string PLAYER_TEXTURE_PATH = "../res/textures/anonymous.png";
  30.  
  31. Game::Game(int width, int height) : player(sf::Color::Green, PLAYER_TEXTURE_PATH, PLAYER_SPEED, CELL_SIZE / 670) {
  32.     this->width = width;
  33.     this->height = height;
  34.     this->window = new sf::RenderWindow(sf::VideoMode(width, height), "Hacker's Maze",
  35.                                         sf::Style::Titlebar | sf::Style::Close);
  36.     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));
  37.     this->window->setKeyRepeatEnabled(true);
  38.     this->window->setFramerateLimit(FPS);
  39.  
  40.     int windowWidth = window->getSize().x;
  41.     int windowHeight = window->getSize().y;
  42.     const int mazeWidth = window->getSize().x - 100;
  43.     const int mazeHeight = window->getSize().y - 150;
  44.  
  45.     this->player.setPosition(sf::Vector2f((5 + windowWidth - mazeWidth) / 2, (5 + windowHeight - mazeHeight + TEXT_SIZE) / 2));
  46.  
  47.     this->maze = new Maze(sf::Vector2f((windowWidth - mazeWidth) / 2, (windowHeight - mazeHeight + TEXT_SIZE) / 2),
  48.                           sf::Vector2f(mazeWidth, mazeHeight), 4,
  49.                           sf::Vector2f(mazeWidth / CELL_SIZE, mazeHeight / CELL_SIZE), sf::Color(0,0,0,0), sf::Color::Green);
  50. }
  51.  
  52. Game::~Game() {
  53.     delete maze;
  54.     delete window;
  55. }
  56.  
  57. void Game:: start() {
  58.     while (window->isOpen() && !gameOver()) {
  59.         eventHandler();
  60.     }
  61. }
  62.  
  63. bool Game::gameOver() {
  64.     //TODO
  65. }
  66.  
  67. void Game::eventHandler() {
  68.     sf::Event event;
  69.     while (window->pollEvent(event)) {
  70.         if (event.type == sf::Event::Closed || (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape))
  71.             window->close();
  72.     }
  73.     sf::Font welcomeFont;
  74.     welcomeFont.loadFromFile(WELCOME_FONT_PATH);
  75.     sf::Text welcome = createText(WELCOME, welcomeFont, TEXT_SIZE, sf::Color::Green, 100, 20);
  76.  
  77.     window->clear(sf::Color::Black);
  78.     maze->update();
  79.     player.move(maze);
  80.  
  81.     window->draw(welcome);
  82.     player.draw(window);
  83.     maze->draw(window);
  84.     window->display();
  85.  
  86. }
  87.  
  88. sf::Text Game::createText(const std::string &txt, const sf::Font &font, int size, sf::Color color, int posX, int posY) {
  89.     sf::Text text;
  90.     text.setString(txt);
  91.     text.setCharacterSize(size);
  92.     text.setFont(font);
  93.     text.setFillColor(color);
  94.     text.setPosition(posX, posY);
  95.     return text;
  96. }