Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <vector>
- #include <random>
- #include <cmath>
- const int Width = 1080;
- const int Heigth = 720;
- class RainDrop {
- private:
- double _x, _y, _z;
- int ceiling=Heigth;
- double yspeed=0, gravity=0;
- public:
- RainDrop() : _x(0), _y(0), _z(1),gravity(0) {};
- RainDrop(double x, double y, double z, double G = 0) : _x(x), _y(y), _z(z) { SetGravity(G); };
- void Show(sf::RenderWindow & window) {
- using namespace sf;
- RectangleShape Drop;
- Drop.setFillColor(Color::Magenta);
- Drop.setSize(Vector2f(_z,2*_z));
- Drop.setPosition(_x, _y);
- window.draw(Drop);
- }
- bool Fall() {
- if (_y > ceiling) {
- yspeed = 0;
- return 1;
- }
- yspeed -= gravity;
- _y -= yspeed;
- return 0;
- }
- void SetGravity(double Gravity){
- gravity = Gravity * (std::exp(_z/2)) / 100;
- }
- void SetCoordX(double x) {
- _x = x;
- }
- void SetCoordY(double y) {
- _y = y;
- }
- };
- int main()
- {
- sf::RenderWindow window(sf::VideoMode(Width, Heigth), "SFML works!");
- window.setFramerateLimit(60);
- std::random_device rd;
- std::mt19937 mt(rd());
- std::uniform_real_distribution<> RandX(0, Width);
- std::uniform_real_distribution<> RandY(0, -100);
- std::uniform_real_distribution<> RandZ(1,7);
- const int RainDrops = 2000;
- std::vector<RainDrop> Rain(RainDrops);
- for (auto & Drop : Rain) {
- Drop = RainDrop(RandX(mt),RandY(mt),RandZ(mt),0.1);
- }
- while (window.isOpen())
- {
- sf::Event event;
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- window.close();
- }
- window.clear();
- for (auto & Drop : Rain) {
- if (Drop.Fall()) {
- Drop.SetCoordX(RandX(mt));
- Drop.SetCoordY(RandY(mt));
- }
- Drop.Show(window);
- }
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement