#include <SFML/Graphics.hpp>
#include <stdio.h>
#include <iostream>
#include <time.h>
using namespace std;
void inicializar_semilla() {
srand(time(NULL));
}
int aleatorio_mod255() {
int n = 0;
n = (rand() % 254 + 1);
return n;
}
double aleatorio_mod10() {
double n = 0.0;
n = (drand48() * (10.0-1.0));
return n;
}
double aleatorio_mod1000() {
double n = 0.0;
n = (drand48() * (1000.0-500.0));
return n;
}
double aleatorio_mod100() {
double n = 0.0;
n = (drand48() * (100.0-50.0));
return n;
}
int main () {
int colorR = 0, colorG = 0, colorB = 0;
double x1 = 0.0;
double x2 = 0.0;
double y1 = 0.0;
double y2 = 0.0;
double m1 = 0.0;
double m2 = 0.0;
int i;
sf::RenderWindow Screen (sf::VideoMode (800, 600, 32), "Title");
sf::Shape Rect [1500];
float x;
float y;
while(Screen.IsOpened ()){
sf::Event Evento;;
while(Screen.GetEvent (Evento)){
if (Evento.Type == sf::Event::Closed || Evento.Key.Code == sf::Key::Escape)
Screen.Close();
}//End of Evento loop
Screen.Clear();
for (i = 0; i < 1500; i++){
inicializar_semilla();
x1 = aleatorio_mod1000();
x2 = aleatorio_mod1000();
y1 = aleatorio_mod1000();
y2 = aleatorio_mod1000();
colorR = aleatorio_mod255();
colorG = aleatorio_mod255();
colorB = aleatorio_mod255();
printf("%d, %f", colorR,x1);
Rect[i] = sf::Shape::Rectangle (x1,x2,y1,y2, sf::Color(colorR,colorG,colorB));
m1 = aleatorio_mod100();
m2 = aleatorio_mod100();
Screen.Draw (Rect[i]);
Rect[i].Move (m1, m2);
Screen.Display();
sf::Sleep(2.00);
}
} //End of Game Loop
return 0;
}
I was messing with shapes and I got a sqare moving, thats nice, so I said to myself: What about I create multiple rectangles at once and make them move on the screen.
I used two random functions to set the size, color and speed of said rectangles random, I wanted tons of colors on my screen.
Okay, I advanced a bit, now it displays random rectangular shapes of various colors BUT they don't move at all, once the next shape is loading the rest stops moving I think thats the problem. Is there any way for me to make them move permanently?
Thank you
PS. BONUS: just for testing and learning, is there any simple way to make them bounce off on the border of the screen?