Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <iostream>
- //C++17 has std::clamp
- int clamp(int n, int min, int max) {
- return std::max(min, std::min(n, max));
- }
- struct pos {
- int x;
- int y;
- };
- const int WIDTH = 1200;
- const int HEIGHT = 600;
- const int TILE_SIZE = WIDTH / 20; // 40 tiles in a row
- const int TILE_HEIGHT = HEIGHT / 10; // 20 tiles in a column
- class Snake : public sf::Drawable {
- public:
- Snake() {
- }
- /**
- Adds a RectangleShape to our snake
- @param RectangleShape to add to the snake.
- **/
- void add(sf::RectangleShape& point) {
- body.push_back(point);
- pos pos;
- pos.x = 1;
- pos.y = 0;
- positions.push_back(pos);
- }
- void update(std::vector<sf::RectangleShape>& food) {
- std::cout << body.size();
- for(int i = body.size() - 1; i > 0; i--) {
- checkBounds(body.at(i));
- body.at(i).setPosition(body.at(i - 1).getPosition());
- if(body.at(i).getGlobalBounds().intersects(food.front().getGlobalBounds())) {
- grow(food);
- }
- }
- checkBounds(body.at(0));
- body.at(0).move(positions.front().x * TILE_SIZE, positions.front().y * TILE_HEIGHT);
- if(body.at(0).getGlobalBounds().intersects(food.front().getGlobalBounds())) {
- grow(food);
- }
- }
- void rotate(int x, int y) {
- if(-x == positions.front().x && -y == positions.front().y) return;
- positions.front().x = x;
- positions.front().y = y;
- }
- private:
- /**
- Checks if the RectangleShape is within the screen, if not then it places it at the opposite side.
- @param RectangleShape to check.
- **/
- void checkBounds(sf::RectangleShape& point) {
- if(point.getPosition().x < 0) point.move(WIDTH, 0);
- if(point.getPosition().y < 0) point.move(0, HEIGHT);
- if(point.getPosition().x > WIDTH) point.move(-WIDTH, 0);
- if(point.getPosition().y > HEIGHT) point.move(0, -HEIGHT);
- }
- /// Default SFML draw function.
- virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const {
- for(int i = body.size(); i > 0; i--) {
- target.draw(body.at(i - 1));
- }
- }
- void grow(std::vector<sf::RectangleShape>& food) {
- std::cout << "FOOD" << std::endl;
- sf::RectangleShape snakeBody;
- snakeBody.setFillColor(sf::Color(144,238,144));
- snakeBody.setSize(sf::Vector2f(TILE_SIZE, TILE_HEIGHT));
- snakeBody.setPosition(body.back().getPosition().x - positions.front().x * TILE_SIZE,
- body.back().getPosition().y - positions.front().y * TILE_HEIGHT);
- body.push_back(snakeBody);
- food.pop_back();
- }
- std::vector<sf::RectangleShape> body;
- std::vector<pos> positions;
- sf::Sprite m_sprite;
- sf::Texture m_texture;
- sf::VertexArray m_vertices;
- };
- void spawnFood(std::vector<sf::RectangleShape>& f);
- int main() {
- srand(time( NULL ));
- sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Snake");
- window.setFramerateLimit(15);
- sf::Clock clock;
- float lastTime = 0;
- sf::RectangleShape background;
- background.setFillColor(sf::Color::White);
- background.setPosition(0, 0);
- background.setSize(sf::Vector2f(WIDTH, HEIGHT));
- Snake snake;
- sf::RectangleShape snakeHead;
- snakeHead.setFillColor(sf::Color::Green);
- snakeHead.setSize(sf::Vector2f(TILE_SIZE, TILE_HEIGHT));
- snake.add(snakeHead);
- sf::RectangleShape snakeBody;
- snakeBody.setFillColor(sf::Color(144,238,144));
- snakeBody.setSize(sf::Vector2f(TILE_SIZE, TILE_HEIGHT));
- std::vector<sf::RectangleShape> food;
- sf::RectangleShape foodPiece;
- foodPiece.setFillColor(sf::Color(139,0,0));
- foodPiece.setSize(sf::Vector2f(TILE_SIZE, TILE_HEIGHT));
- while (window.isOpen()) {
- sf::Event event;
- while (window.pollEvent(event)) {
- if (event.type == sf::Event::Closed) {
- window.close();
- } else if(event.type == sf::Event::KeyPressed) {
- switch(event.key.code) {
- case sf::Keyboard::W:
- snake.rotate(0, -1);
- break;
- case sf::Keyboard::A:
- snake.rotate(-1, 0);
- break;
- case sf::Keyboard::S:
- snake.rotate(0, 1);
- break;
- case sf::Keyboard::D:
- snake.rotate(1, 0);
- break;
- default:
- break;
- }
- }
- }
- window.clear();
- float currentTime = clock.restart().asSeconds();
- float fps = 1.f / lastTime;
- float timeDelta = currentTime - lastTime;
- lastTime = currentTime;
- if(food.empty()) {
- spawnFood(food);
- }
- snake.update(food);
- window.draw(background);
- for(sf::RectangleShape& elem: food) {
- window.draw(elem);
- }
- window.draw(snake);
- window.display();
- }
- return 0;
- }
- /**
- Generates a food at a random position (aligned).
- @param The food vector which we add a new food to.
- **/
- void spawnFood(std::vector<sf::RectangleShape>& food) {
- int x = ((rand() % WIDTH) / 60) * 60;
- int y = ((rand() % HEIGHT) / 60) * 60;
- sf::RectangleShape foodPiece;
- foodPiece.setFillColor(sf::Color(139,0,0));
- foodPiece.setSize(sf::Vector2f(TILE_SIZE, TILE_HEIGHT));
- foodPiece.setPosition(x, y);
- food.push_back(foodPiece);
- std::cout << "X: " << x << " Y: " << y << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement