Advertisement
Guest User

Untitled

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