Advertisement
Guest User

Untitled

a guest
Mar 27th, 2023
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.81 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <time.h>
  3.  
  4. using namespace sf;
  5.  
  6. int N = 30, M = 20; //N - длина, M - ширина
  7. int ts = 25; //Размер каждой плитки
  8.  
  9. int dir = 2, num = 4; //переменная dir отвечает за поворот, переменная num отвечает за длину змейки
  10.  
  11. bool game = true; //переменная game отвечает за режима игры
  12.  
  13. struct Snake {
  14.     int x, y;
  15. } s[600];
  16.  
  17. struct Fruct
  18. {
  19.     int x, y;
  20. } f;
  21.  
  22. int score = 0;
  23. int lives = 3;
  24.  
  25. void Tick(sf::Text& scoreText,sf::Text & livesText) {
  26.     for (int i = num; i > 0; i--) {
  27.         s[i].x = s[i - 1].x;
  28.         s[i].y = s[i - 1].y;
  29.     }
  30.     if (dir == 0)
  31.         s[0].y += 1; //вниз
  32.     if (dir == 1)
  33.         s[0].x -= 1; //влево
  34.     if (dir == 2)
  35.         s[0].x += 1; //право
  36.     if (dir == 3)
  37.         s[0].y -= 1; //вверх
  38.  
  39.     if (s[0].x > N)
  40.         s[0].x = 0;
  41.     if (s[0].x < 0)
  42.         s[0].x = N;
  43.     if (s[0].y > M)
  44.         s[0].y = 0;
  45.     if (s[0].y < 0)
  46.         s[0].y = M;
  47.  
  48.     if((s[0].x == f.x) && (s[0].y == f.y)) {
  49.         num++;
  50.         score++;
  51.  
  52.         f.x = rand() % N;
  53.         f.y = rand() % M;
  54.         scoreText.setString("Score: " + std::to_string(score));
  55.     }
  56.  
  57.     for (int i = 1; i < num; i++) {
  58.         if ((s[0].x == s[i].x) && (s[0].y == s[i].y)) {
  59.             lives--;
  60.             if (lives <= 0) {
  61.                 game = false;
  62.             }
  63.             livesText.setString("Lives: " + std::to_string(lives));  // Обновите текст жизней после уменьшения их количества
  64.         }
  65.     }
  66.  
  67.     for (int i = 1; i < num; i++)
  68.         if ((s[0].x == s[i].x) && (s[0].y == s[i].y))
  69.             game = false;
  70.  
  71. }
  72.  
  73. int main()
  74. {
  75.     srand(time(0));
  76.  
  77.     RenderWindow window(VideoMode(ts * N, ts * M), "Snake!");
  78.  
  79.     Font font;
  80.     font.loadFromFile("D:/Snake/Paint/sans.ttf");
  81.  
  82.  
  83.     Text scoreText;
  84.     scoreText.setFont(font);
  85.     scoreText.setCharacterSize(30);
  86.     scoreText.setFillColor(Color::Red);
  87.     scoreText.setString("Score: " + std::to_string(score));
  88.     scoreText.setPosition(10, 10);
  89.  
  90.     Text livesText;
  91.     livesText.setFont(font);
  92.     livesText.setCharacterSize(30);
  93.     livesText.setFillColor(Color::Red);
  94.     livesText.setString("Lives: " + std::to_string(lives));
  95.     livesText.setPosition(10, 40);
  96.  
  97.  
  98.     Texture t;
  99.     t.loadFromFile("D:/Snake/Paint/tiles.png");
  100.     Sprite tiles(t);
  101.  
  102.     Texture sn;
  103.     sn.loadFromFile("D:/Snake/Paint/snake.png");
  104.     Sprite snake(sn);
  105.  
  106.     Texture ap;
  107.     ap.loadFromFile("D:/Snake/Paint/apple.png");
  108.     Sprite apple(ap);
  109.  
  110.     Texture go;
  111.     go.loadFromFile("D:/Snake/Paint/gameover.png");
  112.     Sprite gameover(go);
  113.     gameover.setPosition(0, 175);
  114.  
  115.     f.x = 10;
  116.     f.y = 10;
  117.  
  118.     Clock clock;
  119.     float timer = 0, delay = 0.1;
  120.     while (window.isOpen())
  121.     {
  122.         float time = clock.getElapsedTime().asSeconds();
  123.         clock.restart();
  124.         timer += time;
  125.         Event event;
  126.         while (window.pollEvent(event))
  127.         {
  128.             if (event.type == Event::Closed)
  129.                 window.close();
  130.         }
  131.  
  132.         while (!game && window.isOpen()) {
  133.             Event event;
  134.             while (window.pollEvent(event)) {
  135.                 if (event.type == Event::Closed)
  136.                     window.close();
  137.  
  138.                 if (event.type == Event::KeyPressed && event.key.code == Keyboard::R) {
  139.                     game = true;
  140.                     num = 4;
  141.                     score = 0;
  142.                     dir = 2;
  143.                     s[0].x = 10;
  144.                     s[0].y = 10;
  145.                     f.x = rand() % N;
  146.                     f.y = rand() % M;
  147.                     scoreText.setString("Score: " + std::to_string(score)); // Обновите текст счета после сброса счета
  148.                 }
  149.             }
  150.  
  151.             // Draw game over message on top of previously drawn objects
  152.             window.draw(gameover);
  153.             window.display();
  154.         }
  155.  
  156.         if (Keyboard::isKeyPressed(Keyboard::Left) && dir != 2) // Если стрелка влево нажата и змейка двигается не вправо
  157.             dir = 1;
  158.         if (Keyboard::isKeyPressed(Keyboard::Right) && dir != 1) // Если стрелка вправо нажата и змейка двигается не влево
  159.             dir = 2;
  160.         if (Keyboard::isKeyPressed(Keyboard::Up) && dir != 0) // Если стрелка вверх нажата и змейка двигается не вниз
  161.             dir = 3;
  162.         if (Keyboard::isKeyPressed(Keyboard::Down) && dir != 3) // Если стрелка вниз нажата и змейка двигается не вверх
  163.             dir = 0;
  164.  
  165.         if (timer > delay && game) {
  166.             timer = 0;
  167.             Tick(scoreText, livesText);
  168.         }
  169.  
  170.         window.clear();
  171.  
  172.         for (int i = 0; i < N; i++)
  173.             for (int j = 0; j < M; j++) {
  174.                 tiles.setPosition(i * ts, j * ts);
  175.                 window.draw(tiles);
  176.             }
  177.  
  178.         for (int i = 0; i < num; i++) {
  179.             if (i != 0)
  180.                 snake.setTextureRect(IntRect(0, 0, ts, ts));
  181.             else
  182.                 snake.setTextureRect(IntRect(dir * ts, ts, ts, ts));
  183.  
  184.             if (!game && i == 1)
  185.                 snake.setTextureRect(IntRect(dir * ts, ts * 2, ts, ts));
  186.  
  187.             snake.setPosition(s[i].x * ts, s[i].y * ts);
  188.             window.draw(snake);
  189.         }
  190.  
  191.         apple.setPosition(f.x * ts, f.y * ts);
  192.         window.draw(apple);
  193.  
  194.         if (!game)
  195.             window.draw(gameover);
  196.  
  197.         window.draw(scoreText);
  198.         window.draw(livesText);
  199.  
  200.         window.display();
  201.     }
  202.  
  203.     return 0;
  204. }
  205.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement