-
Sonar Systems adminLikes 0Problem Description
How to generate random colours and implement them into SFML
Solution Description#include <SFML/Graphics.hpp> #include <stdlib.h> /* srand, rand */ #include <time.h> /* time */ int main( ) { // needed for better random generation srand (time(NULL)); sf::RenderWindow window( sf::VideoMode( 640, 480 ), "Awesome Game" ); sf::RectangleShape rectangles[10]; for (int i = 0; i < 10; i++) { rectangles[i].setSize( sf::Vector2f( 50.0f, 50.0f ) ); int r = rand() % 255; int g = rand() % 255; int b = rand() % 255; rectangles[i].setFillColor( sf::Color( r, g, b ) ); rectangles[i].setPosition( i * 60.0f, 0.0f ); } while ( window.isOpen( ) ) { sf::Event event; // handle all events while ( window.pollEvent( event ) ) { switch ( event.type ) { case sf::Event::Closed: window.close( ); break; } } // update the game window.clear( ); // draw objects here for (int i = 0; i < 10; i++) { window.draw( rectangles[i] ); } window.display( ); } }
Login to reply