Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Snake creation not renders  (Read 6274 times)

0 Members and 1 Guest are viewing this topic.

booster

  • Newbie
  • *
  • Posts: 4
    • View Profile
Snake creation not renders
« on: October 25, 2021, 09:51:53 pm »
Hello to everyone. I'm trying to create step by ste a snake, but i'm struggling with simple body creation. can you explain me why my code does not renders thee rectangle shapes as I want.
Code: [Select]
int main(int argc, char **argv)
{
    sf::RenderWindow window(sf::VideoMode(600, 600), "Snake");
    std::vector<sf::RectangleShape> body(3);


    for (int i = 0; i < 3; i++)
    {
        body.push_back(sf::RectangleShape(sf::Vector2f(100, 100)));
        body[i].setFillColor(sf::Color::Green);
     }
   
    body[0].setPosition(100, 200);
    body[1].setPosition(205, 200);
    body[2].setPosition(310, 200);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();

        window.draw(body[0]);
        window.draw(body[1]);
        window.draw(body[2]);
        window.display();
    }

return 0;
}

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Snake creation not renders
« Reply #1 on: October 25, 2021, 11:22:22 pm »
You've got 6 rectangleshapes. The first 3 have positions but zero size. The last 3 have 100x100 size but no position.

std::vector<sf::RectangleShape> body(3);
This creates 3 rectangleshapes with default properties (0x0 size).

for (int i = 0; i < 3; i++)
    {
        body.push_back(sf::RectangleShape(sf::Vector2f(100, 100)));
        body.setFillColor(sf::Color::Green);
     }
The push_backs create 3 more rectangleshapes with 100x100 size (so there's now 6 total).

If you remove the (3) from the vector line it should work.

booster

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Snake creation not renders
« Reply #2 on: October 26, 2021, 09:28:25 am »
Thank you so musch! :D

 

anything