Advertisement
Guest User

snek

a guest
Dec 6th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <iostream>
  3. #include <random>
  4. #include <vector>
  5.  
  6. #define WIDTH 800
  7. #define HEIGHT 600
  8.  
  9. sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Snake");
  10.  
  11. void SetRandPos(sf::RectangleShape &shape)
  12. {
  13.     int x = rand()%WIDTH;
  14.     int y = rand()%HEIGHT;
  15.     shape.setPosition({x, y});
  16. }
  17.  
  18. class Snake
  19. {
  20. public:
  21.     sf::RectangleShape head;
  22.     std::vector<sf::RectangleShape> tail;
  23.  
  24.     int eatenCoins = 0;
  25.  
  26.     float speed;
  27.  
  28.     sf::Vector2f defaultSize;
  29.     sf::Vector2f dir = {0, -1}; // default in sus
  30.  
  31. /*
  32.  0, -1 -> sus
  33.  0,  1 -> jos
  34.  1,  0 -> dreapta
  35. -1,  0 -> stanga
  36. */
  37.  
  38.     void ChangeDir()
  39.     {
  40.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))    dir = {0, -1};
  41.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))  dir = {0, 1};
  42.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))  dir = {-1, 0};
  43.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) dir = {1, 0};
  44.     }
  45.  
  46.     void Move()
  47.     {
  48.         head.move(dir * speed);
  49.         for(int i = 0; i<tail.size(); ++i){
  50.  
  51.         }
  52.     }
  53.  
  54.     void AddNewTailPart()
  55.     {
  56.  
  57.     }
  58.  
  59.     void CheckForCoin(sf::RectangleShape &coin)
  60.     {
  61.         if(head.getGlobalBounds().contains(coin.getPosition())){
  62.             ++eatenCoins;
  63.             std::cout << eatenCoins << '\n';
  64.             SetRandPos(coin);
  65.             AddNewTailPart();
  66.         }
  67.     }
  68.  
  69.     void Draw()
  70.     {
  71.         window.draw(head);
  72.         for(auto &i:tail) window.draw(i);
  73.     }
  74.  
  75.     Snake(sf::Vector2f StartPos, sf::Vector2f Size, float Speed)
  76.     {
  77.         defaultSize = Size;
  78.         speed = Speed;
  79.  
  80.         head.setSize(Size);
  81.         head.setFillColor(sf::Color(255, 255, 255));
  82.         head.setPosition(StartPos);
  83.         head.setOrigin(Size.x/2, Size.y/2);
  84.     }
  85. };
  86.  
  87. int main()
  88. {
  89.     srand((time(0)));
  90.     window.setFramerateLimit(60);
  91.  
  92.     Snake snake({WIDTH/2, HEIGHT/2}, {20, 20}, 4);
  93.  
  94.     sf::RectangleShape coin;
  95.     coin.setFillColor(sf::Color::Yellow);
  96.     coin.setSize({20, 20});
  97.     coin.setOrigin(20/2, 20/2);
  98.     SetRandPos(coin);
  99.  
  100.     while(window.isOpen())
  101.     {
  102.         sf::Event e;
  103.         while(window.pollEvent(e)){
  104.             if(e.type == sf::Event::Closed) window.close();
  105.         }
  106.  
  107.         snake.Move();
  108.         snake.ChangeDir();
  109.         snake.CheckForCoin(coin);
  110.  
  111.         window.clear(sf::Color(0, 0, 0));
  112.         window.draw(coin);
  113.         snake.Draw();
  114.         window.display();
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement