First, this is not compilable code, simply because you haven't included libraries it depends on. Yes, we all know it requires SFML (Graphics only, I assume), but if you want people to help you, you should make it easier for them, not harder. You adding that line could make the difference between someone helping you and someone giving up.
Imagine it yourself:
Someone says "This is compilable. Help me!"
You paste it and it fails to compile.
It could be enough to just say "Meh! If they're not willing to actually provide compilable code, why should I help them?"
Second, PLEASE include a way to quit through normal channels. e.g. clicking the close on the window should close it.
I had to force-close it because I didn't know that you'd set it up to only close when you press a specific key that you obviously favoured for this task.
Thirdly, I highly recommend not using rand() but using std::random instead. Sure, it can be a little more complicated to set up but it's relatively reliable and unbiased. Saying that, for a test program such as this, rand() can be fine temporarily.
Fourthly, you have no need to "use namespace std". Fully qualifying them (adding std:: before them) is simple, especially since you've already done that with SFML (sf::).
Fifthly, global variables here are unnecessary and they don't help anything. Declare them where they're needed and pass them to functions/classes that need them.
Sixthly (no more "
n-thly" after this one!), the event loop is fully processed
every frame and every frame should also be processed every frame (this should be obvious).
If you want something to happen when an event happens, you should only "start" it happening (or just signal that it should start) in the event loop. Process the "something to happen" outside of that loop.
In a simple project, it could just be a few bools that keep track of the state you want to be in.
I'm now going to look at your code
Why don't you make a std::vector of
houses too, in the similar way you make a vector of stones. Iterating through them (for example, to draw them all), would be able to be done in the same way as gameStones e.g. (for (auto& house : houses)
For that matter, why not use that iteration (
for (auto& stone : gameStones)) elsewhere instead of
for (i = 0; i<10; i++)?
If you want your stones to be moving every frame, they should be updated every frame, not once - whenever you press a key.
Move those updates out of the event loop
If you don't want to allow a stone to be at least a "stoneRadius" away from the edges of the window, you shouldn't be creating them there. e.g. If you create one randomly at (10, 10), how does your code deal with this?
You should read our previous posts. There are important things that you haven't fixed that we told you about.
Fixing some of the things we mentioned (including the thing at the bottom of this post), I managed to get a bunch of circles bouncing around (while holding B - I didn't fix the update-in-event-loop problem).
You are creating this velocity variable and never using it. You likely meant to set the Stone's velocity member variable.
I assume that they think this:
st.move(velocity);
is using it.
You are creating a temporary sf::Vector2f with the same name as the one in the class and assigning x and y to that one.
You are not setting x and y of the velocity in the class.Just so you know, this is
the error that, once fixed, makes everything do what you think it does (assuming that you fixed what we said and read what we wrote about events)
EDIT: I'm sure Jesper would want you to consider looking at this
working example of a bouncing ball on the SFML Wiki.