Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. #include "Game.h"
  2.  
  3. namespace
  4. {
  5. SnakeBody *snakebody;
  6. FoodGenerator *foodgenerator;
  7. sf::Clock *gameclock;
  8. }
  9.  
  10. void Game::Start()
  11. {
  12. if (mGameState != UNINITIALIZED)
  13. return;
  14.  
  15. mMainWindow.create(sf::VideoMode(windowparameters::RESOLUTION_X, windowparameters::RESOLUTION_Y, windowparameters::COLOR_DEPTH), "Snake!");
  16. mGameState = SHOWING_MENU;
  17.  
  18. while (mGameState != EXITING)
  19. GameLoop();
  20.  
  21. mMainWindow.close();
  22. }
  23.  
  24. void Game::ShowMenuScreen()
  25. {
  26. MainMenu menuScreen;
  27. MainMenu::MenuResult result = menuScreen.Show(mMainWindow);
  28.  
  29. switch (result)
  30. {
  31. case MainMenu::Exit:
  32. mGameState = EXITING;
  33. break;
  34.  
  35. case MainMenu::Play:
  36. mGameState = RUNNING;
  37. break;
  38. }
  39. }
  40.  
  41. Game::GameState Game::WaitForEnterOrExit()
  42. {
  43. GameState nextstate = GAMEOVER;
  44. sf::Event currentevent;
  45.  
  46. while (nextstate != EXITING && nextstate != RUNNING)
  47. {
  48. while (mMainWindow.pollEvent(currentevent))
  49. {
  50. if (currentevent.type == sf::Event::EventType::KeyPressed &&
  51. sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
  52. {
  53. nextstate = RUNNING;
  54. }
  55. else if (currentevent.type == sf::Event::EventType::Closed)
  56. {
  57. nextstate = EXITING;
  58. }
  59. }
  60. }
  61.  
  62. return nextstate;
  63. }
  64.  
  65. void Game::InitializeGameElements()
  66. {
  67. snakebody = new SnakeBody();
  68. foodgenerator = new FoodGenerator(windowparameters::RESOLUTION_X, windowparameters::RESOLUTION_Y, windowparameters::UNIT_SPACING);
  69. gameclock = new sf::Clock();
  70. }
  71.  
  72. void Game::CleanupGameElements()
  73. {
  74. delete(gameclock);
  75. delete(snakebody);
  76. delete(foodgenerator);
  77. }
  78.  
  79. void Game::HandleEvents()
  80. {
  81. sf::Event currentevent;
  82.  
  83. while (mMainWindow.pollEvent(currentevent))
  84. {
  85. if (currentevent.type == sf::Event::EventType::KeyPressed)
  86. {
  87. if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
  88. {
  89. snakebody->RedirectHead(SnakeBody::LEFT);
  90. }
  91. else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
  92. {
  93. snakebody->RedirectHead(SnakeBody::RIGHT);
  94. }
  95. else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
  96. {
  97. snakebody->RedirectHead(SnakeBody::UP);
  98. }
  99. else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
  100. {
  101. snakebody->RedirectHead(SnakeBody::DOWN);
  102. }
  103. break;
  104. }
  105. else if (currentevent.type == sf::Event::EventType::Closed)
  106. {
  107. mGameState = EXITING;
  108. mMainWindow.close();
  109. }
  110. }
  111. }
  112.  
  113. void Game::GameTick()
  114. {
  115. // tick scene
  116. if (gameclock->getElapsedTime().asMilliseconds() >= windowparameters::TIC_RATE_IN_MS)
  117. {
  118. // Check Collision with body
  119. if (snakebody->CheckCollision())
  120. mGameState = GAMEOVER;
  121.  
  122. else if (snakebody->CheckEating(foodgenerator->mGraphic))
  123. {
  124. snakebody->IncrementSegments();
  125. foodgenerator->mUneaten = false;
  126.  
  127. std::cout << "SCORE = " << snakebody->mNumSegments << std::endl;
  128. }
  129.  
  130. // update snake
  131. snakebody->UpdateSegments(0, windowparameters::RESOLUTION_X, 0, windowparameters::RESOLUTION_Y);
  132.  
  133. // update food
  134. if (!foodgenerator->mUneaten)
  135. foodgenerator->Generate(snakebody);
  136.  
  137. // reset screen, render, display
  138. mMainWindow.clear(sf::Color(230, 230, 230));
  139.  
  140. mMainWindow.draw(foodgenerator->mGraphic);
  141. snakebody->DrawSegments(mMainWindow);
  142.  
  143. mMainWindow.display();
  144. gameclock->restart();
  145. }
  146. }
  147.  
  148. void Game::GameLoop()
  149. {
  150. while (true)
  151. {
  152. switch (mGameState)
  153. {
  154. case SHOWING_MENU:
  155. ShowMenuScreen();
  156. break;
  157.  
  158. case GAMEOVER:
  159. mGameState = WaitForEnterOrExit();
  160. break;
  161.  
  162. case RUNNING:
  163.  
  164. InitializeGameElements();
  165.  
  166. // run game loop
  167. while (mMainWindow.isOpen() && mGameState == RUNNING)
  168. {
  169. HandleEvents();
  170. GameTick();
  171. }
  172.  
  173. CleanupGameElements();
  174. break;
  175.  
  176. case EXITING:
  177. mMainWindow.close();
  178. break;
  179.  
  180. default:
  181. mMainWindow.close();
  182. break;
  183. }
  184. }
  185. }
  186.  
  187. // Because Game is a static class, the member variables need to be instantiated MANUALLY
  188. Game::GameState Game::mGameState = Game::UNINITIALIZED;
  189. sf::RenderWindow Game::mMainWindow;
  190. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement