#draw rectangles at different positions while in game loop

12 messages · Page 1 of 1 (latest)

ebon tusk
#

I think there's a simple solution I'm overlooking, but how can I do it so that the 3 rectangles are at different x positions from each other without getting moved out the screen.

//Game.cpp
void Game::draw_cars(sf::RenderWindow& window)
{
    for (int i = 0; i < state.cars.size(); i++) {
        state.cars[i].draw_vehicles(window);
    }
}

//Car.cpp

void Car::draw_vehicles(sf::RenderWindow& window)
{
    rectangle.setPosition(sf::Vector2f(CAR_POSITION_X + Offset, CAR_POSITION_Y));
    window.draw(rectangle);
}
teal warrenBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

ebon tusk
#

this is source.cpp

int main()
{
    //...

    Game game;

    while (window.isOpen())
    {
        while (const std::optional event = window.pollEvent())
        {
            if (event->is<sf::Event::Closed>())
            {
                window.close();
            }
        }

        window.clear();

        game.draw_cars(window);
        //game.move_cars();

        window.display();
    }
}
dusty belfry
#

What's the issue now?

ebon tusk
#

I want to draw the cars at different x positions and do it in a simple way

#

If I use static variables and add them to the x position they get moved out the screen

dusty belfry
#

Probably Car should have its position as a non-static variable

#

And then when you create the cars, that piece of code decides where they will be

ebon tusk
#

I have car inherit from vehicle, so I forgot to give position_x and position_y values notlikethis

#

you're right, I was doing it in a weird way

ebon tusk
#

I'm getting red squiggly lines for this. Does this look normal, because I think VS is just being weird

Car::Car(int x, int y, int vel_x, int vel_y, int offset) : Vehicle{ x, y, vel_x, vel_y }, offset{ offset }
{

}
ebon tusk
#

!solved