Advertisement
Guest User

Untitled

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