Advertisement
Guest User

Untitled

a guest
Aug 14th, 2017
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <iostream>
  3.  
  4. //C++17 has std::clamp
  5. int clamp(int n, int min, int max) {
  6. return std::max(min, std::min(n, max));
  7. }
  8.  
  9. struct pos {
  10. int x;
  11. int y;
  12. };
  13.  
  14. const int WIDTH = 1200;
  15. const int HEIGHT = 600;
  16. const int TILE_SIZE = WIDTH / 20; // 40 tiles in a row
  17. const int TILE_HEIGHT = HEIGHT / 10; // 20 tiles in a column
  18.  
  19. class Snake : public sf::Drawable {
  20. public:
  21. Snake() {
  22.  
  23. }
  24. /**
  25. Adds a RectangleShape to our snake
  26. @param RectangleShape to add to the snake.
  27. **/
  28. void add(sf::RectangleShape& point) {
  29. body.push_back(point);
  30. pos pos;
  31. pos.x = 1;
  32. pos.y = 0;
  33. positions.push_back(pos);
  34.  
  35. }
  36.  
  37. void update(std::vector<sf::RectangleShape>& food) {
  38. std::cout << body.size();
  39. for(int i = body.size() - 1; i > 0; i--) {
  40. checkBounds(body.at(i));
  41. body.at(i).setPosition(body.at(i - 1).getPosition());
  42. if(body.at(i).getGlobalBounds().intersects(food.front().getGlobalBounds())) {
  43. grow(food);
  44. }
  45.  
  46. }
  47. checkBounds(body.at(0));
  48. body.at(0).move(positions.front().x * TILE_SIZE, positions.front().y * TILE_HEIGHT);
  49.  
  50. if(body.at(0).getGlobalBounds().intersects(food.front().getGlobalBounds())) {
  51. grow(food);
  52. }
  53. }
  54.  
  55. void rotate(int x, int y) {
  56. if(-x == positions.front().x && -y == positions.front().y) return;
  57. positions.front().x = x;
  58. positions.front().y = y;
  59. }
  60.  
  61. private:
  62.  
  63. /**
  64. Checks if the RectangleShape is within the screen, if not then it places it at the opposite side.
  65. @param RectangleShape to check.
  66. **/
  67. void checkBounds(sf::RectangleShape& point) {
  68. if(point.getPosition().x < 0) point.move(WIDTH, 0);
  69. if(point.getPosition().y < 0) point.move(0, HEIGHT);
  70. if(point.getPosition().x > WIDTH) point.move(-WIDTH, 0);
  71. if(point.getPosition().y > HEIGHT) point.move(0, -HEIGHT);
  72. }
  73.  
  74. /// Default SFML draw function.
  75. virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const {
  76. for(int i = body.size(); i > 0; i--) {
  77. target.draw(body.at(i - 1));
  78. }
  79. }
  80.  
  81. void grow(std::vector<sf::RectangleShape>& food) {
  82. std::cout << "FOOD" << std::endl;
  83. sf::RectangleShape snakeBody;
  84. snakeBody.setFillColor(sf::Color(144,238,144));
  85. snakeBody.setSize(sf::Vector2f(TILE_SIZE, TILE_HEIGHT));
  86. snakeBody.setPosition(body.back().getPosition().x - positions.front().x * TILE_SIZE,
  87. body.back().getPosition().y - positions.front().y * TILE_HEIGHT);
  88.  
  89. body.push_back(snakeBody);
  90. food.pop_back();
  91. }
  92.  
  93. std::vector<sf::RectangleShape> body;
  94. std::vector<pos> positions;
  95.  
  96. sf::Sprite m_sprite;
  97. sf::Texture m_texture;
  98. sf::VertexArray m_vertices;
  99. };
  100.  
  101. void spawnFood(std::vector<sf::RectangleShape>& f);
  102.  
  103. int main() {
  104. srand(time( NULL ));
  105.  
  106. sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Snake");
  107. window.setFramerateLimit(15);
  108.  
  109. sf::Clock clock;
  110. float lastTime = 0;
  111.  
  112. sf::RectangleShape background;
  113. background.setFillColor(sf::Color::White);
  114. background.setPosition(0, 0);
  115. background.setSize(sf::Vector2f(WIDTH, HEIGHT));
  116.  
  117. Snake snake;
  118.  
  119. sf::RectangleShape snakeHead;
  120. snakeHead.setFillColor(sf::Color::Green);
  121. snakeHead.setSize(sf::Vector2f(TILE_SIZE, TILE_HEIGHT));
  122. snake.add(snakeHead);
  123.  
  124. sf::RectangleShape snakeBody;
  125. snakeBody.setFillColor(sf::Color(144,238,144));
  126. snakeBody.setSize(sf::Vector2f(TILE_SIZE, TILE_HEIGHT));
  127.  
  128. std::vector<sf::RectangleShape> food;
  129.  
  130. sf::RectangleShape foodPiece;
  131. foodPiece.setFillColor(sf::Color(139,0,0));
  132. foodPiece.setSize(sf::Vector2f(TILE_SIZE, TILE_HEIGHT));
  133.  
  134. while (window.isOpen()) {
  135. sf::Event event;
  136. while (window.pollEvent(event)) {
  137. if (event.type == sf::Event::Closed) {
  138. window.close();
  139. } else if(event.type == sf::Event::KeyPressed) {
  140. switch(event.key.code) {
  141. case sf::Keyboard::W:
  142. snake.rotate(0, -1);
  143. break;
  144. case sf::Keyboard::A:
  145. snake.rotate(-1, 0);
  146. break;
  147. case sf::Keyboard::S:
  148. snake.rotate(0, 1);
  149. break;
  150. case sf::Keyboard::D:
  151. snake.rotate(1, 0);
  152. break;
  153. default:
  154. break;
  155. }
  156. }
  157. }
  158.  
  159. window.clear();
  160.  
  161. float currentTime = clock.restart().asSeconds();
  162. float fps = 1.f / lastTime;
  163. float timeDelta = currentTime - lastTime;
  164. lastTime = currentTime;
  165.  
  166. if(food.empty()) {
  167. spawnFood(food);
  168. }
  169.  
  170. snake.update(food);
  171.  
  172. window.draw(background);
  173.  
  174. for(sf::RectangleShape& elem: food) {
  175. window.draw(elem);
  176. }
  177.  
  178. window.draw(snake);
  179.  
  180. window.display();
  181. }
  182.  
  183. return 0;
  184. }
  185.  
  186. /**
  187. Generates a food at a random position (aligned).
  188. @param The food vector which we add a new food to.
  189. **/
  190. void spawnFood(std::vector<sf::RectangleShape>& food) {
  191. int x = ((rand() % WIDTH) / 60) * 60;
  192. int y = ((rand() % HEIGHT) / 60) * 60;
  193. sf::RectangleShape foodPiece;
  194. foodPiece.setFillColor(sf::Color(139,0,0));
  195. foodPiece.setSize(sf::Vector2f(TILE_SIZE, TILE_HEIGHT));
  196. foodPiece.setPosition(x, y);
  197. food.push_back(foodPiece);
  198. std::cout << "X: " << x << " Y: " << y << std::endl;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement