Advertisement
Guest User

Matrix Rain

a guest
Jul 9th, 2019
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. #include "olcConsoleGameEngine.h"
  6.  
  7.  
  8. class OneLoneCoder_Matrix : public olcConsoleGameEngine
  9. {
  10. public:
  11.     OneLoneCoder_Matrix()
  12.     {
  13.         m_sAppName = L"Matrix";
  14.     }
  15.  
  16. private:
  17.     struct sStreamer
  18.     {
  19.         int nColumn = 0;
  20.         float fPosition = 0;
  21.         float fSpeed = 0;
  22.         wstring sText;
  23.     };
  24.  
  25.     list<sStreamer> listStreamers;
  26.  
  27.     int nMaxStreamers = 300;
  28.  
  29.     wchar_t RandomCharacter()
  30.     {
  31.         return (wchar_t)(rand() % 0x1EF + 0x00C0);
  32.         return (wchar_t)(rand() % 93 + 33); // Random ASCII
  33.     }
  34.  
  35.     void PrepareStreamer(sStreamer * s)
  36.     {
  37.         s->nColumn = rand() % ScreenWidth();
  38.         s->fPosition = 0;
  39.         s->fSpeed = rand() % 40 + 5;
  40.         s->sText.clear();
  41.  
  42.         int nStreamerLength = rand() % 80 + 10;
  43.         for (int i = 0; i < nStreamerLength; i++)
  44.             s->sText.append(1, RandomCharacter());
  45.     }
  46.  
  47.  
  48. protected:
  49.     virtual bool OnUserCreate()
  50.     {
  51.         for (int n = 0; n < nMaxStreamers; n++)
  52.         {
  53.             sStreamer s;
  54.             PrepareStreamer(&s);
  55.             listStreamers.push_back(s);
  56.         }
  57.         return true;
  58.     }
  59.  
  60.     virtual bool OnUserUpdate(float fElapsedTime)
  61.     {
  62.        
  63.         Fill(0, 0, ScreenWidth(), ScreenHeight(), PIXEL_SOLID, 0);
  64.  
  65.         for (auto& s : listStreamers)
  66.         {
  67.             s.fPosition += fElapsedTime * s.fSpeed;
  68.             for (int i = 0; i < s.sText.size(); i++)
  69.             {
  70.                
  71.                 short col = s.fSpeed < 15.0f ? FG_DARK_GREEN : FG_GREEN; // ;-)
  72.                 if (i == 0)
  73.                     col = FG_WHITE;
  74.                 else
  75.                     if (i <= 3)
  76.                         col = FG_GREY;
  77.  
  78.                 int nCharIndex = (i - (int)s.fPosition) % s.sText.size();
  79.  
  80.                 Draw(s.nColumn, (int)s.fPosition - i, s.sText[nCharIndex], col);
  81.            
  82.                 if (rand() % 1000 < 5)
  83.                     s.sText[i] = RandomCharacter();
  84.             }
  85.  
  86.             if (s.fPosition - s.sText.size() >= ScreenHeight())
  87.                 PrepareStreamer(&s);
  88.  
  89.         }
  90.         return true;
  91.     }
  92. };
  93.  
  94.  
  95. int main()
  96. {
  97.     OneLoneCoder_Matrix game;
  98.     game.ConstructConsole(128, 80, 12, 12);
  99.     game.Start();
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement