Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <time.h>
- using namespace sf;
- int N = 30, M = 20; //N - длина, M - ширина
- int ts = 25; //Размер каждой плитки
- int dir = 2, num = 4; //переменная dir отвечает за поворот, переменная num отвечает за длину змейки
- bool game = true; //переменная game отвечает за режима игры
- struct Snake {
- int x, y;
- } s[600];
- struct Fruct
- {
- int x, y;
- } f;
- int score = 0;
- int lives = 3;
- void Tick(sf::Text& scoreText,sf::Text & livesText) {
- for (int i = num; i > 0; i--) {
- s[i].x = s[i - 1].x;
- s[i].y = s[i - 1].y;
- }
- if (dir == 0)
- s[0].y += 1; //вниз
- if (dir == 1)
- s[0].x -= 1; //влево
- if (dir == 2)
- s[0].x += 1; //право
- if (dir == 3)
- s[0].y -= 1; //вверх
- if (s[0].x > N)
- s[0].x = 0;
- if (s[0].x < 0)
- s[0].x = N;
- if (s[0].y > M)
- s[0].y = 0;
- if (s[0].y < 0)
- s[0].y = M;
- if((s[0].x == f.x) && (s[0].y == f.y)) {
- num++;
- score++;
- f.x = rand() % N;
- f.y = rand() % M;
- scoreText.setString("Score: " + std::to_string(score));
- }
- for (int i = 1; i < num; i++) {
- if ((s[0].x == s[i].x) && (s[0].y == s[i].y)) {
- lives--;
- if (lives <= 0) {
- game = false;
- }
- livesText.setString("Lives: " + std::to_string(lives)); // Обновите текст жизней после уменьшения их количества
- }
- }
- for (int i = 1; i < num; i++)
- if ((s[0].x == s[i].x) && (s[0].y == s[i].y))
- game = false;
- }
- int main()
- {
- srand(time(0));
- RenderWindow window(VideoMode(ts * N, ts * M), "Snake!");
- Font font;
- font.loadFromFile("D:/Snake/Paint/sans.ttf");
- Text scoreText;
- scoreText.setFont(font);
- scoreText.setCharacterSize(30);
- scoreText.setFillColor(Color::Red);
- scoreText.setString("Score: " + std::to_string(score));
- scoreText.setPosition(10, 10);
- Text livesText;
- livesText.setFont(font);
- livesText.setCharacterSize(30);
- livesText.setFillColor(Color::Red);
- livesText.setString("Lives: " + std::to_string(lives));
- livesText.setPosition(10, 40);
- Texture t;
- t.loadFromFile("D:/Snake/Paint/tiles.png");
- Sprite tiles(t);
- Texture sn;
- sn.loadFromFile("D:/Snake/Paint/snake.png");
- Sprite snake(sn);
- Texture ap;
- ap.loadFromFile("D:/Snake/Paint/apple.png");
- Sprite apple(ap);
- Texture go;
- go.loadFromFile("D:/Snake/Paint/gameover.png");
- Sprite gameover(go);
- gameover.setPosition(0, 175);
- f.x = 10;
- f.y = 10;
- Clock clock;
- float timer = 0, delay = 0.1;
- while (window.isOpen())
- {
- float time = clock.getElapsedTime().asSeconds();
- clock.restart();
- timer += time;
- Event event;
- while (window.pollEvent(event))
- {
- if (event.type == Event::Closed)
- window.close();
- }
- while (!game && window.isOpen()) {
- Event event;
- while (window.pollEvent(event)) {
- if (event.type == Event::Closed)
- window.close();
- if (event.type == Event::KeyPressed && event.key.code == Keyboard::R) {
- game = true;
- num = 4;
- score = 0;
- dir = 2;
- s[0].x = 10;
- s[0].y = 10;
- f.x = rand() % N;
- f.y = rand() % M;
- scoreText.setString("Score: " + std::to_string(score)); // Обновите текст счета после сброса счета
- }
- }
- // Draw game over message on top of previously drawn objects
- window.draw(gameover);
- window.display();
- }
- if (Keyboard::isKeyPressed(Keyboard::Left) && dir != 2) // Если стрелка влево нажата и змейка двигается не вправо
- dir = 1;
- if (Keyboard::isKeyPressed(Keyboard::Right) && dir != 1) // Если стрелка вправо нажата и змейка двигается не влево
- dir = 2;
- if (Keyboard::isKeyPressed(Keyboard::Up) && dir != 0) // Если стрелка вверх нажата и змейка двигается не вниз
- dir = 3;
- if (Keyboard::isKeyPressed(Keyboard::Down) && dir != 3) // Если стрелка вниз нажата и змейка двигается не вверх
- dir = 0;
- if (timer > delay && game) {
- timer = 0;
- Tick(scoreText, livesText);
- }
- window.clear();
- for (int i = 0; i < N; i++)
- for (int j = 0; j < M; j++) {
- tiles.setPosition(i * ts, j * ts);
- window.draw(tiles);
- }
- for (int i = 0; i < num; i++) {
- if (i != 0)
- snake.setTextureRect(IntRect(0, 0, ts, ts));
- else
- snake.setTextureRect(IntRect(dir * ts, ts, ts, ts));
- if (!game && i == 1)
- snake.setTextureRect(IntRect(dir * ts, ts * 2, ts, ts));
- snake.setPosition(s[i].x * ts, s[i].y * ts);
- window.draw(snake);
- }
- apple.setPosition(f.x * ts, f.y * ts);
- window.draw(apple);
- if (!game)
- window.draw(gameover);
- window.draw(scoreText);
- window.draw(livesText);
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement