Advertisement
Timtsa

Untitled

Jan 22nd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #define OLC_PGE_APPLICATION
  2. #include "olcPixelGameEngine.h"
  3. #include <iostream>
  4. #include <vector>
  5. #include <algorithm>
  6. struct Particle
  7. {
  8.     float x, y;
  9.     float dx, dy;
  10.  
  11.     Particle(float ix, float iy, float idx, float idy) : x(ix), y(iy), dx(idx), dy(idy)
  12.     {};
  13. };
  14. class GameExample : public olc::PixelGameEngine
  15.  
  16. {
  17. public:
  18.     std::vector <Particle> particles;
  19.     static const int N = 150;
  20.     static const int SPEED = 150;
  21.     GameExample()
  22.     {
  23.         sAppName = "GameExample";
  24.        
  25.     }
  26.  
  27.     bool OnUserCreate() override
  28.     {
  29.         for (int i=0;i<N;i++)
  30.         {
  31.             particles.push_back({ ScreenWidth() / 2.0f,ScreenHeight() / 2.0f,rand() % SPEED -SPEED / 2.0f,-rand() % SPEED*1.0f  });
  32.         }
  33.         return true;
  34.     }
  35.  
  36.     bool OnUserUpdate(float fElapsedTime) override
  37.     {
  38.         int count = std::count_if (particles.begin(), particles.end(), [this](Particle p) {return (p.x<0 || p.y<0 || p.x>ScreenWidth() || p.y>ScreenHeight()); });
  39.         particles.erase(std::remove_if(particles.begin(), particles.end(), [this](Particle p) {return (p.x<0 || p.y<0 || p.x>ScreenWidth() || p.y>ScreenHeight()); }), particles.end());
  40.         FillRect(0, 0, ScreenWidth(), ScreenHeight(), olc::BLACK);
  41.         std::cout << count << std::endl;
  42.            
  43.         for (auto p:particles)
  44.         {
  45.             Draw(p.x, p.y, olc::RED);
  46.         }
  47.         for (auto &p : particles)
  48.         {
  49.            
  50.             p.x += p.dx;
  51.             p.y += p.dy;
  52.             p.dy += 0.1;
  53.         };
  54.         for (int i = 0; i< count; i++)
  55.         {
  56.             particles.push_back({ ScreenWidth() / 2.0f,ScreenHeight() / 2.0f,rand() % SPEED - SPEED / 2.0f,-rand() % SPEED*1.0f });
  57.         }
  58.  
  59.         return true;
  60.     }
  61.  
  62. };
  63.  
  64.  
  65. int main()
  66. {
  67.     GameExample game;
  68.     if (game.Construct(800, 600, 1, 1))
  69.     {
  70.         game.Start();
  71.     }
  72.  
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement