Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define OLC_PGE_APPLICATION
- #include "olcPixelGameEngine.h"
- #include <iostream>
- #include <vector>
- #include <algorithm>
- struct Particle
- {
- float x, y;
- float dx, dy;
- Particle(float ix, float iy, float idx, float idy) : x(ix), y(iy), dx(idx), dy(idy)
- {};
- };
- class GameExample : public olc::PixelGameEngine
- {
- public:
- std::vector <Particle> particles;
- static const int N = 150;
- static const int SPEED = 150;
- GameExample()
- {
- sAppName = "GameExample";
- }
- bool OnUserCreate() override
- {
- for (int i=0;i<N;i++)
- {
- particles.push_back({ ScreenWidth() / 2.0f,ScreenHeight() / 2.0f,rand() % SPEED -SPEED / 2.0f,-rand() % SPEED*1.0f });
- }
- return true;
- }
- bool OnUserUpdate(float fElapsedTime) override
- {
- 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()); });
- 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());
- FillRect(0, 0, ScreenWidth(), ScreenHeight(), olc::BLACK);
- std::cout << count << std::endl;
- for (auto p:particles)
- {
- Draw(p.x, p.y, olc::RED);
- }
- for (auto &p : particles)
- {
- p.x += p.dx;
- p.y += p.dy;
- p.dy += 0.1;
- };
- for (int i = 0; i< count; i++)
- {
- particles.push_back({ ScreenWidth() / 2.0f,ScreenHeight() / 2.0f,rand() % SPEED - SPEED / 2.0f,-rand() % SPEED*1.0f });
- }
- return true;
- }
- };
- int main()
- {
- GameExample game;
- if (game.Construct(800, 600, 1, 1))
- {
- game.Start();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement