-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathascii_cityscape.cpp
More file actions
100 lines (67 loc) · 1.75 KB
/
Copy pathascii_cityscape.cpp
File metadata and controls
100 lines (67 loc) · 1.75 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
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
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <thread>
#include <chrono>
using namespace std;
const int WIDTH = 120;
const int HEIGHT = 40;
struct Building {
int x;
int width;
int height;
};
void clear_screen() {
cout << "\x1b[2J";
}
void move_cursor_home() {
cout << "\x1b[H";
}
int rand_range(int a, int b) {
return a + rand() % (b - a + 1);
}
int main() {
srand(time(nullptr));
vector<Building> buildings;
int x = 0;
while (x < WIDTH * 2) {
Building b;
b.x = x;
b.width = rand_range(5, 12);
b.height = rand_range(10, HEIGHT - 5);
buildings.push_back(b);
x += b.width + rand_range(2, 6);
}
int offset = 0;
clear_screen();
while (true) {
move_cursor_home();
vector<string> screen(HEIGHT, string(WIDTH, ' '));
for (auto &b : buildings) {
int bx = b.x - offset;
for (int x = 0; x < b.width; x++) {
int screen_x = bx + x;
if (screen_x < 0 || screen_x >= WIDTH)
continue;
for (int y = 0; y < b.height; y++) {
int screen_y = HEIGHT - 1 - y;
if (screen_y < 0)
continue;
char c = '#';
if (y % 3 == 1 && x % 3 == 1) {
c = (rand() % 4 == 0) ? '*' : ' ';
}
screen[screen_y][screen_x] = c;
}
}
}
for (auto &row : screen)
cout << row << "\n";
offset++;
if (offset > WIDTH)
offset = 0;
this_thread::sleep_for(chrono::milliseconds(120));
}
return 0;
}