Skip to content

Instantly share code, notes, and snippets.

@inxanedev
Created September 25, 2020 17:05
Show Gist options
  • Save inxanedev/17a35de9ad781b213b33a5f4fdc06ce8 to your computer and use it in GitHub Desktop.
Save inxanedev/17a35de9ad781b213b33a5f4fdc06ce8 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <ncurses.h> // curses library
#include <vector>
#include <cstdlib> // rand()
#include <ctime> // for initializing random
#include <unistd.h> // usleep()
// Amount of drops to create on the screen
const unsigned int DROP_AMOUNT = 50;
class Raindrop {
public:
int m_PosY, m_PosX, m_PrevY, m_PrevX, m_Velocity;
Raindrop(int y, int x) {
m_PosY = y;
m_PosX = x;
m_Velocity = 1;
}
void update(int maxY, int maxX) {
// Set previous position
m_PrevY = m_PosY;
m_PrevX = m_PosX;
// Update position
m_PosY += m_Velocity;
// if at the bottom of the screen
if (m_PosY >= maxY) {
m_PosY = 0;
m_PosX = rand() % maxX;
}
}
void draw() {
// Draw drop
mvprintw(m_PosY, m_PosX, "|");
// Erase previous drawn drop
mvprintw(m_PrevY, m_PrevX, " ");
}
};
int main() {
initscr();
// Initialize RNG
srand(time(NULL));
// Disable cursor in the terminal
curs_set(false);
int maxy = 0, maxx = 0;
// Get width and height of the terminal
getmaxyx(stdscr, maxy, maxx);
std::vector<Raindrop> drops;
// Add drops to the drop list
for (int i = 0; i < DROP_AMOUNT; i++) {
Raindrop drop(rand() % maxy, rand() % maxx);
drops.push_back(drop);
}
while (true) {
for (int i = 0; i < drops.size(); i++) {
drops[i].update(maxy, maxx);
drops[i].draw();
}
usleep(70000);
refresh();
}
endwin();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment