我正在托盘模拟Matrix
电影的背景,我编写了以下代码:
#include <iostream>
#include<windows.h>
#include <conio.h>
using namespace std;
void CursorPosition(short x, short y) {
COORD coordScreen = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coordScreen);
}
void color(int nb) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), nb);
}
int main(int argc, char *argv[]) {
unsigned int y[79];
unsigned int ymin[79];
unsigned int a, b;
char i[36] = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 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};
system("title The Matrix !");
color(10);
for (a = 0; a < 79; a++) {
y[a] = 0;
ymin[a] = 1;
}
do {
for (unsigned int x = 0; x < 79; x++) {
for (unsigned int j = 0; j < 5; j++) {
a = rand() % 79;
if (y[a] != 0) {
b = rand() % y[a] + ymin[a];
if (b < 26) {
if (b < ymin[a] + 2 & b > 2) color(2);
CursorPosition(a + 1, b);
cout << i[rand() % 36];
color(10);
}
}
}
if (y[x] != 0 || (rand() % 50) == 0) {
if ((rand() % (y[x] + 1)) < 20 & ymin[x] == 1) {
y[x]++;
} else {
CursorPosition(x + 1, ymin[x]);
cout << " ";
ymin[x]++;
if (ymin[x] < 25) {
color(2);
CursorPosition(x + 1, ymin[x] + 1);
cout << i[rand() % 36];
color(10);
}
}
if ((y[x] + ymin[x]) < 26) {
CursorPosition(x + 1, y[x] + ymin[x]);
color(15);
cout << i[rand() % 36];
color(10);
}
if ((y[x] + ymin[x]) < 27) {
CursorPosition(x + 1, y[x] + ymin[x] - 1);
cout << i[rand() % 36];
}
}
if (ymin[x] > 25) {
ymin[x] = 1;
y[x] = 0;
}j
}
Sleep(35);
} while (!kbhit());
return 0;
}
您对改进代码有什么建议吗?我的目的是这样的: