Hello i have problem with one function in my multi moving circle app.
#include <SFML/Graphics.hpp>
#include <vector>
#include <string>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <string>
#include "Ball.h"
#include "Rectangle.h"
#include <SFML/Graphics/Text.hpp>
#include <ctime>
using namespace sf;
using namespace std;
Font font;
//tablica zawierająca wielkość promieni kul
float radius[49];
// Tablice poczatkwoych wartości kul
float beginningPositionX[49];
float beginningPositionY[49];
//predkosc poruszania sie kuli
float xSpeed[49];
float ySpeed[49];
//tablica liczb losowanych z przedziału 0-49
// Wywołąnie timera to losowania
Clock deltaClock;
Time myEventTimer;
//odbicie kuli od krawędzi prostokata rectangle
int random[6];
void balls_Reflection(vector<Ball> & ball)
{
for (int i = 0; i < 49; i++)
{
if (ball[i].get_Position().x < 620){
int positionY = ball[i].get_Position().y;
if (positionY <= 20){ ySpeed[i] = -ySpeed[i]; }
if (positionY >= 520.0 - 2 * ball[i].get_Radius()){ ySpeed[i] = -ySpeed[i]; }
int positionX = ball[i].get_Position().x;
if (positionX <= 20){ xSpeed[i] = -xSpeed[i]; }
if (positionX >= 620.0 - 2 * ball[i].get_Radius()){ xSpeed[i] = -xSpeed[i]; }
}
}
}
//losownaie kul nie dizała poprawnie
void random_Balls(vector<Ball> & ball, int random[6])
{
if (deltaClock.getElapsedTime() >= seconds(5)){
ball[random[0]].set_Position(650, 40 + ball[random[0]].get_Radius());
ball[random[0]].set_CharacterPosition(ball[random[0]].get_Position().x + ball[random[0]].get_Radius() * 2 / 3, ball[random[0]].get_Position().y + ball[random[0]].get_Radius() / 4);
if (deltaClock.getElapsedTime() >= seconds(8)){
ball[1].set_Position(650, 80 + ball[1].get_Radius() * 2);
ball[1].set_CharacterPosition(ball[1].get_Position().x + ball[1].get_Radius() * 2 / 3, ball[1].get_Position().y + ball[1].get_Radius() / 4);
if (deltaClock.getElapsedTime() >= seconds(11)){
ball[2].set_Position(650, 120 + ball[2].get_Radius() * 2);
ball[2].set_CharacterPosition(ball[2].get_Position().x + ball[2].get_Radius() * 2 / 3, ball[2].get_Position().y + ball[2].get_Radius() / 4);
if (deltaClock.getElapsedTime() >= seconds(14)){
ball[3].set_Position(650, 160 + ball[3].get_Radius() * 2);
ball[3].set_CharacterPosition(ball[3].get_Position().x + ball[3].get_Radius() * 2 / 3, ball[3].get_Position().y + ball[3].get_Radius() / 4);
if (deltaClock.getElapsedTime() >= seconds(17)){
ball[4].set_Position(650, 200 + ball[4].get_Radius() * 2);
ball[4].set_CharacterPosition(ball[4].get_Position().x + ball[4].get_Radius() * 2 / 3, ball[4].get_Position().y + ball[4].get_Radius() / 4);
if (deltaClock.getElapsedTime() >= seconds(20)){
ball[5].set_Position(650, 240 + ball[5].get_Radius() * 2);
ball[5].set_CharacterPosition(ball[5].get_Position().x + ball[5].get_Radius() * 2 / 3, ball[5].get_Position().y + ball[5].get_Radius() / 4);
deltaClock.restart();
}
}
}
}
}
}
}
//Ustawianie wartosci poczatkowych kuli
void settingUp_Balls(vector<Ball> & ball)
{
font.loadFromFile("arial.ttf");
for (int i = 0; i < 49; i++)
{
// Zamiana wartosci liczbowej i na Stringa
std::string s = std::to_string(i + 1);
//losowanie ustwień poszczególnych kul;
//losowanie koloru wyświetlanje kuli
Color color1 = Color(rand() % 255, rand() % 255, rand() % 255, 255);
//losowanie wielkosci promienia kuli
radius[i] = (std::rand() % 40) + 30;
// losownaie pozycji poczatkowej kuli
beginningPositionX[i] = (std::rand() % 450) + 10;
beginningPositionY[i] = (std::rand() % 320) + 10;
ball[i].set_Position(radius[i] + beginningPositionX[i], radius[i] + beginningPositionY[i]);
ball[i].set_Color(color1, Color::Black);
ball[i].set_Radius(radius[i]);
ball[i].set_Font(font);
ball[i].set_String(s);
ball[i].set_CharacterSize(radius[i]);
ball[i].set_CharacterPosition(ball[i].get_Position().x + ball[i].get_Radius() * 2 / 3, ball[i].get_Position().y + ball[i].get_Radius() / 4);
// predkosci poruszania się kuli
ySpeed[i] = (std::rand() % 100) + 10;
xSpeed[i] = (std::rand() % 100) + 10;
}
}
int main()
{
RenderWindow win(VideoMode(800, 600), L"Losowanie");
vector < Ball > ball(49, Ball(win));
Rectangle rectangle(win);
rectangle.set_Position(20, 20);
rectangle.set_Size(600, 500);
// obsługa poruszania się kul
Clock stoper;
srand(time(NULL));
for (int i = 0; i < 7; i++)
{
random[i] = (std::rand() % 49);
}
// wywosanie fukcji, definiujacej ustawienia kuli
settingUp_Balls(ball);
// funkcja nie działa popranie
while (win.isOpen())
{
win.clear(Color::White);
Event e;
while (win.pollEvent(e))
{
if (e.type == Event::Closed)
win.close();
}
//poruszanie sie kul
auto dt = stoper.restart().asSeconds();
for (int i = 0; i < 49; i++){
if (ball[i].get_Position().x < 620)
ball[i].Move(xSpeed[i] * dt, ySpeed[i] * dt);
}
//Rysownaie prostokata masyzny losującej
rectangle.draw_Rectangle();
// Rysowanie kul
for (int i = 0; i < 49; i++)
{
ball[i].draw_Ball();
}
// sprawdzanie odbicie kuli
balls_Reflection(ball);
random_Balls(ball, random);
win.display();
}
}
I have problem with function
void random_Balls(vector<Ball> & ball, int random[6])
{
if (deltaClock.getElapsedTime() >= seconds(5)){
ball[random[0]].set_Position(650, 40 + ball[random[0]].get_Radius());
ball[random[0]].set_CharacterPosition(ball[random[0]].get_Position().x + ball[random[0]].get_Radius() * 2 / 3, ball[random[0]].get_Position().y + ball[random[0]].get_Radius() / 4);
if (deltaClock.getElapsedTime() >= seconds(8)){
ball[1].set_Position(650, 80 + ball[1].get_Radius() * 2);
ball[1].set_CharacterPosition(ball[1].get_Position().x + ball[1].get_Radius() * 2 / 3, ball[1].get_Position().y + ball[1].get_Radius() / 4);
if (deltaClock.getElapsedTime() >= seconds(11)){
ball[2].set_Position(650, 120 + ball[2].get_Radius() * 2);
ball[2].set_CharacterPosition(ball[2].get_Position().x + ball[2].get_Radius() * 2 / 3, ball[2].get_Position().y + ball[2].get_Radius() / 4);
if (deltaClock.getElapsedTime() >= seconds(14)){
ball[3].set_Position(650, 160 + ball[3].get_Radius() * 2);
ball[3].set_CharacterPosition(ball[3].get_Position().x + ball[3].get_Radius() * 2 / 3, ball[3].get_Position().y + ball[3].get_Radius() / 4);
if (deltaClock.getElapsedTime() >= seconds(17)){
ball[4].set_Position(650, 200 + ball[4].get_Radius() * 2);
ball[4].set_CharacterPosition(ball[4].get_Position().x + ball[4].get_Radius() * 2 / 3, ball[4].get_Position().y + ball[4].get_Radius() / 4);
if (deltaClock.getElapsedTime() >= seconds(20)){
ball[5].set_Position(650, 240 + ball[5].get_Radius() * 2);
ball[5].set_CharacterPosition(ball[5].get_Position().x + ball[5].get_Radius() * 2 / 3, ball[5].get_Position().y + ball[5].get_Radius() / 4);
deltaClock.restart();
}
}
}
}
}
}
}
I can't code this is shorter, but in ball[2] visual studio stopped