Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <SFML/Graphics.hpp>
- using namespace std;
- unsigned int width = 500;
- unsigned int height = 500;
- int main()
- {
- sf::RenderWindow window(sf::VideoMode(width, height), "In progress");
- window.setFramerateLimit(5);
- srand(time(NULL));
- sf::RectangleShape player(sf::Vector2f(50, 50));
- player.setFillColor(sf::Color::Green);
- sf::RectangleShape fruit(sf::Vector2f(50, 50));
- fruit.setFillColor(sf::Color::Red);
- sf::RectangleShape body[100];
- for (int x = 0; x < 100; x++)
- {
- body[x].setSize(sf::Vector2f(50, 50));
- body[x].setFillColor(sf::Color::Magenta);
- }
- int tailX[100];
- int tailY[100];
- int direction = 2;
- int up = 1;
- int right = 2;
- int down = 3;
- int left = 4;
- int playerX = 250;
- int playerY = 250;
- int tailN = -1;
- bool defeat = false;
- int fruitX = 300;
- int fruitY = 400;
- int randomX = 0;
- int randomY = 0;
- sf::Event event;
- while (window.isOpen() && defeat == false)
- {
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- {
- window.close();
- }
- if (event.type == sf::Event::KeyPressed)
- {
- if (event.key.code == sf::Keyboard::Right)
- {
- direction = right;
- }
- else if (event.key.code == sf::Keyboard::Left)
- {
- direction = left;
- }
- else if (event.key.code == sf::Keyboard::Up)
- {
- direction = up;
- }
- else if (event.key.code == sf::Keyboard::Down)
- {
- direction = down;
- }
- else if (event.key.code == sf::Keyboard::Space)
- {
- cout << "The snake grew" << endl;
- tailN++;
- }
- }
- }
- if (tailN >= 0)
- {
- for (int a = tailN; a > 0; a--)
- {
- tailX[a] = tailX[a - 1];
- tailY[a] = tailY[a - 1];
- }
- tailX[0] = playerX;
- tailY[0] = playerY;
- }
- if (direction == right)
- {
- playerX += 50;
- }
- else if (direction == left)
- {
- playerX -= 50;
- }
- else if (direction == up)
- {
- playerY -= 50;
- }
- else if (direction == down)
- {
- playerY += 50;
- }
- window.clear(sf::Color::White); //Clear
- //Check collision with fruit
- if (playerX == fruitX && playerY == fruitY)
- {
- cout << "The snake just ate a fruit" << endl;
- tailN++;
- bool collision_with_body = true;
- bool collision_with_head = true;
- while (collision_with_body == true && collision_with_head == true)
- {
- randomX = rand() % 10;
- randomY = rand() % 10;
- randomX = randomX * 50;
- randomY = randomY * 50;
- if (playerX == randomX && playerY == randomY)
- collision_with_head = true;
- else
- collision_with_head = false;
- for (int u = tailN; u >= 0; u--)
- {
- if (tailX[u] == randomX && tailY[u] == randomY)
- {
- collision_with_body = true;
- break;
- }
- else
- collision_with_body = false;
- }
- }
- fruitX = randomX;
- fruitY = randomY;
- }
- for (int x = 0; x < 500; x += 50)
- {
- for (int y = 0; y < 500; y += 50)
- {
- if (x == playerX && y == playerY)
- {
- player.setPosition(x, y);
- window.draw(player);
- }
- else if (x == fruitX && y == fruitY)
- {
- fruit.setPosition(x, y);
- window.draw(fruit);
- }
- else
- {
- for (int m = tailN; m >= 0; m--)
- {
- if (x == tailX[m] && y == tailY[m])
- {
- body[m].setPosition(x, y);
- window.draw(body[m]);
- }
- }
- }
- }
- }
- //Check defeat
- for (int q = tailN; q >= 0; q--)
- {
- if (playerX == tailX[q] && playerY == tailY[q])
- {
- defeat = true;
- }
- }
- cout << tailN << endl;
- window.display(); //Display
- }
- window.close();
- int pause; cin >> pause;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement