SHARE
TWEET

Untitled

a guest Nov 19th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Game.h"
  2.  
  3. Game::Game() {
  4.  
  5. }
  6.  
  7. void Game::_init() {
  8.     _gameOver = false;
  9.     _score = 0;
  10.     _lblSstr << "Score: " << _score;
  11.     _lblFontArial.loadFromFile("fonts\\arial.ttf");
  12.     _lblScore.setCharacterSize(30);
  13.     _lblScore.setPosition(10, 10);
  14.     _lblScore.setFont(_lblFontArial);
  15.     _lblScore.setString(_lblSstr.str());
  16.     for (int i = 0; i < NUMBER_OF_COINS; i++) {
  17.         _coins[i].init();
  18.     }
  19.     for (int i = 0; i < NUMBER_OF_ENEMIES; i++) {
  20.         _enemies[i].init({ 30, 30 }, {(float)(rand() % 700 + 100), (float)(rand() % 400 + 100)});
  21.     }
  22. }
  23.  
  24. void Game::_victory() {
  25.     _lblSstr.str("");
  26.     _lblSstr << "You won!";
  27.     _lblScore.setPosition(325, 250);
  28.     _lblScore.setString(_lblSstr.str());
  29.     _gameOver = true;
  30. }
  31.  
  32. void Game::_scoreCheck() {
  33.     if (_score >= 100) {
  34.         _victory();
  35.     }
  36.  }
  37.  
  38. void Game::_checkForInput()
  39. {
  40.     if (GetAsyncKeyState(VK_UP))
  41.     {
  42.         _player.MoveDirection = 'U';
  43.     }
  44.     else if (GetAsyncKeyState(VK_DOWN)) {
  45.         _player.MoveDirection = 'D';
  46.     }
  47.     else if (GetAsyncKeyState(VK_RIGHT))
  48.     {
  49.         _player.MoveDirection = 'R';
  50.     }
  51.     else if (GetAsyncKeyState(VK_LEFT)) {
  52.         _player.MoveDirection = 'L';
  53.     }
  54.     else {
  55.         _player.MoveDirection = 'C';
  56.     }
  57. }
  58.  
  59. void Game::_updateWorld() {
  60.     _lblSstr.str("");
  61.     _lblSstr << "Score: " << _score;
  62.     _lblScore.setString(_lblSstr.str());
  63.     _player.checkForMovement();
  64.     _scoreCheck();
  65.     for (int i = 0; i < NUMBER_OF_ENEMIES; i++) {
  66.         _enemies[i].updateEnemy();
  67.         _enemies[i].checkCollision(_player, _lblScore, _lblSstr, _gameOver);
  68.     }
  69.     for (int i = 0; i < NUMBER_OF_COINS; i++) {
  70.         _coins[i].checkCollision(_player, _score);
  71.     }
  72. }
  73.  
  74. void Game::_draw(sf::RenderWindow &window)
  75. {
  76.     _player.drawTo(window);
  77.     for (int i = 0; i < NUMBER_OF_COINS; i++) {
  78.         _coins[i].drawTo(window);
  79.     }
  80.     for (int i = 0; i < NUMBER_OF_ENEMIES; i++) {
  81.         _enemies[i].drawTo(window);
  82.     }
  83.     window.draw(_lblScore);
  84.  
  85. }
  86.  
  87. void Game::start()
  88. {
  89.     srand(time(NULL));
  90.     _init();
  91.     RenderWindow app(VideoMode(GAME_WIDTH, GAME_HEIGHT), "Cube Game", Style::Titlebar | Style::Close);
  92.     Event WINDOW_CLOSE_EVENT;
  93.     while (app.isOpen())
  94.     {
  95.         while (app.pollEvent(WINDOW_CLOSE_EVENT)) if (WINDOW_CLOSE_EVENT.type == Event::Closed) app.close();
  96.         if (_gameOver != true) {
  97.             _checkForInput();
  98.             _updateWorld();
  99.             app.clear(sf::Color::Blue);
  100.             _draw(app);
  101.             app.display();
  102.             Sleep(FRAME_RATE);
  103.         }
  104.     }
  105. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top