-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_rain.cpp
More file actions
61 lines (52 loc) · 1.51 KB
/
matrix_rain.cpp
File metadata and controls
61 lines (52 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <thread>
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
void enable_virtual_terminal() {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut == INVALID_HANDLE_VALUE) return;
DWORD dwMode = 0;
if (!GetConsoleMode(hOut, &dwMode)) return;
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hOut, dwMode);
}
#else
void enable_virtual_terminal() {}
#endif
const int WIDTH = 80;
const int HEIGHT = 30;
const char charset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@#$%^&*";
char random_char() {
return charset[rand() % (sizeof(charset) - 1)];
}
void clear_screen() {
std::cout << "\033[2J\033[H";
}
void matrix_rain() {
std::vector<int> positions(WIDTH, 0);
while (true) {
for (int i = 0; i < WIDTH; ++i) {
if (positions[i] < HEIGHT && rand() % 10 < 2)
positions[i]++;
for (int j = 0; j < positions[i]; ++j) {
std::cout << "\033[" << j+1 << ";" << i+1 << "H";
if (j == positions[i] - 1)
std::cout << "\033[1;92m" << random_char();
else
std::cout << "\033[32m" << random_char();
}
}
std::cout << std::flush;
std::this_thread::sleep_for(std::chrono::milliseconds(75));
}
}
int main() {
srand(static_cast<unsigned int>(time(0)));
enable_virtual_terminal();
clear_screen();
matrix_rain();
return 0;
}