#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define WIDTH 80
#define HEIGHT 25
void clearScreen() {
printf("\033[2J\033[H");
}
void setBGColor() {
printf("\033[40m"); // 设置背景为黑色
printf("\033[37m"); // 设置文字为白色
}
void printStar(int x, int y) {
printf("\033[%d;%dH*", y, x);
}
int main() {
srand(time(NULL)); // 初始化随机数种子
clearScreen(); // 清屏
setBGColor(); // 设置背景和文字颜色
while (1) {
clearScreen(); // 每次循环清屏,重新绘制星星
for (int i = 0; i < 100; i++) { // 随机生成100颗星星
int x = rand() % WIDTH + 1; // 随机生成x坐标
int y = rand() % HEIGHT + 1; // 随机生成y坐标
printStar(x, y); // 在指定位置打印星星
}
usleep(100000); // 暂停0.1秒
}
return 0;
}
|