-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.cpp
executable file
·155 lines (140 loc) · 3.9 KB
/
matrix.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <SFML/Graphics.hpp>
#include <SFML/System/Time.hpp>
#include <SFML/System/Clock.hpp>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <vector>
int Width = 800;
int Height = 600;
int fontSize = 17;
sf::RenderWindow window(sf::VideoMode(Width, Height), "Matrix Rain");
sf::Font font;
struct JString {
wchar_t ch;
float x,y;
int len;
std::wstring jstring;
int speed;
JString();
void fall();
void update();
void draw();
void reduce_speed();
void increase_speed();
void random_speed();
};
void JString::reduce_speed()
{
if (speed > 2)
speed--;
}
void JString::increase_speed()
{
if (speed < 15)
speed++;
}
void JString::random_speed()
{
speed = rand() % 5 + 3;
}
int prev_x = 0;
JString::JString()
{
x = prev_x;
prev_x += fontSize;
y = -(rand() % 500);
speed = rand() % 5 + 3;
len = rand() % 15 + 5;
for (int i = 0;i<len;i++)
jstring+=wchar_t(0x30A0 + rand() % 96);
}
void JString::update()
{
int n = rand() % len;
for (int i=0;i<n;i++)
{
int m = rand() % len;
jstring[m] = wchar_t(0x30A0 + rand() % 96);
}
}
void JString::fall()
{
y+=speed;
if (y>Height) {
y=0;-(rand() % 500) - 200;
//speed = rand() % 5 + 4;
}
}
void JString::draw()
{
float h = y;
sf::Text text(L"", font);
text.setCharacterSize(fontSize);
text.setFillColor(sf::Color(0, 255, 150));
for (wchar_t jch : jstring) {
text.setString(jch);
sf::Glyph glyph = font.getGlyph(jch, fontSize, false);
h += glyph.advance;
if (h>=Height) h-=Height;
text.setPosition(x,h);
window.draw(text);
}
}
int main()
{
font.loadFromFile("Osaka.ttf");
window.setFramerateLimit(60);
sf::Clock timer;
srand(time(NULL));
font.loadFromFile("Osaka.ttf");
std::vector<JString> matrix(Width/fontSize);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
window.close();
if (event.type == sf::Event::Resized) {
window.setView(sf::View(sf::FloatRect(0, 0, event.size.width, event.size.height)));
Height = event.size.height;
if (event.size.width < Width) {
for (auto it = matrix.begin();it!=matrix.end();) {
if (it->x > event.size.width)
it = matrix.erase(it);
else
it++;
}
} else if (event.size.width > matrix.back().x+3*fontSize) {
prev_x = matrix.back().x + fontSize;
while (prev_x < event.size.width - fontSize) {
matrix.push_back(JString());
}
}
Width = event.size.width;
}
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::L)
for (auto &js : matrix)
js.reduce_speed();
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::I)
for (auto &js : matrix)
js.increase_speed();
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::R)
for (auto &js : matrix)
js.random_speed();
}
if (timer.getElapsedTime() >= sf::milliseconds(100)) {
for (auto &jstring : matrix)
jstring.update();
timer.restart();
}
window.clear(sf::Color::Black);
for (auto &jstring : matrix) {
jstring.draw();
jstring.fall();
}
window.display();
}
return 0;
}