Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <vector>
  3. #include <iostream>
  4. #include <ctime>
  5.  
  6.  
  7. using namespace sf;
  8. using std::vector;
  9.  
  10. class Player
  11. {
  12. private: float x, y;
  13. public: float speed = 0;
  14.         int dir;
  15.         Player()
  16.         {
  17.             Snk.push_back(Snake(11, 4));
  18.             Snk.push_back(Snake(1, 4));
  19.         }
  20.         struct Snake
  21.         {
  22.             RectangleShape SnakeBody;
  23.             int x;
  24.             int y;
  25.  
  26.             Snake(float x_, float y_) :
  27.                 x(x_),
  28.                 y(y_)
  29.             {
  30.                 SnakeBody.setSize(Vector2f(10, 10));
  31.                 SnakeBody.setFillColor(Color::Red);
  32.             }
  33.         };
  34.         vector <Snake> Snk;
  35.  
  36.         void Update(float time)
  37.         {
  38.  
  39.             move();
  40.             speed = 800.f;
  41.             switch (dir)
  42.             {
  43.             case 1: x += speed * time;
  44.                 break;
  45.             case 2: x -= speed * time;
  46.                 break;
  47.             case 3: y += speed * time;
  48.                 break;
  49.             case 4: y -= speed * time;
  50.             }
  51.  
  52.         }
  53.  
  54.         void move()
  55.         {
  56.             if (Keyboard::isKeyPressed(Keyboard::Right)) {
  57.                 dir = 1;
  58.             }
  59.             if (Keyboard::isKeyPressed(Keyboard::Left)) {
  60.                 dir = 2;
  61.             }
  62.             if (Keyboard::isKeyPressed(Keyboard::Down)) {
  63.                 dir = 3;
  64.             }
  65.             if (Keyboard::isKeyPressed(Keyboard::Up)) {
  66.                 dir = 4;
  67.             }
  68.         }
  69.         void Snake_move(RenderWindow & window)
  70.         {
  71.             for (int i = Snk.size() - 1; i > 0; i --) {
  72.                 Snk[i].x = Snk[i - 1].x;
  73.                 Snk[i].y = Snk[i - 1].y;
  74.                 for (int j = 0; j < Snk.size(); j++) {
  75.                     Snk[i].x += 5;
  76.                 }
  77.             }
  78.             Snk[0].x = (int)x / 10;
  79.             Snk[0].y = (int)y / 10;
  80.            
  81.             for (int i = 0; i < Snk.size(); i++) {
  82.                 Snk[i].SnakeBody.setPosition(Snk[i].x, Snk[i].y);
  83.                 window.draw(Snk[i].SnakeBody);
  84.             }
  85.         }
  86. };
  87.  
  88.  
  89. int main()
  90. {
  91.     const int WINDOW_WIDTH = 400;
  92.     const int WINDOW_HEIGHT = 400;
  93.     RenderWindow window(VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), L"Змейка");
  94.  
  95.     srand(time(NULL));
  96.     Clock clock;
  97.  
  98.     Event event;
  99.     Player player;
  100.  
  101.     while (window.isOpen()) {
  102.  
  103.         float time = clock.getElapsedTime().asMilliseconds() / 1000.f;
  104.         clock.restart();
  105.  
  106.         player.Update(time);
  107.  
  108.         while (window.pollEvent(event)) {
  109.             if (event.type == Event::Closed) {
  110.                 window.close();
  111.             }
  112.         }
  113.         window.clear(Color::Green);
  114.         player.Snake_move(window);
  115.         window.display();
  116.     }
  117.  
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement