Advertisement
Guest User

Untitled

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